1
- import * as response from "../../../src/constants/responses" ;
2
- import JSONResponse from "../../../src/utils/JsonResponse" ;
3
- import { verifyCommand } from "../../../src/controllers/verifyCommand" ;
4
- import { guildEnv } from "../../fixtures/fixture " ;
1
+ import {
2
+ RETRY_COMMAND ,
3
+ VERIFICATION_STRING ,
4
+ } from "../../../src/constants/responses " ;
5
5
import config from "../../../config/config" ;
6
6
7
- describe ( "verifyCommand" , ( ) => {
8
- test ( "should return INTERNAL_SERVER_ERROR when response is not ok" , async ( ) => {
9
-
10
- jest . mock ( "crypto" , ( ) => {
11
- return {
12
- randomUUID : jest . fn ( ( ) => 'shreya' ) ,
13
- subtle : { digest : jest . fn ( ( ) => "123" ) } ,
14
- } ;
15
- } ) ;
7
+ const mockDateNow = 1626512345678 ;
8
+ const UNIQUE_TOKEN = "UNIQUE_TOKEN" ;
9
+ const env = {
10
+ BOT_PUBLIC_KEY : "BOT_PUBLIC_KEY" ,
11
+ DISCORD_GUILD_ID : "DISCORD_GUILD_ID" ,
12
+ DISCORD_TOKEN : "SIGNED_JWT" ,
13
+ } ;
16
14
15
+ describe ( "verifyCommand" , ( ) => {
16
+ beforeEach ( ( ) => {
17
+ jest . mock ( "@tsndr/cloudflare-worker-jwt" ) ;
18
+ jest . spyOn ( Date , "now" ) . mockReturnValue ( mockDateNow ) ;
17
19
jest . mock ( "../../../src/utils/generateUniqueToken" , ( ) => ( {
18
- generateUniqueToken : ( ) => Promise . resolve ( "jashdkjahskajhd" ) ,
20
+ generateUniqueToken : ( ) => Promise . resolve ( UNIQUE_TOKEN ) ,
19
21
} ) ) ;
22
+ } ) ;
20
23
21
- // const mockResponse = response.INTERNAL_SERVER_ERROR;
22
- // jest
23
- // .spyOn(global, "fetch")
24
- // .mockImplementation(() =>
25
- // Promise.resolve(new JSONResponse(mockResponse))
26
- // );
27
-
28
- const env = {
29
- BOT_PUBLIC_KEY : "xyz" ,
30
- DISCORD_GUILD_ID : "123" ,
31
- DISCORD_TOKEN : "abc" ,
32
- } ;
24
+ afterEach ( ( ) => {
25
+ jest . spyOn ( Date , "now" ) . mockRestore ( ) ;
26
+ } ) ;
33
27
28
+ test ( "should return JSON response when response is ok" , async ( ) => {
34
29
const data = {
35
- token : 1233434 ,
36
- userId : "sjkhdkjashdksjh" ,
37
- userAvatarHash : "test user" ,
38
- userName : "sndbhsbgdj" ,
39
- env : env ,
30
+ type : "discord" ,
31
+ token : UNIQUE_TOKEN ,
32
+ attributes : {
33
+ discordId : 1 ,
34
+ userAvatar : "https://cdn.discordapp.com/avatars/1/userAvatarHash.jpg" ,
35
+ userName : "userName" ,
36
+ discriminator : "discriminator" ,
37
+ expiry : mockDateNow + 1000 * 60 * 2 ,
38
+ } ,
40
39
} ;
41
40
41
+ jest . spyOn ( global , "fetch" ) . mockResolvedValueOnce ( {
42
+ ok : true ,
43
+ status : 200 ,
44
+ json : jest . fn ( ) . mockResolvedValueOnce ( data ) ,
45
+ } as unknown as Response ) ;
46
+
47
+ const { verifyCommand } = await import (
48
+ "../../../src/controllers/verifyCommand"
49
+ ) ;
50
+
42
51
const result = await verifyCommand (
43
- 1233434 ,
44
- "sjkhdkjashdksjh " ,
45
- "test user " ,
46
- "sndbhsbgdj " ,
52
+ 1 ,
53
+ "userAvatarHash " ,
54
+ "userName " ,
55
+ "discriminator " ,
47
56
env
48
57
) ;
49
58
50
- // expect(result.data.content).toEqual(response.RETRY_COMMAND);
51
59
expect ( global . fetch ) . toHaveBeenCalledWith (
52
- `https ://api.realdevsquad.com /external-accounts` ,
60
+ `http ://localhost:3000 /external-accounts` ,
53
61
{
54
62
method : "POST" ,
55
63
headers : {
56
64
"Content-Type" : "application/json" ,
57
- Authorization : `Bot ${ guildEnv . DISCORD_TOKEN } ` ,
65
+ Authorization : `Bearer ${ env . DISCORD_TOKEN } ` ,
58
66
} ,
59
67
body : JSON . stringify ( data ) ,
60
68
}
61
69
) ;
62
- } ) ;
63
-
64
- test ( "should return JSON response when response is ok" , async ( ) => {
65
- const mockResponse = { } ;
70
+ const resultText = await result . text ( ) ;
71
+ const resultData = JSON . parse ( resultText ) ;
66
72
67
- // jest
68
- // .spyOn(global, "fetch")
69
- // .mockImplementation(() =>
70
- // Promise.resolve(new JSONResponse(mockResponse))
71
- // );
73
+ const verificationSiteURL = config ( env ) . VERIFICATION_SITE_URL ;
74
+ const message =
75
+ `${ verificationSiteURL } /discord?token=${ UNIQUE_TOKEN } \n` +
76
+ VERIFICATION_STRING ;
72
77
73
- const env = {
74
- BOT_PUBLIC_KEY : "xyz" ,
75
- DISCORD_GUILD_ID : "123" ,
76
- DISCORD_TOKEN : "abc" ,
77
- } ;
78
+ expect ( resultData . data . content ) . toEqual ( message ) ;
79
+ } ) ;
78
80
81
+ test ( "should return INTERNAL_SERVER_ERROR when response is not ok" , async ( ) => {
79
82
const data = {
80
- token : 1233434 ,
81
- userId : "sjkhdkjashdksjh" ,
82
- userAvatarHash : "test user" ,
83
- userName : "sndbhsbgdj" ,
84
- env : env ,
83
+ type : "discord" ,
84
+ token : UNIQUE_TOKEN ,
85
+ attributes : {
86
+ discordId : 1 ,
87
+ userAvatar : "https://cdn.discordapp.com/avatars/1/userAvatarHash.jpg" ,
88
+ userName : "userName" ,
89
+ discriminator : "discriminator" ,
90
+ expiry : mockDateNow + 1000 * 60 * 2 ,
91
+ } ,
85
92
} ;
86
93
94
+ jest . spyOn ( global , "fetch" ) . mockResolvedValueOnce ( {
95
+ ok : true ,
96
+ status : 400 , // ERROR STATUS
97
+ json : jest . fn ( ) . mockResolvedValueOnce ( data ) ,
98
+ } as unknown as Response ) ;
99
+
100
+ const { verifyCommand } = await import (
101
+ "../../../src/controllers/verifyCommand"
102
+ ) ;
87
103
const result = await verifyCommand (
88
104
1233434 ,
89
105
"sjkhdkjashdksjh" ,
@@ -92,21 +108,20 @@ describe("verifyCommand", () => {
92
108
env
93
109
) ;
94
110
95
- const verificationSiteURL = config ( env ) . VERIFICATION_SITE_URL ;
96
- const message =
97
- `${ verificationSiteURL } /discord?token=${ guildEnv . DISCORD_TOKEN } \n` +
98
- response . VERIFICATION_STRING ;
99
-
100
111
expect ( global . fetch ) . toHaveBeenCalledWith (
101
- `https ://api.realdevsquad.com /external-accounts` ,
112
+ `http ://localhost:3000 /external-accounts` ,
102
113
{
103
114
method : "POST" ,
104
115
headers : {
105
116
"Content-Type" : "application/json" ,
106
- Authorization : `Bot ${ guildEnv . DISCORD_TOKEN } ` ,
117
+ Authorization : `Bearer ${ env . DISCORD_TOKEN } ` ,
107
118
} ,
108
119
body : JSON . stringify ( data ) ,
109
120
}
110
121
) ;
122
+ const resultText = await result . text ( ) ;
123
+ const resultData = JSON . parse ( resultText ) ;
124
+
125
+ expect ( resultData . data . content ) . toEqual ( RETRY_COMMAND ) ;
111
126
} ) ;
112
127
} ) ;
0 commit comments