2022-09-21 09:04:16 +02:00
|
|
|
import assert from "assert";
|
|
|
|
import { config } from "../../src/config";
|
|
|
|
import axios from "axios";
|
|
|
|
import * as tokenUtils from "../../src/utils/tokenUtils";
|
|
|
|
import MockAdapter from "axios-mock-adapter";
|
2022-10-30 22:07:25 +01:00
|
|
|
import { validateLicenseKeyRegex } from "../../src/routes/verifyToken";
|
2022-09-21 09:04:16 +02:00
|
|
|
let mock: MockAdapter;
|
2022-09-25 04:10:29 +02:00
|
|
|
import * as patreon from "../mocks/patreonMock";
|
2022-09-21 09:04:16 +02:00
|
|
|
|
2022-10-30 22:07:25 +01:00
|
|
|
const validateToken = validateLicenseKeyRegex;
|
2022-09-21 09:04:16 +02:00
|
|
|
|
|
|
|
describe("tokenUtils test", function() {
|
|
|
|
before(function() {
|
|
|
|
mock = new MockAdapter(axios, { onNoMatch: "throwException" });
|
2022-09-25 04:10:29 +02:00
|
|
|
mock.onPost("https://www.patreon.com/api/oauth2/token").reply(200, patreon.fakeOauth);
|
2022-09-25 08:04:30 +02:00
|
|
|
mock.onGet(/identity/).reply(200, patreon.activeIdentity);
|
2022-09-21 09:04:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it("Should be able to create patreon token", function (done) {
|
|
|
|
if (!config?.patreon) this.skip();
|
|
|
|
tokenUtils.createAndSaveToken(tokenUtils.TokenType.patreon, "test_code").then((licenseKey) => {
|
|
|
|
assert.ok(validateToken(licenseKey));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("Should be able to create local token", (done) => {
|
|
|
|
tokenUtils.createAndSaveToken(tokenUtils.TokenType.local).then((licenseKey) => {
|
|
|
|
assert.ok(validateToken(licenseKey));
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("Should be able to get patreon identity", function (done) {
|
|
|
|
if (!config?.patreon) this.skip();
|
|
|
|
tokenUtils.getPatreonIdentity("fake_access_token").then((result) => {
|
2022-09-25 08:04:30 +02:00
|
|
|
assert.deepEqual(result, patreon.activeIdentity);
|
2022-09-21 09:04:16 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("Should be able to refresh token", function (done) {
|
|
|
|
if (!config?.patreon) this.skip();
|
|
|
|
tokenUtils.refreshToken(tokenUtils.TokenType.patreon, "fake-licence-Key", "fake_refresh_token").then((result) => {
|
|
|
|
assert.strictEqual(result, true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function () {
|
|
|
|
mock.restore();
|
|
|
|
});
|
|
|
|
});
|