Merge pull request #583 from kolplattformen/feature/api-test-app-hjarntorget
Add support for running api-test-app with api-hjarntorget
This commit is contained in:
commit
f7aa4675a4
|
@ -14,9 +14,13 @@ const path = require('path')
|
|||
const fs = require('fs')
|
||||
const HttpProxyAgent = require('https-proxy-agent')
|
||||
const agentWrapper = require('./app/agentFetchWrapper')
|
||||
const init = require('@skolplattformen/api-skolplattformen').default
|
||||
const initSkolplattformen = require('@skolplattformen/api-skolplattformen').default
|
||||
const initHjarntorget = require('@skolplattformen/api-hjarntorget').default
|
||||
|
||||
const [, , personalNumber, platform] = process.argv
|
||||
const isHjarntorget = platform && platform.startsWith('hj')
|
||||
const init = isHjarntorget ? initHjarntorget : initSkolplattformen;
|
||||
|
||||
const [, , personalNumber] = process.argv
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
|
||||
const cookieJar = new CookieJar()
|
||||
let bankIdUsed = false
|
||||
|
@ -136,10 +140,9 @@ async function Login(api) {
|
|||
try {
|
||||
console.log('Attempt to use saved session cookie to login')
|
||||
const rawContent = await readFile(`${recordFolder}/latestSessionCookie.txt`)
|
||||
const sessionCookie = JSON.parse(rawContent)
|
||||
|
||||
await api.setSessionCookie(`${sessionCookie.key}=${sessionCookie.value}`)
|
||||
|
||||
const sessionCookies = JSON.parse(rawContent)
|
||||
await api.setSessionCookie(`${sessionCookies[0].key}=${sessionCookies[0].value}`)
|
||||
|
||||
useBankId = false
|
||||
console.log('Login with old cookie succeeded')
|
||||
} catch (error) {
|
||||
|
@ -177,10 +180,12 @@ function ensureDirectoryExistence(filePath) {
|
|||
fs.mkdirSync(dirname)
|
||||
}
|
||||
|
||||
|
||||
function getSessionCookieFromCookieJar() {
|
||||
const cookies = cookieJar.getCookiesSync('https://etjanst.stockholm.se')
|
||||
const sessionCookie = cookies.find((c) => c.key === 'SMSESSION')
|
||||
return sessionCookie
|
||||
const cookieUrl = isHjarntorget ? 'https://hjarntorget.goteborg.se' : 'https://etjanst.stockholm.se'
|
||||
const cookies = cookieJar.getCookiesSync(cookieUrl)
|
||||
const sessionCookieKey = isHjarntorget ? 'JSESSIONID' : 'SMSESSION'
|
||||
return cookies.find(c => c.key === sessionCookieKey)
|
||||
}
|
||||
|
||||
const record = async (info, data) => {
|
||||
|
|
|
@ -147,13 +147,8 @@ export class ApiHjarntorget extends EventEmitter implements Api {
|
|||
}
|
||||
|
||||
async setSessionCookie(sessionCookie: string): Promise<void> {
|
||||
await this.fetch('login-cookie', hjarntorgetUrl, {
|
||||
headers: {
|
||||
cookie: sessionCookie,
|
||||
},
|
||||
redirect: 'manual',
|
||||
})
|
||||
|
||||
this.cookieManager.setCookieString(sessionCookie, hjarntorgetUrl)
|
||||
|
||||
const user = await this.getUser()
|
||||
if (!user.isAuthenticated) {
|
||||
throw new Error('Session cookie is expired')
|
||||
|
|
Loading…
Reference in New Issue