Skip to content

Commit e0be474

Browse files
committed
fix tokenUtils tests, skip if not configured
1 parent dd7656d commit e0be474

File tree

6 files changed

+173
-1
lines changed

6 files changed

+173
-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
@@ -39,8 +39,10 @@
3939
"@types/mocha": "^9.1.1",
4040
"@types/node": "^18.0.3",
4141
"@types/pg": "^8.6.5",
42+
"@types/sinon": "^10.0.13",
4243
"@typescript-eslint/eslint-plugin": "^5.30.6",
4344
"@typescript-eslint/parser": "^5.30.6",
45+
"axios-mock-adapter": "^1.21.2",
4446
"eslint": "^8.19.0",
4547
"mocha": "^10.0.0",
4648
"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.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,10 @@
5959
"statusCode": 200
6060
}
6161
},
62+
"patreon": {
63+
"clientId": "testClientID",
64+
"clientSecret": "testClientSecret",
65+
"redirectUri": "http://127.0.0.1/fake/callback"
66+
},
6267
"minReputationToSubmitFiller": -1
6368
}

test/cases/tokenUtils.ts

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

0 commit comments

Comments
 (0)