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