Skip to content

Commit f333a19

Browse files
committed
add tokenUtils tests
1 parent 2f7e51d commit f333a19

File tree

5 files changed

+169
-1
lines changed

5 files changed

+169
-1
lines changed

ci.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"discordCompletelyIncorrectReportWebhookURL": "http://127.0.0.1:8081/webhook/CompletelyIncorrectReport",
1010
"discordNeuralBlockRejectWebhookURL": "http://127.0.0.1:8081/webhook/NeuralBlockReject",
1111
"neuralBlockURL": "http://127.0.0.1:8081/NeuralBlock",
12-
"userCounterURL": "https://127.0.0.1:8081/UserCounter",
12+
"userCounterURL": "http://127.0.0.1:8081/UserCounter",
1313
"behindProxy": true,
1414
"postgres": {
1515
"user": "ci_db_user",
@@ -71,5 +71,10 @@
7171
"statusCode": 200
7272
}
7373
},
74+
"patreon": {
75+
"clientID": "testClientID",
76+
"clientSecret": "testClientSecret",
77+
"redirectUri": "http://127.0.0.1/fake/callback"
78+
},
7479
"minReputationToSubmitFiller": -1
7580
}

package-lock.json

Lines changed: 84 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@
4141
"@types/mocha": "^9.1.1",
4242
"@types/node": "^18.0.3",
4343
"@types/pg": "^8.6.5",
44+
"@types/sinon": "^10.0.13",
4445
"@typescript-eslint/eslint-plugin": "^5.30.6",
4546
"@typescript-eslint/parser": "^5.30.6",
47+
"axios-mock-adapter": "^1.21.2",
4648
"eslint": "^8.19.0",
4749
"mocha": "^10.0.0",
4850
"nodemon": "^2.0.19",

src/routes/verifyToken.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@ interface VerifyTokenRequest extends Request {
1212
}
1313
}
1414

15+
export const validatelicenseKeyRegex = (token: string) =>
16+
new RegExp(/[A-Za-z0-9]{40}/).test(token);
17+
1518
export async function verifyTokenRequest(req: VerifyTokenRequest, res: Response): Promise<Response> {
1619
const { query: { licenseKey } } = req;
1720

1821
if (!licenseKey) {
1922
return res.status(400).send("Invalid request");
23+
} else if (!validatelicenseKeyRegex(licenseKey)) {
24+
// fast check for invalid licence key
25+
return res.status(200).send({
26+
allowed: false
27+
});
2028
}
2129
const licenseRegex = new RegExp(/[a-zA-Z0-9]{40}|[A-Z0-9-]{35}/);
2230
if (!licenseRegex.test(licenseKey)) {

test/cases/tokenUtils.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import assert from "assert";
2+
import axios from "axios";
3+
import * as tokenUtils from "../../src/utils/tokenUtils";
4+
import MockAdapter from "axios-mock-adapter";
5+
import { validatelicenseKeyRegex } from "../../src/routes/verifyToken";
6+
let mock: MockAdapter;
7+
8+
const validateToken = validatelicenseKeyRegex;
9+
10+
describe("tokenUtils test", function() {
11+
before(function() {
12+
mock = new MockAdapter(axios, { onNoMatch: "throwException" });
13+
});
14+
15+
it("Should be able to create patreon token", (done) => {
16+
mock.onPost("https://www.patreon.com/api/oauth2/token").reply(200, {
17+
access_token: "test_access_token",
18+
refresh_token: "test_refresh_token",
19+
expires_in: 3600,
20+
});
21+
tokenUtils.createAndSaveToken(tokenUtils.TokenType.patreon, "test_code").then((licenseKey) => {
22+
assert.ok(validateToken(licenseKey));
23+
done();
24+
});
25+
});
26+
it("Should be able to create local token", (done) => {
27+
tokenUtils.createAndSaveToken(tokenUtils.TokenType.local).then((licenseKey) => {
28+
assert.ok(validateToken(licenseKey));
29+
done();
30+
});
31+
});
32+
it("Should be able to get patreon identity", (done) => {
33+
const fakePatreonIdentity = {
34+
data: {},
35+
links: {},
36+
included: [
37+
{
38+
attributes: {
39+
is_monthly: true,
40+
currently_entitled_amount_cents: 100,
41+
patron_status: "active_patron",
42+
},
43+
id: "id",
44+
type: "campaign"
45+
}
46+
],
47+
};
48+
mock.onGet(/identity/).reply(200, fakePatreonIdentity);
49+
tokenUtils.getPatreonIdentity("fake_access_token").then((result) => {
50+
assert.deepEqual(result, fakePatreonIdentity);
51+
done();
52+
});
53+
});
54+
it("Should be able to refresh token", (done) => {
55+
mock.onPost("https://www.patreon.com/api/oauth2/token").reply(200, {
56+
access_token: "test_access_token",
57+
refresh_token: "test_refresh_token",
58+
expires_in: 3600,
59+
});
60+
tokenUtils.refreshToken(tokenUtils.TokenType.patreon, "fake-licence-Key", "fake_refresh_token").then((result) => {
61+
assert.strictEqual(result, true);
62+
done();
63+
});
64+
});
65+
66+
after(function () {
67+
mock.restore();
68+
});
69+
});

0 commit comments

Comments
 (0)