|
| 1 | +import assert from "assert"; |
| 2 | +import { config } from "../../src/config"; |
| 3 | +import axios from "axios"; |
| 4 | +import { createAndSaveToken, TokenType } from "../../src/utils/tokenUtils"; |
| 5 | +import MockAdapter from "axios-mock-adapter"; |
| 6 | +let mock: MockAdapter; |
| 7 | +import * as patreon from "../mocks/patreonMock"; |
| 8 | +import * as gumroad from "../mocks/gumroadMock"; |
| 9 | +import { client } from "../utils/httpClient"; |
| 10 | +import { validatelicenseKeyRegex } from "../../src/routes/verifyToken"; |
| 11 | + |
| 12 | +const generateEndpoint = "/api/generateToken"; |
| 13 | +const getGenerateToken = (type: string, code: string | null, adminUserID: string | null) => client({ |
| 14 | + url: `${generateEndpoint}/${type}`, |
| 15 | + params: { code, adminUserID } |
| 16 | +}); |
| 17 | + |
| 18 | +const verifyEndpoint = "/api/verifyToken"; |
| 19 | +const getVerifyToken = (licenseKey: string | null) => client({ |
| 20 | + url: verifyEndpoint, |
| 21 | + params: { licenseKey } |
| 22 | +}); |
| 23 | + |
| 24 | +let patreonLicense: string; |
| 25 | +let localLicense: string; |
| 26 | +const gumroadLicense = gumroad.generateLicense(); |
| 27 | + |
| 28 | +const extractLicenseKey = (data: string) => { |
| 29 | + const regex = /([A-Za-z0-9]{40})/; |
| 30 | + const match = data.match(regex); |
| 31 | + if (!match) throw new Error("Failed to extract license key"); |
| 32 | + return match[1]; |
| 33 | +}; |
| 34 | + |
| 35 | +describe("generateToken test", function() { |
| 36 | + |
| 37 | + before(function() { |
| 38 | + mock = new MockAdapter(axios, { onNoMatch: "throwException" }); |
| 39 | + mock.onPost("https://www.patreon.com/api/oauth2/token").reply(200, patreon.fakeOauth); |
| 40 | + }); |
| 41 | + |
| 42 | + after(function () { |
| 43 | + mock.restore(); |
| 44 | + }); |
| 45 | + |
| 46 | + it("Should be able to create patreon token for active patron", function (done) { |
| 47 | + mock.onGet(/identity/).reply(200, patreon.activeIdentity); |
| 48 | + if (!config?.patreon) this.skip(); |
| 49 | + getGenerateToken("patreon", "patreon_code", "").then(res => { |
| 50 | + patreonLicense = extractLicenseKey(res.data); |
| 51 | + assert.ok(validatelicenseKeyRegex(patreonLicense)); |
| 52 | + done(); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it("Should be able to create new local token", function (done) { |
| 57 | + createAndSaveToken(TokenType.local).then((licenseKey) => { |
| 58 | + assert.ok(validatelicenseKeyRegex(licenseKey)); |
| 59 | + localLicense = licenseKey; |
| 60 | + done(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it("Should return 400 if missing code parameter", function (done) { |
| 65 | + getGenerateToken("patreon", null, "").then(res => { |
| 66 | + assert.strictEqual(res.status, 400); |
| 67 | + done(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it("Should return 403 if missing adminuserID parameter", function (done) { |
| 72 | + getGenerateToken("local", "fake-code", null).then(res => { |
| 73 | + assert.strictEqual(res.status, 403); |
| 74 | + done(); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it("Should return 403 for invalid adminuserID parameter", function (done) { |
| 79 | + getGenerateToken("local", "fake-code", "fakeAdminID").then(res => { |
| 80 | + assert.strictEqual(res.status, 403); |
| 81 | + done(); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
| 85 | + |
| 86 | +describe("verifyToken static tests", function() { |
| 87 | + it("Should fast reject invalid token", function (done) { |
| 88 | + getVerifyToken("00000").then(res => { |
| 89 | + assert.strictEqual(res.status, 200); |
| 90 | + assert.ok(!res.data.allowed); |
| 91 | + done(); |
| 92 | + }).catch(err => done(err)); |
| 93 | + }); |
| 94 | + |
| 95 | + it("Should return 400 if missing code token", function (done) { |
| 96 | + getVerifyToken(null).then(res => { |
| 97 | + assert.strictEqual(res.status, 400); |
| 98 | + done(); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe("verifyToken mock tests", function() { |
| 104 | + |
| 105 | + beforeEach(function() { |
| 106 | + mock = new MockAdapter(axios, { onNoMatch: "throwException" }); |
| 107 | + }); |
| 108 | + |
| 109 | + afterEach(function () { |
| 110 | + mock.restore(); |
| 111 | + }); |
| 112 | + |
| 113 | + it("Should accept current patron", function (done) { |
| 114 | + if (!config?.patreon) this.skip(); |
| 115 | + mock.onGet(/identity/).reply(200, patreon.activeIdentity); |
| 116 | + getVerifyToken(patreonLicense).then(res => { |
| 117 | + assert.strictEqual(res.status, 200); |
| 118 | + assert.ok(res.data.allowed); |
| 119 | + done(); |
| 120 | + }).catch(err => done(err)); |
| 121 | + }); |
| 122 | + |
| 123 | + it("Should reject nonexistent patron", function (done) { |
| 124 | + if (!config?.patreon) this.skip(); |
| 125 | + mock.onGet(/identity/).reply(200, patreon.invalidIdentity); |
| 126 | + getVerifyToken(patreonLicense).then(res => { |
| 127 | + assert.strictEqual(res.status, 200); |
| 128 | + assert.ok(!res.data.allowed); |
| 129 | + done(); |
| 130 | + }).catch(err => done(err)); |
| 131 | + }); |
| 132 | + |
| 133 | + it("Should accept qualitying former patron", function (done) { |
| 134 | + if (!config?.patreon) this.skip(); |
| 135 | + mock.onGet(/identity/).reply(200, patreon.formerIdentitySucceed); |
| 136 | + getVerifyToken(patreonLicense).then(res => { |
| 137 | + assert.strictEqual(res.status, 200); |
| 138 | + assert.ok(res.data.allowed); |
| 139 | + done(); |
| 140 | + }).catch(err => done(err)); |
| 141 | + }); |
| 142 | + |
| 143 | + it("Should reject unqualitifed former patron", function (done) { |
| 144 | + if (!config?.patreon) this.skip(); |
| 145 | + mock.onGet(/identity/).reply(200, patreon.formerIdentityFail); |
| 146 | + getVerifyToken(patreonLicense).then(res => { |
| 147 | + assert.strictEqual(res.status, 200); |
| 148 | + assert.ok(!res.data.allowed); |
| 149 | + done(); |
| 150 | + }).catch(err => done(err)); |
| 151 | + }); |
| 152 | + |
| 153 | + it("Should accept real gumroad key", function (done) { |
| 154 | + mock.onPost("https://api.gumroad.com/v2/licenses/verify").reply(200, gumroad.licenseSuccess); |
| 155 | + getVerifyToken(gumroadLicense).then(res => { |
| 156 | + assert.strictEqual(res.status, 200); |
| 157 | + assert.ok(res.data.allowed); |
| 158 | + done(); |
| 159 | + }).catch(err => done(err)); |
| 160 | + }); |
| 161 | + |
| 162 | + it("Should reject fake gumroad key", function (done) { |
| 163 | + mock.onPost("https://api.gumroad.com/v2/licenses/verify").reply(200, gumroad.licenseFail); |
| 164 | + getVerifyToken(gumroadLicense).then(res => { |
| 165 | + assert.strictEqual(res.status, 200); |
| 166 | + assert.ok(!res.data.allowed); |
| 167 | + done(); |
| 168 | + }).catch(err => done(err)); |
| 169 | + }); |
| 170 | + |
| 171 | + it("Should validate local license", function (done) { |
| 172 | + getVerifyToken(localLicense).then(res => { |
| 173 | + assert.strictEqual(res.status, 200); |
| 174 | + assert.ok(res.data.allowed); |
| 175 | + done(); |
| 176 | + }).catch(err => done(err)); |
| 177 | + }); |
| 178 | +}); |
0 commit comments