Skip to content

Commit

Permalink
test: added portokasse tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schaechinger committed Aug 10, 2021
1 parent 291dddf commit 07e8f3a
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .mocharc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

module.exports = {
timeout: 10000,
timeout: 5000,
recursive: true,
};
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@
"@types/inversify": "^2.0.33",
"@types/md5": "^2.3.1",
"@types/mocha": "^9.0.0",
"@types/moxios": "^0.4.12",
"@types/node": "^16.4.3",
"@types/sinon": "10.0.2",
"@types/tough-cookie": "^4.0.1",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"husky": "^7.0.1",
"mocha": "^9.0.3",
"moxios": "^0.4.0",
"nyc": "^15.1.0",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
Expand Down
21 changes: 8 additions & 13 deletions src/portokasse/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface Portokasse {
topUp(amount: Amount | number, paymentMethod: PaymentMethod): Promise<Amount | false>;
}

const BASE_URL = 'https://portokasse.deutschepost.de/portokasse';
export const BASE_URL = 'https://portokasse.deutschepost.de/portokasse';

@injectable()
export class PortokasseService extends RestService implements Portokasse {
Expand All @@ -33,16 +33,14 @@ export class PortokasseService extends RestService implements Portokasse {
}

public async init(options: PortokasseServiceOptions): Promise<boolean> {
if (!this.cookieJar) {
if (!options.user) {
throw new UserError('Missing user credentials for Portokasse service init.');
}

axiosCookieJarSupport(axios);
this.user.setCredentials(options.user);
this.cookieJar = new CookieJar();
if (!options.user) {
throw new UserError('Missing user credentials for Portokasse service init.');
}

axiosCookieJarSupport(axios);
this.user.setCredentials(options.user);
this.cookieJar = new CookieJar();

return this.login();
}

Expand Down Expand Up @@ -104,19 +102,16 @@ export class PortokasseService extends RestService implements Portokasse {
try {
const res = await axios(options);

if (res.headers['set-cookie']) {
if (res.headers && res.headers['set-cookie']) {
res.headers['set-cookie'].forEach((cookie: string) => {
if (cookie.startsWith('CSRF-TOKEN')) {
this.csrf = cookie.substr(11, 36);
}
});
}
console.log(res.status, res.data);

return res.data;
} catch (e) {
console.log(e.response?.data || e);

return e.response?.data || null;
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/1c4a/helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { PartnerCredentials } from '../../src/1c4a/Partner';
import { UserCredentials } from '../../src/User';

export const partnerCredentials: PartnerCredentials = {
id: 'partnerid',
secret: 'secret',
keyPhase: 2
};

export const userCredentials: UserCredentials = {
username: 'username',
password: 'password'
};
76 changes: 71 additions & 5 deletions test/portokasse/Service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,86 @@
import { expect } from 'chai';
import moxios from 'moxios';
import { UserError } from '../../src/Error';
import { PortokasseService } from '../../src/portokasse/Service';
import { userStub } from '../stubs/User.stub';
import { PaymentMethod, PortokasseService } from '../../src/portokasse/Service';
import { userCredentials } from '../1c4a/helper';
import { User } from '../../src/User';

describe('Portokasse Service', () => {
let service: PortokasseService;
const token = 'bb993a82-8bf8-483d-b5cc-b8cbd1abbd22';

beforeEach(() => {
service = new PortokasseService(userStub);
moxios.install();
moxios.stubOnce('get', /\/login$/, {
status: 200,
headers: {
'set-cookie': [`CSRF-TOKEN=${token}; path=/portokasse; secure`]
},
response: { email: 'username' }
});
moxios.stubOnce('post', /\/login$/, {
status: 200,
headers: {},
response: { email: '[email protected]' }
});

service = new PortokasseService(new User());
});

afterEach(() => {
moxios.uninstall();
});

describe('init', () => {
it('should prevent init without user credentials', async () => {
await expect(service.init({} as any)).to.eventually.be.rejectedWith(UserError);
expect(service.init({} as any)).to.eventually.be.rejectedWith(UserError);
expect(service.isInitialized()).to.be.false;
});

it('should init with minimal options', async () => {
expect(service.init({ user: userCredentials })).to.eventually.be.fulfilled;
expect(service.isInitialized()).to.be.true;
});
});

it('should access the homepage', () => {});
describe('getUserInfo', () => {
it('should fail to get user info before init', async () => {
moxios.stubOnce('get', /\/wallet-overviews\/me$/, {
status: 401,
headers: {},
response: {
code: 'UNAUTHORIZED'
}
});

const info = await service.getUserInfo();

expect(info.isAuthenticated).to.be.false;
});

it('should retrieve user balance', async () => {
moxios.stubOnce('get', /\/wallet-overviews\/me$/, {
status: 200,
headers: {},
response: {
balance: 2000
}
});

await service.init({ user: userCredentials });
const info = await service.getUserInfo();

expect(info.isAuthenticated).to.be.true;
expect(info.walletBalance).to.exist;
expect(info.walletBalance!.value).to.equal(20);
});
});

describe('topUp', () => {
it('should add tests once topup is implemented', async () => {
const res = await service.topUp(1000, PaymentMethod.GiroPay);

expect(res).to.be.false;
});
});
});

0 comments on commit 07e8f3a

Please sign in to comment.