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
+ let mock : MockAdapter ;
7
+ import * as patreon from "../mocks/patreonMock" ;
8
+ import { client } from "../utils/httpClient" ;
9
+
10
+ const validateToken = ( token : string ) =>
11
+ new RegExp ( / [ A - Z a - z 0 - 9 ] { 40 } | [ A - Z a - z 0 - 9 - ] { 35 } / ) . test ( token ) ;
12
+
13
+ const endpoint = "/api/verifyToken" ;
14
+ const getVerifyToken = ( licenseKey : string ) => client ( {
15
+ url : endpoint ,
16
+ params : { licenseKey }
17
+ } ) ;
18
+
19
+ describe ( "tokenUtils test" , function ( ) {
20
+ let patreonLicense : string , localLicense : string , gumroadLicense : string ;
21
+
22
+ before ( function ( ) {
23
+ mock = new MockAdapter ( axios , { onNoMatch : "throwException" } ) ;
24
+ mock . onPost ( "https://www.patreon.com/api/oauth2/token" ) . reply ( 200 , patreon . fakeOauth ) ;
25
+ mock . onGet ( / i d e n t i t y / ) . reply ( 200 , patreon . fakeIdentity ) ;
26
+ } ) ;
27
+
28
+ after ( function ( ) {
29
+ mock . restore ( ) ;
30
+ } ) ;
31
+
32
+ it ( "Should be able to create patreon token" , function ( done ) {
33
+ if ( ! config ?. patreon ) this . skip ( ) ;
34
+ tokenUtils . createAndSaveToken ( tokenUtils . TokenType . patreon , "test_code" ) . then ( ( licenseKey ) => {
35
+ patreonLicense = licenseKey ;
36
+ assert . ok ( validateToken ( licenseKey ) ) ;
37
+ done ( ) ;
38
+ } ) ;
39
+ } ) ;
40
+
41
+ it ( "Should be able to create local token" , ( done ) => {
42
+ tokenUtils . createAndSaveToken ( tokenUtils . TokenType . local ) . then ( ( licenseKey ) => {
43
+ localLicense = licenseKey ;
44
+ assert . ok ( validateToken ( licenseKey ) ) ;
45
+ done ( ) ;
46
+ } ) ;
47
+ } ) ;
48
+
49
+ it ( "Should be able to create local token" , ( done ) => {
50
+ getVerifyToken ( patreonLicense ) . then ( ( licenseKey ) => {
51
+ localLicense = licenseKey ;
52
+ assert . ok ( validateToken ( licenseKey ) ) ;
53
+ done ( ) ;
54
+ } ) ;
55
+ } ) ;
56
+ } ) ;
0 commit comments