1
1
import { beforeEach , describe , expect , it } from "@jest/globals" ;
2
2
import { StatusCode } from "status-code-enum" ;
3
3
import Config from "../../common/config" ;
4
- import { AttendeeProfile } from "../../database/attendee-db " ;
4
+ import { AttendeeProfile , AttendeeProfileCreateRequest } from "./profile-schemas " ;
5
5
import Models from "../../database/models" ;
6
- import { TESTER , delAsUser , getAsAdmin , getAsUser , postAsAttendee , postAsStaff , postAsUser } from "../../common/testTools" ;
6
+ import { TESTER , getAsAdmin , getAsAttendee , getAsUser , postAsAttendee , postAsStaff , postAsUser } from "../../common/testTools" ;
7
+ import { Degree , Gender , HackInterest , HackOutreach , Race , RegistrationApplication } from "../registration/registration-schemas" ;
7
8
8
9
const TESTER_USER = {
9
10
userId : TESTER . id ,
@@ -35,112 +36,137 @@ const TESTER_USER_3 = {
35
36
foodWave : 2 ,
36
37
} satisfies AttendeeProfile ;
37
38
38
- const profile : AttendeeProfile = {
39
- userId : TESTER . id ,
39
+ const CREATE_REQUEST = {
40
+ avatarId : TESTER . avatarId ,
40
41
displayName : TESTER . name ,
41
- avatarUrl : TESTER . avatarUrl ,
42
42
discordTag : TESTER . discordTag ,
43
+ } satisfies AttendeeProfileCreateRequest ;
44
+
45
+ const PROFILE = {
46
+ userId : TESTER . id ,
47
+ displayName : CREATE_REQUEST . displayName ,
48
+ avatarUrl : TESTER . avatarUrl ,
49
+ discordTag : CREATE_REQUEST . discordTag ,
43
50
points : 0 ,
44
51
coins : 0 ,
45
52
foodWave : 1 ,
46
- } ;
53
+ } satisfies AttendeeProfile ;
54
+
55
+ const REGISTRATION = {
56
+ userId : TESTER . id ,
57
+ hasSubmitted : true ,
58
+ isProApplicant : false ,
59
+ preferredName : TESTER . name ,
60
+ legalName : TESTER . name ,
61
+ emailAddress : TESTER . email ,
62
+ university : "ap" ,
63
+ hackEssay1 : "ap" ,
64
+ hackEssay2 : "ap" ,
65
+ optionalEssay : "ap" ,
66
+ location : "ap" ,
67
+ gender : Gender . OTHER ,
68
+ degree : Degree . BACHELORS ,
69
+ major : "CS" ,
70
+ gradYear : 0 ,
71
+ requestedTravelReimbursement : false ,
72
+ dietaryRestrictions : [ "some restriction" ] ,
73
+ race : [ Race . NO_ANSWER ] ,
74
+ hackInterest : [ HackInterest . TECHNICAL_WORKSHOPS ] ,
75
+ hackOutreach : [ HackOutreach . INSTAGRAM ] ,
76
+ } satisfies RegistrationApplication ;
47
77
48
78
beforeEach ( async ( ) => {
49
79
await Models . AttendeeProfile . create ( TESTER_USER ) ;
50
80
await Models . AttendeeProfile . create ( TESTER_USER_2 ) ;
51
81
await Models . AttendeeProfile . create ( TESTER_USER_3 ) ;
82
+ await Models . RegistrationApplication . create ( REGISTRATION ) ;
52
83
} ) ;
53
84
54
85
describe ( "POST /profile" , ( ) => {
55
86
it ( "works for an attendee" , async ( ) => {
56
87
await Models . AttendeeProfile . deleteOne ( { userId : TESTER_USER . userId } ) ;
57
- const response = await postAsAttendee ( "/profile/" ) . send ( profile ) . expect ( StatusCode . SuccessOK ) ;
88
+ const response = await postAsAttendee ( "/profile/" ) . send ( CREATE_REQUEST ) . expect ( StatusCode . SuccessOK ) ;
58
89
59
90
expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "displayName" , TESTER . name ) ;
91
+
92
+ const stored = await Models . AttendeeProfile . findOne ( { userId : TESTER_USER . userId } ) ;
93
+ expect ( stored ?. toObject ( ) ) . toMatchObject ( PROFILE ) ;
60
94
} ) ;
61
95
62
96
it ( "fails when a profile is already created" , async ( ) => {
63
97
await Models . AttendeeProfile . deleteOne ( { userId : TESTER_USER . userId } ) ;
64
- const response = await postAsUser ( "/profile/" ) . send ( profile ) . expect ( StatusCode . SuccessOK ) ;
98
+ const response = await postAsAttendee ( "/profile/" ) . send ( CREATE_REQUEST ) . expect ( StatusCode . SuccessOK ) ;
65
99
66
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "displayName" , TESTER . name ) ;
100
+ expect ( JSON . parse ( response . text ) ) . toMatchObject ( PROFILE ) ;
101
+
102
+ const stored = await Models . AttendeeProfile . findOne ( { userId : TESTER_USER . userId } ) ;
103
+ expect ( stored ?. toObject ( ) ) . toMatchObject ( PROFILE ) ;
67
104
68
105
// to verify they can't double create
69
- const response2 = await postAsUser ( "/profile/" ) . send ( profile ) . expect ( StatusCode . ClientErrorBadRequest ) ;
70
- expect ( JSON . parse ( response2 . text ) ) . toHaveProperty ( "error" , "UserAlreadyExists " ) ;
106
+ const response2 = await postAsAttendee ( "/profile/" ) . send ( CREATE_REQUEST ) . expect ( StatusCode . ClientErrorBadRequest ) ;
107
+ expect ( JSON . parse ( response2 . text ) ) . toHaveProperty ( "error" , "AlreadyExists " ) ;
71
108
} ) ;
72
109
73
110
it ( "fails when invalid data is provided" , async ( ) => {
74
- const response = await postAsUser ( "/profile/" )
111
+ const response = await postAsAttendee ( "/profile/" )
75
112
. send ( {
76
113
displayName : 123 ,
77
114
avatarId : 1 ,
78
115
discordTag : "test" ,
79
116
} )
80
117
. expect ( StatusCode . ClientErrorBadRequest ) ;
81
118
82
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidParams " ) ;
119
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "BadRequest " ) ;
83
120
} ) ;
84
121
} ) ;
85
122
86
123
describe ( "GET /profile" , ( ) => {
87
124
it ( "fails to get a profile that doesn't exist" , async ( ) => {
88
125
await Models . AttendeeProfile . deleteOne ( { userId : TESTER_USER . userId } ) ;
89
126
90
- const response = await getAsUser ( "/profile/" ) . expect ( StatusCode . ClientErrorNotFound ) ;
127
+ const response = await getAsAttendee ( "/profile/" ) . expect ( StatusCode . ClientErrorNotFound ) ;
91
128
92
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "UserNotFound " ) ;
129
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "NotFound " ) ;
93
130
} ) ;
94
131
95
132
it ( "gets a profile that exists" , async ( ) => {
96
- const response = await getAsUser ( "/profile/" ) . expect ( StatusCode . SuccessOK ) ;
97
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "displayName" , TESTER . name ) ;
133
+ const response = await getAsAttendee ( "/profile/" ) . expect ( StatusCode . SuccessOK ) ;
134
+ expect ( JSON . parse ( response . text ) ) . toMatchObject ( PROFILE ) ;
98
135
} ) ;
99
136
} ) ;
100
137
101
- describe ( "GET /profile/userid/:USERID " , ( ) => {
102
- it ( "fails to get a profile as a user " , async ( ) => {
103
- const response = await getAsUser ( " /profile/userid/" + TESTER . id ) . expect ( StatusCode . ClientErrorForbidden ) ;
138
+ describe ( "GET /profile/:id/ " , ( ) => {
139
+ it ( "fails to get a profile as a attendee " , async ( ) => {
140
+ const response = await getAsAttendee ( ` /profile/${ TESTER . id } ` ) . expect ( StatusCode . ClientErrorForbidden ) ;
104
141
105
142
expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "Forbidden" ) ;
106
143
} ) ;
107
144
108
145
it ( "gets a profile as an admin" , async ( ) => {
109
- const response = await getAsAdmin ( " /profile/userid/" + TESTER . id ) . expect ( StatusCode . SuccessOK ) ;
146
+ const response = await getAsAdmin ( ` /profile/${ TESTER . id } ` ) . expect ( StatusCode . SuccessOK ) ;
110
147
111
148
expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "displayName" , TESTER . name ) ;
112
149
} ) ;
113
150
114
151
it ( "gets a user that doesnt exist" , async ( ) => {
115
- const response = await getAsAdmin ( "/profile/userid/doesnotexist" ) . expect ( StatusCode . ClientErrorNotFound ) ;
116
-
117
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "UserNotFound" ) ;
118
- } ) ;
119
- } ) ;
120
-
121
- describe ( "DELETE /profile/" , ( ) => {
122
- it ( "fails to delete a profile that doesn't exist" , async ( ) => {
123
- await Models . AttendeeProfile . deleteOne ( { userId : TESTER_USER . userId } ) ;
124
- const response = await delAsUser ( "/profile" ) . expect ( StatusCode . ClientErrorNotFound ) ;
125
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "AttendeeNotFound" ) ;
126
- } ) ;
152
+ const response = await getAsAdmin ( "/profile/doesnotexist" ) . expect ( StatusCode . ClientErrorNotFound ) ;
127
153
128
- it ( "deletes a profile" , async ( ) => {
129
- const response = await delAsUser ( "/profile" ) . expect ( StatusCode . SuccessOK ) ;
130
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "success" , true ) ;
154
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "NotFound" ) ;
131
155
} ) ;
132
156
} ) ;
133
157
134
158
describe ( "GET /profile/leaderboard" , ( ) => {
135
159
it ( "gets 3 entries when no limit is set" , async ( ) => {
136
- await getAsUser ( "/profile/leaderboard" ) . expect ( StatusCode . SuccessOK ) ;
160
+ const response = await getAsUser ( "/profile/leaderboard" ) . expect ( StatusCode . SuccessOK ) ;
161
+ const parsed = JSON . parse ( response . text ) ;
162
+ expect ( parsed . profiles . length ) . toBe ( 3 ) ;
137
163
} ) ;
138
164
139
165
it ( "gets with a limit of 2" , async ( ) => {
140
166
const response = await getAsUser ( "/profile/leaderboard?limit=2" ) . expect ( StatusCode . SuccessOK ) ;
141
167
142
- const responseArray = JSON . parse ( response . text ) ;
143
- expect ( responseArray . profiles . length ) . toBeLessThan ( 3 ) ;
168
+ const parsed = JSON . parse ( response . text ) ;
169
+ expect ( parsed . profiles . length ) . toBe ( 2 ) ;
144
170
} ) ;
145
171
146
172
it ( "only gets the max limit when no limit is set" , async ( ) => {
@@ -159,13 +185,13 @@ describe("GET /profile/leaderboard", () => {
159
185
const response = await getAsUser ( "/profile/leaderboard" ) . expect ( StatusCode . SuccessOK ) ;
160
186
const responseArray = JSON . parse ( response . text ) ;
161
187
162
- expect ( responseArray . profiles . length ) . toBeLessThan ( Config . LEADERBOARD_QUERY_LIMIT + 1 ) ;
188
+ expect ( responseArray . profiles . length ) . toBe ( Config . LEADERBOARD_QUERY_LIMIT ) ;
163
189
} ) ;
164
190
165
191
it ( "fails when an invalid limit is set" , async ( ) => {
166
192
const response = await getAsUser ( "/profile/leaderboard?limit=0" ) . expect ( StatusCode . ClientErrorBadRequest ) ;
167
193
168
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidLimit " ) ;
194
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "BadRequest " ) ;
169
195
} ) ;
170
196
} ) ;
171
197
@@ -192,14 +218,14 @@ describe("GET /profile/addpoints", () => {
192
218
expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "Forbidden" ) ;
193
219
} ) ;
194
220
195
- it ( "returns UserNotFound for nonexistent users" , async ( ) => {
221
+ it ( "returns NotFound for nonexistent users" , async ( ) => {
196
222
const response = await postAsStaff ( "/profile/addpoints" )
197
223
. send ( {
198
224
userId : "idontexists" ,
199
225
points : 10 ,
200
226
} )
201
- . expect ( StatusCode . ClientErrorBadRequest ) ;
227
+ . expect ( StatusCode . ClientErrorNotFound ) ;
202
228
203
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "UserNotFound " ) ;
229
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "NotFound " ) ;
204
230
} ) ;
205
231
} ) ;
0 commit comments