1
1
import { describe , expect , it } from "@jest/globals" ;
2
2
import Models from "../../database/models" ;
3
3
import { StatusCode } from "status-code-enum" ;
4
- import { OfficeHoursFormat } from "./mentor-formats " ;
5
- import { postAsAdmin , postAsAttendee , getAsAttendee , getAsAdmin , delAsAttendee , delAsAdmin } from "../../common/testTools " ;
4
+ import { postAsAdmin , postAsAttendee , getAsAttendee , delAsAttendee , delAsAdmin , getAsStaff } from "../../common/testTools " ;
5
+ import { MentorOfficeHours } from "./mentor-schemas " ;
6
6
7
7
const TESTER_OFFICE_HOURS_1 = {
8
8
mentorName : "asdf" ,
9
9
mentorId : "io213123012" ,
10
10
attendees : [ ] ,
11
- } satisfies OfficeHoursFormat ;
11
+ } satisfies MentorOfficeHours ;
12
12
13
13
const TESTER_OFFICE_HOURS_2 = {
14
14
mentorName : "3211" ,
15
15
mentorId : "2k2kk3mmn3" ,
16
16
attendees : [ ] ,
17
- } satisfies OfficeHoursFormat ;
17
+ } satisfies MentorOfficeHours ;
18
18
19
19
describe ( "POST /mentor" , ( ) => {
20
20
it ( "gives an invalid perms error for a non-staff user" , async ( ) => {
21
21
const response = await postAsAttendee ( `/mentor/` )
22
22
. send ( { mentorName : "John Sanson" } )
23
23
. expect ( StatusCode . ClientErrorForbidden ) ;
24
24
25
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidPermission" ) ;
26
- } ) ;
27
-
28
- it ( "gives a bad request error for missing fields" , async ( ) => {
29
- const response = await postAsAttendee ( `/mentor/` ) . send ( { } ) . expect ( StatusCode . ClientErrorBadRequest ) ;
30
-
31
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidRequest" ) ;
25
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "Forbidden" ) ;
32
26
} ) ;
33
27
34
28
it ( "works for staff" , async ( ) => {
35
29
const response = await postAsAdmin ( `/mentor/` ) . send ( { mentorName : "John Sanson" } ) . expect ( StatusCode . SuccessCreated ) ;
36
30
37
31
expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "mentorName" , "John Sanson" ) ;
38
- //mentorId will be randomly generated, and attendees will be empty after initialization
39
32
} ) ;
40
33
} ) ;
41
34
@@ -45,46 +38,39 @@ describe("GET /mentor", () => {
45
38
. send ( { mentorName : "John Sanson" } )
46
39
. expect ( StatusCode . ClientErrorForbidden ) ;
47
40
48
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidPermission " ) ;
41
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "Forbidden " ) ;
49
42
} ) ;
50
43
51
44
it ( "works for staff" , async ( ) => {
52
45
await Models . MentorOfficeHours . create ( TESTER_OFFICE_HOURS_1 ) ;
53
46
await Models . MentorOfficeHours . create ( TESTER_OFFICE_HOURS_2 ) ;
54
47
55
- const response = await getAsAdmin ( `/mentor/` ) . expect ( StatusCode . SuccessOK ) ;
56
-
57
- console . log ( response ) ;
58
-
48
+ const response = await getAsStaff ( `/mentor/` ) . expect ( StatusCode . SuccessOK ) ;
59
49
expect ( JSON . parse ( response . text ) ) . toMatchObject ( [ TESTER_OFFICE_HOURS_1 , TESTER_OFFICE_HOURS_2 ] ) ;
60
50
} ) ;
61
51
} ) ;
62
52
63
- describe ( "DELETE /mentor" , ( ) => {
53
+ describe ( "DELETE /mentor/id/ " , ( ) => {
64
54
it ( "gives an invalid perms error for a non-staff user" , async ( ) => {
65
- const response = await delAsAttendee ( `/mentor/` ) . send ( { mentorId : "12312312" } ) . expect ( StatusCode . ClientErrorForbidden ) ;
66
-
67
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidPermission" ) ;
68
- } ) ;
69
-
70
- it ( "gives a bad request error for missing fields" , async ( ) => {
71
- const response = await delAsAdmin ( `/mentor/` ) . send ( { } ) . expect ( StatusCode . ClientErrorBadRequest ) ;
55
+ const response = await delAsAttendee ( `/mentor/${ TESTER_OFFICE_HOURS_1 . mentorId } /` ) . expect (
56
+ StatusCode . ClientErrorForbidden ,
57
+ ) ;
72
58
73
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidRequest " ) ;
59
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "Forbidden " ) ;
74
60
} ) ;
75
61
76
62
it ( "gives a not found error for a nonexistent mentor" , async ( ) => {
77
- const response = await delAsAdmin ( `/mentor/` ) . send ( { mentorId : "dne" } ) . expect ( StatusCode . ClientErrorBadRequest ) ;
63
+ const response = await delAsAdmin ( `/mentor/${ TESTER_OFFICE_HOURS_1 . mentorId } /` ) . expect ( StatusCode . ClientErrorNotFound ) ;
78
64
79
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "MentorNotFound " ) ;
65
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "NotFound " ) ;
80
66
} ) ;
81
67
82
68
it ( "works for staff" , async ( ) => {
83
69
await Models . MentorOfficeHours . create ( TESTER_OFFICE_HOURS_1 ) ;
84
70
85
- await delAsAdmin ( `/mentor/` ) . send ( { mentorId : "io213123012" } ) . expect ( StatusCode . SuccessOK ) ;
71
+ await delAsAdmin ( `/mentor/${ TESTER_OFFICE_HOURS_1 . mentorId } /` ) . expect ( StatusCode . SuccessOK ) ;
86
72
87
- const officeHours : OfficeHoursFormat | null = await Models . MentorOfficeHours . findOne ( { mentorId : "io213123012" } ) ;
73
+ const officeHours = await Models . MentorOfficeHours . findOne ( { mentorId : TESTER_OFFICE_HOURS_1 . mentorId } ) ;
88
74
expect ( officeHours ) . toBeNull ( ) ;
89
75
} ) ;
90
76
} ) ;
@@ -94,33 +80,29 @@ describe("POST /mentor/attendance", () => {
94
80
await Models . MentorOfficeHours . create ( TESTER_OFFICE_HOURS_1 ) ;
95
81
96
82
const response = await postAsAdmin ( `/mentor/attendance` )
97
- . send ( { mentorId : "io213123012" } )
83
+ . send ( { mentorId : TESTER_OFFICE_HOURS_1 . mentorId } )
98
84
. expect ( StatusCode . ClientErrorForbidden ) ;
99
85
100
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidPermission" ) ;
101
- } ) ;
102
-
103
- it ( "gives a bad request error for missing fields" , async ( ) => {
104
- const response = await postAsAttendee ( `/mentor/attendance` ) . send ( { } ) . expect ( StatusCode . ClientErrorBadRequest ) ;
105
-
106
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "InvalidRequest" ) ;
86
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "Forbidden" ) ;
107
87
} ) ;
108
88
109
89
it ( "gives a not found error for a nonexistent mentor" , async ( ) => {
110
90
const response = await postAsAttendee ( `/mentor/attendance` )
111
91
. send ( { mentorId : "dne" } )
112
- . expect ( StatusCode . ClientErrorBadRequest ) ;
92
+ . expect ( StatusCode . ClientErrorNotFound ) ;
113
93
114
- expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "MentorNotFound " ) ;
94
+ expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "NotFound " ) ;
115
95
} ) ;
116
96
117
97
it ( "gives an already checked in error if an attendee tries to check in twice" , async ( ) => {
118
98
await Models . MentorOfficeHours . create ( TESTER_OFFICE_HOURS_1 ) ;
119
99
120
- await postAsAttendee ( `/mentor/attendance` ) . send ( { mentorId : "io213123012" } ) . expect ( StatusCode . SuccessOK ) ;
100
+ await postAsAttendee ( `/mentor/attendance` )
101
+ . send ( { mentorId : TESTER_OFFICE_HOURS_1 . mentorId } )
102
+ . expect ( StatusCode . SuccessOK ) ;
121
103
122
104
const response = await postAsAttendee ( `/mentor/attendance` )
123
- . send ( { mentorId : "io213123012" } )
105
+ . send ( { mentorId : TESTER_OFFICE_HOURS_1 . mentorId } )
124
106
. expect ( StatusCode . ClientErrorBadRequest ) ;
125
107
126
108
expect ( JSON . parse ( response . text ) ) . toHaveProperty ( "error" , "AlreadyCheckedIn" ) ;
@@ -129,9 +111,11 @@ describe("POST /mentor/attendance", () => {
129
111
it ( "works for attendee" , async ( ) => {
130
112
await Models . MentorOfficeHours . create ( TESTER_OFFICE_HOURS_1 ) ;
131
113
132
- await postAsAttendee ( `/mentor/attendance/` ) . send ( { mentorId : "io213123012" } ) . expect ( StatusCode . SuccessOK ) ;
114
+ await postAsAttendee ( `/mentor/attendance/` )
115
+ . send ( { mentorId : TESTER_OFFICE_HOURS_1 . mentorId } )
116
+ . expect ( StatusCode . SuccessOK ) ;
133
117
134
- const officeHours : OfficeHoursFormat | null = await Models . MentorOfficeHours . findOne ( { mentorId : "io213123012" } ) ;
118
+ const officeHours = await Models . MentorOfficeHours . findOne ( { mentorId : TESTER_OFFICE_HOURS_1 . mentorId } ) ;
135
119
136
120
expect ( officeHours ?. attendees ) . toContain ( "bob-the-tester101010101011" ) ;
137
121
} ) ;
0 commit comments