@@ -5,6 +5,7 @@ import { expect, test } from "@jest/globals";
5
5
import { dbMock } from "@/test/dbMock" ;
6
6
import { authMock } from "@/test/authMock" ;
7
7
import { UserType } from "@prisma/client" ;
8
+ import { PrismaClientKnownRequestError } from "@prisma/client/runtime/library" ;
8
9
9
10
test ( "returns 401 on unauthenticated requests" , async ( ) => {
10
11
await testApiHandler ( {
@@ -74,3 +75,118 @@ test("returns 200 and user list on succesful request", async () => {
74
75
} ,
75
76
} ) ;
76
77
} ) ;
78
+
79
+ test ( "bad form data" , async ( ) => {
80
+ await testApiHandler ( {
81
+ appHandler,
82
+ async test ( { fetch } ) {
83
+ const badFormData = new FormData ( ) ;
84
+ badFormData . append ( 'inviteTokenBad' , 'test_token' ) ;
85
+ badFormData . append ( 'passwordBad' , 'test_password' ) ;
86
+
87
+ const res = await fetch ( { method : "POST" , body : badFormData } ) ;
88
+ await expect ( res . status ) . toEqual ( 400 ) ;
89
+ } ,
90
+ } ) ;
91
+ } ) ;
92
+
93
+ const getGoodFormData = ( ) => {
94
+ const goodFormData = new FormData ( ) ;
95
+ goodFormData . append ( 'inviteToken' , 'test_token' ) ;
96
+ goodFormData . append ( 'password' , 'test_password' ) ;
97
+ return goodFormData ;
98
+ }
99
+
100
+ test ( "missing user invite" , async ( ) => {
101
+ await testApiHandler ( {
102
+ appHandler,
103
+ async test ( { fetch } ) {
104
+ dbMock . userInvite . findUnique . mockResolvedValue ( null ) ;
105
+
106
+ const res = await fetch ( { method : "POST" , body : getGoodFormData ( ) } ) ;
107
+ await expect ( res . status ) . toEqual ( 404 ) ;
108
+ } ,
109
+ } ) ;
110
+ } ) ;
111
+
112
+ test ( "expired user invite" , async ( ) => {
113
+ await testApiHandler ( {
114
+ appHandler,
115
+ async test ( { fetch } ) {
116
+ const yearAgo = new Date ( ) ;
117
+ yearAgo . setFullYear ( new Date ( ) . getFullYear ( ) - 1 ) ;
118
+
119
+ dbMock . userInvite . findUnique . mockResolvedValue ( {
120
+ token : 'test_token' ,
121
+ id : 0 ,
122
+ userType : UserType . SUPER_ADMIN ,
123
+ email : "test_email@test.com" ,
124
+ expiration : yearAgo ,
125
+ partnerDetails : null ,
126
+ name : ""
127
+ } ) ;
128
+
129
+ const res = await fetch ( { method : "POST" , body : getGoodFormData ( ) } ) ;
130
+ await expect ( res . status ) . toEqual ( 400 ) ;
131
+ } ,
132
+ } ) ;
133
+ } ) ;
134
+
135
+
136
+ test ( "user already exists" , async ( ) => {
137
+ await testApiHandler ( {
138
+ appHandler,
139
+ async test ( { fetch } ) {
140
+ const yearLater = new Date ( ) ;
141
+ yearLater . setFullYear ( new Date ( ) . getFullYear ( ) + 1 ) ;
142
+
143
+ dbMock . userInvite . findUnique . mockResolvedValue ( {
144
+ token : 'test_token' ,
145
+ id : 0 ,
146
+ userType : UserType . SUPER_ADMIN ,
147
+ email : "test_email@test.com" ,
148
+ expiration : yearLater ,
149
+ partnerDetails : null ,
150
+ name : ""
151
+ } ) ;
152
+
153
+ dbMock . user . create . mockImplementation ( ( ) => {
154
+ throw new PrismaClientKnownRequestError (
155
+ 'violates uniqueness constraint' ,
156
+ {
157
+ code : 'P2002' ,
158
+ clientVersion : 'mock' ,
159
+ meta : { } ,
160
+ batchRequestIdx : 1
161
+ }
162
+ ) ;
163
+ } ) ;
164
+
165
+ const res = await fetch ( { method : "POST" , body : getGoodFormData ( ) } ) ;
166
+ await expect ( res . status ) . toEqual ( 409 ) ;
167
+ } ,
168
+ } ) ;
169
+ } ) ;
170
+
171
+ test ( "successful create" , async ( ) => {
172
+ await testApiHandler ( {
173
+ appHandler,
174
+ async test ( { fetch } ) {
175
+ const yearLater = new Date ( ) ;
176
+ yearLater . setFullYear ( new Date ( ) . getFullYear ( ) + 1 ) ;
177
+
178
+ dbMock . userInvite . findUnique . mockResolvedValue ( {
179
+ token : 'test_token' ,
180
+ id : 0 ,
181
+ userType : UserType . SUPER_ADMIN ,
182
+ email : "test_email@test.com" ,
183
+ expiration : yearLater ,
184
+ partnerDetails : null ,
185
+ name : ""
186
+ } ) ;
187
+
188
+ const res = await fetch ( { method : "POST" , body : getGoodFormData ( ) } ) ;
189
+ await expect ( res . status ) . toEqual ( 200 ) ;
190
+ } ,
191
+ } ) ;
192
+ } ) ;
0 commit comments