1
- import { getHash } from "../../src/utils/getHash" ;
2
1
import { HashedUserID } from "../../src/types/user.model" ;
3
2
import { client } from "../utils/httpClient" ;
4
3
import { db } from "../../src/databases/databases" ;
5
4
import assert from "assert" ;
5
+ import { genAnonUser , genUsers } from "../utils/genUser" ;
6
6
7
7
// helpers
8
8
const checkUserVIP = ( publicID : string ) => db . prepare ( "get" , `SELECT "userID" FROM "vipUsers" WHERE "userID" = ?` , [ publicID ] ) ;
9
9
10
+ const cases = [
11
+ "vip-1" ,
12
+ ] ;
13
+ const users = genUsers ( "endpoint" , cases ) ;
14
+
15
+ // hardcoded into test code
10
16
const adminPrivateUserID = "testUserId" ;
11
- const permVIP1 = "addVIP_permaVIPOne" ;
12
- const publicPermVIP1 = getHash ( permVIP1 ) as HashedUserID ;
13
- const permVIP2 = "addVIP_permaVIPTwo" ;
14
- const publicPermVIP2 = getHash ( permVIP2 ) as HashedUserID ;
15
- const permVIP3 = "addVIP_permaVIPThree" ;
16
- const publicPermVIP3 = getHash ( permVIP3 ) as HashedUserID ;
17
17
18
18
const endpoint = "/api/addUserAsVIP" ;
19
19
const addUserAsVIP = ( userID : string , enabled : boolean , adminUserID = adminPrivateUserID ) => client ( {
@@ -26,116 +26,68 @@ const addUserAsVIP = (userID: string, enabled: boolean, adminUserID = adminPriva
26
26
}
27
27
} ) ;
28
28
29
+ const testVIPUpdate = ( target : HashedUserID , enabled : boolean , adminID : string = adminPrivateUserID ) =>
30
+ addUserAsVIP ( target , enabled , adminID )
31
+ . then ( res => assert . strictEqual ( res . status , 200 ) )
32
+ . then ( ( ) => checkUserVIP ( target ) )
33
+ . then ( row => assert . ok ( Boolean ( row ) == enabled ) ) ;
34
+
35
+ const statusTest = ( status : number , data : Record < string , any > ) =>
36
+ client ( {
37
+ method : "POST" ,
38
+ url : endpoint ,
39
+ params : data
40
+ } ) . then ( res => assert . strictEqual ( res . status , status ) ) ;
41
+
29
42
describe ( "addVIP test" , function ( ) {
30
- it ( "User should not already be VIP" , ( done ) => {
31
- checkUserVIP ( publicPermVIP1 )
32
- . then ( result => {
33
- assert . ok ( ! result ) ;
34
- done ( ) ;
35
- } )
36
- . catch ( err => done ( err ) ) ;
37
- } ) ;
38
- it ( "Should be able to add user as VIP" , ( done ) => {
39
- addUserAsVIP ( publicPermVIP1 , true )
40
- . then ( async res => {
41
- assert . strictEqual ( res . status , 200 ) ;
42
- const row = await checkUserVIP ( publicPermVIP1 ) ;
43
- assert . ok ( row ) ;
44
- done ( ) ;
45
- } )
46
- . catch ( err => done ( err ) ) ;
47
- } ) ;
48
- it ( "Should be able to add second user as VIP" , ( done ) => {
49
- addUserAsVIP ( publicPermVIP2 , true )
50
- . then ( async res => {
51
- assert . strictEqual ( res . status , 200 ) ;
52
- const row = await checkUserVIP ( publicPermVIP2 ) ;
53
- assert . ok ( row ) ;
54
- done ( ) ;
55
- } )
56
- . catch ( err => done ( err ) ) ;
57
- } ) ;
58
- it ( "Should return 403 with invalid adminID" , ( done ) => {
59
- addUserAsVIP ( publicPermVIP1 , true , "Invalid_Admin_User_ID" )
60
- . then ( res => {
61
- assert . strictEqual ( res . status , 403 ) ;
62
- done ( ) ;
63
- } )
64
- . catch ( err => done ( err ) ) ;
65
- } ) ;
66
- it ( "Should return 400 with missing adminID" , ( done ) => {
67
- client ( {
68
- method : "POST" ,
69
- url : endpoint ,
70
- params : {
71
- userID : publicPermVIP1 ,
72
- enabled : String ( true )
73
- }
43
+ it ( "User should not already be VIP" , ( ) =>
44
+ checkUserVIP ( users [ "vip-1" ] . pubID )
45
+ . then ( result => assert . ok ( ! result ) )
46
+ ) ;
47
+ it ( "Should be able to add user as VIP" , ( ) =>
48
+ testVIPUpdate ( users [ "vip-1" ] . pubID , true )
49
+ ) ;
50
+ it ( "Should be able to remove VIP" , ( ) =>
51
+ testVIPUpdate ( users [ "vip-1" ] . pubID , false )
52
+ ) ;
53
+ it ( "Should be able to add second user as VIP" , ( ) =>
54
+ testVIPUpdate ( genAnonUser ( ) . pubID , true )
55
+ ) ;
56
+ it ( "Should return 403 with invalid adminID" , ( ) =>
57
+ addUserAsVIP ( genAnonUser ( ) . pubID , true , genAnonUser ( ) . privID )
58
+ . then ( res => assert . strictEqual ( res . status , 403 ) )
59
+ ) ;
60
+ it ( "Should return 400 with missing adminID" , ( ) =>
61
+ statusTest ( 400 , {
62
+ userID : genAnonUser ( ) . pubID ,
63
+ enabled : String ( true )
74
64
} )
75
- . then ( res => {
76
- assert . strictEqual ( res . status , 400 ) ;
77
- done ( ) ;
78
- } )
79
- . catch ( err => done ( err ) ) ;
80
- } ) ;
81
- it ( "Should return 400 with missing userID" , ( done ) => {
82
- client ( {
83
- method : "POST" ,
84
- url : endpoint ,
85
- params : {
86
- enabled : String ( true ) ,
87
- adminUserID : adminPrivateUserID
88
- }
65
+ ) ;
66
+ it ( "Should return 400 with missing userID" , ( ) =>
67
+ statusTest ( 400 , {
68
+ enabled : String ( true ) ,
69
+ adminUserID : adminPrivateUserID
89
70
} )
90
- . then ( res => {
91
- assert . strictEqual ( res . status , 400 ) ;
92
- done ( ) ;
93
- } )
94
- . catch ( err => done ( err ) ) ;
95
- } ) ;
96
- it ( "Should be able to remove VIP" , ( done ) => {
97
- addUserAsVIP ( publicPermVIP1 , false )
98
- . then ( async res => {
99
- assert . strictEqual ( res . status , 200 ) ;
100
- const row = await checkUserVIP ( publicPermVIP1 ) ;
101
- assert . ok ( ! row ) ;
102
- done ( ) ;
103
- } )
104
- . catch ( err => done ( err ) ) ;
105
- } ) ;
106
- it ( "Should remove VIP if enabled is false" , ( done ) => {
107
- client ( {
108
- method : "POST" ,
109
- url : endpoint ,
110
- params : {
111
- userID : publicPermVIP2 ,
71
+ ) ;
72
+ it ( "Should remove VIP if enabled is not true" , ( ) => {
73
+ const user = genAnonUser ( ) ;
74
+ return testVIPUpdate ( user . pubID , true )
75
+ . then ( ( ) => statusTest ( 200 , {
76
+ userID : user . pubID ,
112
77
adminUserID : adminPrivateUserID ,
113
78
enabled : "invalid-text"
114
- }
115
- } )
116
- . then ( async res => {
117
- assert . strictEqual ( res . status , 200 ) ;
118
- const row = await checkUserVIP ( publicPermVIP2 ) ;
119
- assert . ok ( ! row ) ;
120
- done ( ) ;
121
- } )
122
- . catch ( err => done ( err ) ) ;
79
+ } ) )
80
+ . then ( ( ) => checkUserVIP ( user . pubID ) )
81
+ . then ( row => assert . ok ( ! row ) ) ;
123
82
} ) ;
124
- it ( "Should remove VIP if enabled is missing" , ( done ) => {
125
- client ( {
126
- method : "POST" ,
127
- url : endpoint ,
128
- params : {
129
- userID : publicPermVIP3 ,
130
- adminUserID : adminPrivateUserID
131
- }
132
- } )
133
- . then ( async res => {
134
- assert . strictEqual ( res . status , 200 ) ;
135
- const row = await checkUserVIP ( publicPermVIP3 ) ;
136
- assert . ok ( ! row ) ;
137
- done ( ) ;
138
- } )
139
- . catch ( err => done ( err ) ) ;
83
+ it ( "Should remove VIP if enabled is missing" , ( ) => {
84
+ const user = genAnonUser ( ) ;
85
+ return testVIPUpdate ( user . pubID , true )
86
+ . then ( ( ) => statusTest ( 200 , {
87
+ userID : user . pubID ,
88
+ adminUserID : adminPrivateUserID ,
89
+ } ) )
90
+ . then ( ( ) => checkUserVIP ( user . pubID ) )
91
+ . then ( row => assert . ok ( ! row ) ) ;
140
92
} ) ;
141
93
} ) ;
0 commit comments