1
1
import { beforeEach , describe , expect , it } from "@jest/globals" ;
2
2
import Models from "../../database/models.js" ;
3
- import { DecisionStatus , DecisionResponse } from "../../database/admission-db.js" ;
4
- import { getAsStaff , getAsUser , putAsStaff , putAsUser , TESTER } from "../../testTools.js" ;
5
- import { AdmissionDecision } from "../../database/admission-db.js" ;
3
+ import { DecisionStatus , DecisionResponse , AdmissionDecision } from "../../database/admission-db.js" ;
4
+ import { getAsStaff , getAsUser , putAsStaff , putAsUser , getAsAttendee , putAsApplicant , TESTER } from "../../testTools.js" ;
6
5
import { StatusCode } from "status-code-enum" ;
7
6
import { ApplicantDecisionFormat } from "./admission-formats.js" ;
8
7
9
- const TESTER_USER = {
8
+ const TESTER_DECISION = {
10
9
userId : TESTER . id ,
11
10
status : DecisionStatus . ACCEPTED ,
12
11
response : DecisionResponse . PENDING ,
13
12
emailSent : false ,
14
13
reviewer : "tester-reviewer" ,
15
14
} satisfies AdmissionDecision ;
16
15
17
- const OTHER_USER = {
16
+ const OTHER_DECISION = {
18
17
userId : "other-user" ,
19
18
status : DecisionStatus . REJECTED ,
20
19
response : DecisionResponse . DECLINED ,
21
20
emailSent : true ,
22
21
reviewer : "other-reviewer" ,
23
22
} satisfies AdmissionDecision ;
24
23
25
- const updateData = [
24
+ const updateRequest = [
26
25
{
27
26
userId : TESTER . id ,
28
27
status : DecisionStatus . WAITLISTED ,
@@ -35,39 +34,136 @@ const updateData = [
35
34
36
35
beforeEach ( async ( ) => {
37
36
Models . initialize ( ) ;
38
- await Models . AdmissionDecision . create ( TESTER_USER ) ;
39
- await Models . AdmissionDecision . create ( OTHER_USER ) ;
37
+ await Models . AdmissionDecision . create ( TESTER_DECISION ) ;
38
+ await Models . AdmissionDecision . create ( OTHER_DECISION ) ;
40
39
} ) ;
41
40
42
- describe ( "GET /admission" , ( ) => {
41
+ describe ( "GET /admission/not-sent/ " , ( ) => {
43
42
it ( "gives forbidden error for user without elevated perms" , async ( ) => {
44
- const responseUser = await getAsUser ( "/admission/" ) . expect ( StatusCode . ClientErrorForbidden ) ;
43
+ const responseUser = await getAsUser ( "/admission/not-sent/ " ) . expect ( StatusCode . ClientErrorForbidden ) ;
45
44
expect ( JSON . parse ( responseUser . text ) ) . toHaveProperty ( "error" , "Forbidden" ) ;
46
45
} ) ;
47
46
it ( "should return a list of applicants without email sent" , async ( ) => {
48
- const response = await getAsStaff ( "/admission/" ) . expect ( StatusCode . SuccessOK ) ;
49
- expect ( JSON . parse ( response . text ) ) . toMatchObject ( expect . arrayContaining ( [ expect . objectContaining ( TESTER_USER ) ] ) ) ;
47
+ const response = await getAsStaff ( "/admission/not-sent/ " ) . expect ( StatusCode . SuccessOK ) ;
48
+ expect ( JSON . parse ( response . text ) ) . toMatchObject ( expect . arrayContaining ( [ expect . objectContaining ( TESTER_DECISION ) ] ) ) ;
50
49
} ) ;
51
50
} ) ;
52
51
53
- describe ( "PUT /admission" , ( ) => {
52
+ describe ( "PUT /admission/ " , ( ) => {
54
53
it ( "gives forbidden error for user without elevated perms" , async ( ) => {
55
- const responseUser = await putAsUser ( "/admission/" ) . send ( updateData ) . expect ( StatusCode . ClientErrorForbidden ) ;
54
+ const responseUser = await putAsUser ( "/admission/" ) . send ( updateRequest ) . expect ( StatusCode . ClientErrorForbidden ) ;
56
55
expect ( JSON . parse ( responseUser . text ) ) . toHaveProperty ( "error" , "Forbidden" ) ;
57
56
} ) ;
58
57
it ( "should update application status of applicants" , async ( ) => {
59
- const response = await putAsStaff ( "/admission/" ) . send ( updateData ) . expect ( StatusCode . SuccessOK ) ;
58
+ const response = await putAsStaff ( "/admission/" ) . send ( updateRequest ) . expect ( StatusCode . SuccessOK ) ;
60
59
expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "message" , "StatusSuccess" ) ;
61
- const ops = updateData . map ( ( entry ) => {
60
+ const ops = updateRequest . map ( ( entry ) => {
62
61
return Models . AdmissionDecision . findOne ( { userId : entry . userId } ) ;
63
62
} ) ;
64
63
const retrievedEntries = await Promise . all ( ops ) ;
65
64
expect ( retrievedEntries ) . toMatchObject (
66
65
expect . arrayContaining (
67
- updateData . map ( ( item ) => {
66
+ updateRequest . map ( ( item ) => {
68
67
return expect . objectContaining ( { status : item . status , userId : item . userId } ) ;
69
68
} ) ,
70
69
) ,
71
70
) ;
72
71
} ) ;
73
72
} ) ;
73
+
74
+ describe ( "GET /admission/rsvp/" , ( ) => {
75
+ it ( "gives a UserNotFound error for an non-existent user" , async ( ) => {
76
+ await Models . AdmissionDecision . deleteOne ( {
77
+ userId : TESTER . id ,
78
+ } ) ;
79
+
80
+ const response = await getAsAttendee ( "/admission/rsvp/" ) . expect ( StatusCode . ClientErrorBadRequest ) ;
81
+
82
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "UserNotFound" ) ;
83
+ } ) ;
84
+
85
+ it ( "works for an attendee user and returns filtered data" , async ( ) => {
86
+ const response = await getAsAttendee ( "/admission/rsvp/" ) . expect ( StatusCode . SuccessOK ) ;
87
+
88
+ expect ( JSON . parse ( response . text ) ) . toMatchObject ( {
89
+ userId : TESTER_DECISION . userId ,
90
+ status : TESTER_DECISION . status ,
91
+ response : TESTER_DECISION . response ,
92
+ } ) ;
93
+ } ) ;
94
+
95
+ it ( "works for a staff user and returns unfiltered data" , async ( ) => {
96
+ const response = await getAsStaff ( "/admission/rsvp/" ) . expect ( StatusCode . SuccessOK ) ;
97
+
98
+ expect ( JSON . parse ( response . text ) ) . toMatchObject ( TESTER_DECISION ) ;
99
+ } ) ;
100
+ } ) ;
101
+
102
+ describe ( "GET /admission/rsvp/:USERID" , ( ) => {
103
+ it ( "returns forbidden error if caller doesn't have elevated perms" , async ( ) => {
104
+ const response = await getAsAttendee ( `/admission/rsvp/${ TESTER . id } ` ) . expect ( StatusCode . ClientErrorForbidden ) ;
105
+
106
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "Forbidden" ) ;
107
+ } ) ;
108
+
109
+ it ( "gets if caller has elevated perms" , async ( ) => {
110
+ const response = await getAsStaff ( `/admission/rsvp/${ TESTER . id } ` ) . expect ( StatusCode . SuccessOK ) ;
111
+
112
+ expect ( JSON . parse ( response . text ) ) . toMatchObject ( TESTER_DECISION ) ;
113
+ } ) ;
114
+
115
+ it ( "returns UserNotFound error if user doesn't exist" , async ( ) => {
116
+ const response = await getAsStaff ( "/admission/rsvp/idontexist" ) . expect ( StatusCode . ClientErrorBadRequest ) ;
117
+
118
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "UserNotFound" ) ;
119
+ } ) ;
120
+ } ) ;
121
+
122
+ describe ( "PUT /admission/rsvp" , ( ) => {
123
+ it ( "error checking for empty query works" , async ( ) => {
124
+ const response = await putAsApplicant ( "/admission/rsvp/" ) . send ( { } ) . expect ( StatusCode . ClientErrorBadRequest ) ;
125
+
126
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidParams" ) ;
127
+ } ) ;
128
+
129
+ it ( "returns UserNotFound for nonexistent user" , async ( ) => {
130
+ await Models . AdmissionDecision . deleteOne ( {
131
+ userId : TESTER . id ,
132
+ } ) ;
133
+ const response = await putAsApplicant ( "/admission/rsvp/" )
134
+ . send ( { isAttending : true } )
135
+ . expect ( StatusCode . ClientErrorBadRequest ) ;
136
+
137
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "UserNotFound" ) ;
138
+ } ) ;
139
+
140
+ it ( "lets applicant accept accepted decision" , async ( ) => {
141
+ await putAsApplicant ( "/admission/rsvp/" ) . send ( { isAttending : true } ) . expect ( StatusCode . SuccessOK ) ;
142
+ const stored = await Models . AdmissionDecision . findOne ( { userId : TESTER . id } ) ;
143
+
144
+ expect ( stored ) . toMatchObject ( {
145
+ ...TESTER_DECISION ,
146
+ response : DecisionResponse . ACCEPTED ,
147
+ } satisfies AdmissionDecision ) ;
148
+ } ) ;
149
+
150
+ it ( "lets applicant reject accepted decision" , async ( ) => {
151
+ await putAsApplicant ( "/admission/rsvp/" ) . send ( { isAttending : false } ) . expect ( StatusCode . SuccessOK ) ;
152
+ const stored = await Models . AdmissionDecision . findOne ( { userId : TESTER . id } ) ;
153
+
154
+ expect ( stored ) . toMatchObject ( {
155
+ ...TESTER_DECISION ,
156
+ response : DecisionResponse . DECLINED ,
157
+ } satisfies AdmissionDecision ) ;
158
+ } ) ;
159
+
160
+ it ( "doesn't let applicant accept rejected decision" , async ( ) => {
161
+ await Models . AdmissionDecision . findOneAndUpdate ( { userId : TESTER . id } , { status : DecisionStatus . REJECTED } ) ;
162
+
163
+ const response = await putAsApplicant ( "/admission/rsvp/" )
164
+ . send ( { isAttending : false } )
165
+ . expect ( StatusCode . ClientErrorForbidden ) ;
166
+
167
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "NotAccepted" ) ;
168
+ } ) ;
169
+ } ) ;
0 commit comments