@@ -155,4 +155,127 @@ defmodule Cadet.AccountsTest do
155155 assert length ( all_stu_in_c1g2 ) == 0
156156 end
157157 end
158+
159+ describe "update_role" do
160+ setup do
161+ c1 = insert ( :course , % { course_name: "c1" } )
162+ c2 = insert ( :course , % { course_name: "c2" } )
163+ admin1 = insert ( :course_registration , % { course: c1 , role: :admin } )
164+ staff1 = insert ( :course_registration , % { course: c1 , role: :staff } )
165+ student1 = insert ( :course_registration , % { course: c1 , role: :student } )
166+ student2 = insert ( :course_registration , % { course: c2 , role: :student } )
167+
168+ { :ok , % { a1: admin1 , s1: student1 , s2: student2 , st1: staff1 } }
169+ end
170+
171+ test "successful when admin is admin of the course the user is in (student)" , % {
172+ a1: admin1 ,
173+ s1: % { id: coursereg_id }
174+ } do
175+ { :ok , updated_coursereg } = Accounts . update_role ( admin1 , "student" , coursereg_id )
176+ assert updated_coursereg . role == :student
177+ end
178+
179+ test "successful when admin is admin of the course the user is in (staff)" , % {
180+ a1: admin1 ,
181+ s1: % { id: coursereg_id }
182+ } do
183+ { :ok , updated_coursereg } = Accounts . update_role ( admin1 , "staff" , coursereg_id )
184+ assert updated_coursereg . role == :staff
185+ end
186+
187+ test "successful when admin is admin of the course the user is in (admin)" , % {
188+ a1: admin1 ,
189+ s1: % { id: coursereg_id }
190+ } do
191+ { :ok , updated_coursereg } = Accounts . update_role ( admin1 , "admin" , coursereg_id )
192+ assert updated_coursereg . role == :admin
193+ end
194+
195+ test "fails when admin tries to downgrade own role" , % { a1: % { id: coursereg_id } = admin1 } do
196+ assert { :error , { :bad_request , "Admin not allowed to downgrade own role" } } ==
197+ Accounts . update_role ( admin1 , "staff" , coursereg_id )
198+ end
199+
200+ test "fails when user course registration does not exist" , % {
201+ a1: admin1 ,
202+ s2: % { id: coursereg_id }
203+ } do
204+ assert { :error , { :bad_request , "User course registration does not exist" } } ==
205+ Accounts . update_role ( admin1 , "staff" , coursereg_id + 1 )
206+ end
207+
208+ test "admin is not admin of the course the user is in" , % { a1: admin1 , s2: % { id: coursereg_id } } do
209+ assert { :error , { :forbidden , "Wrong course" } } ==
210+ Accounts . update_role ( admin1 , "staff" , coursereg_id )
211+ end
212+
213+ test "invalid role provided" , % { a1: admin1 , s1: % { id: coursereg_id } } do
214+ assert { :error , { :bad_request , "role is invalid" } } ==
215+ Accounts . update_role ( admin1 , "invalidrole" , coursereg_id )
216+ end
217+
218+ test "fails when staff makes changes" , % { st1: staff1 , s1: % { id: coursereg_id } } do
219+ assert { :error , { :forbidden , "User is not permitted to change others' roles" } } ==
220+ Accounts . update_role ( staff1 , "staff" , coursereg_id )
221+ end
222+ end
223+
224+ describe "delete_user" do
225+ setup do
226+ c1 = insert ( :course , % { course_name: "c1" } )
227+ c2 = insert ( :course , % { course_name: "c2" } )
228+ admin1 = insert ( :course_registration , % { course: c1 , role: :admin } )
229+ admin2 = insert ( :course_registration , % { course: c1 , role: :admin } )
230+ staff1 = insert ( :course_registration , % { course: c1 , role: :staff } )
231+ student1 = insert ( :course_registration , % { course: c1 , role: :student } )
232+ student2 = insert ( :course_registration , % { course: c2 , role: :student } )
233+
234+ { :ok , % { a1: admin1 , a2: admin2 , s1: student1 , s2: student2 , st1: staff1 } }
235+ end
236+
237+ test "successful when admin is admin of the course the user is in (student)" , % {
238+ a1: admin1 ,
239+ s1: % { id: coursereg_id }
240+ } do
241+ { :ok , deleted_entry } = Accounts . delete_user ( admin1 , coursereg_id )
242+ assert deleted_entry . id == coursereg_id
243+ end
244+
245+ test "successful when admin is admin of the course the user is in (staff)" , % {
246+ a1: admin1 ,
247+ st1: % { id: coursereg_id }
248+ } do
249+ { :ok , deleted_entry } = Accounts . delete_user ( admin1 , coursereg_id )
250+ assert deleted_entry . id == coursereg_id
251+ end
252+
253+ test "fails when staff tries to delete user" , % { st1: staff1 , s1: % { id: coursereg_id } } do
254+ assert { :error , { :forbidden , "User is not permitted to delete other users" } } ==
255+ Accounts . delete_user ( staff1 , coursereg_id )
256+ end
257+
258+ test "fails when deleting own self" , % { a1: % { id: coursereg_id } = admin1 } do
259+ assert { :error , { :bad_request , "Admin not allowed to delete ownself from course" } } ==
260+ Accounts . delete_user ( admin1 , coursereg_id )
261+ end
262+
263+ test "fails when user course registration does not exist" , % {
264+ a1: admin1 ,
265+ s2: % { id: coursereg_id }
266+ } do
267+ assert { :error , { :bad_request , "User course registration does not exist" } } ==
268+ Accounts . delete_user ( admin1 , coursereg_id + 1 )
269+ end
270+
271+ test "fails when deleting an admin" , % { a1: admin1 , a2: % { id: coursereg_id } } do
272+ assert { :error , { :bad_request , "Admins cannot be deleted" } } ==
273+ Accounts . delete_user ( admin1 , coursereg_id )
274+ end
275+
276+ test "fails when deleting a user from another course" , % { a1: admin1 , s2: % { id: coursereg_id } } do
277+ assert { :error , { :forbidden , "Wrong course" } } ==
278+ Accounts . delete_user ( admin1 , coursereg_id )
279+ end
280+ end
158281end
0 commit comments