@@ -322,3 +322,137 @@ def test_team_member_add_api_view__when_adding_a_member__fails_because_user_is_n
322322 .count ()
323323 == 0
324324 )
325+
326+
327+ @pytest .mark .django_db
328+ def test_team_update_succeeds (
329+ api_client : APIClient ,
330+ user : UserType ,
331+ team : Team ,
332+ ):
333+ TeamMemberFactory (team = team , user = user , role = "owner" )
334+ api_client .force_authenticate (user )
335+
336+ new_donation_link = "https://example.com"
337+
338+ response = api_client .patch (
339+ f"/api/cyberstorm/team/{ team .name } /update/" ,
340+ json .dumps ({"donation_link" : new_donation_link }),
341+ content_type = "application/json" ,
342+ )
343+
344+ expected_response = {"donation_link" : new_donation_link }
345+ assert response .status_code == 200
346+
347+ assert response .json () == expected_response
348+ assert Team .objects .get (pk = team .pk ).donation_link == new_donation_link
349+
350+
351+ @pytest .mark .django_db
352+ def test_team_update_fails_user_not_authenticated (
353+ api_client : APIClient ,
354+ team : Team ,
355+ ):
356+ new_donation_link = "https://example.com"
357+
358+ response = api_client .patch (
359+ f"/api/cyberstorm/team/{ team .name } /update/" ,
360+ json .dumps ({"donation_link" : new_donation_link }),
361+ content_type = "application/json" ,
362+ )
363+
364+ expected_response = {"detail" : "Authentication credentials were not provided." }
365+
366+ assert response .status_code == 401
367+ assert response .json () == expected_response
368+ assert Team .objects .get (pk = team .pk ).donation_link is None
369+
370+
371+ @pytest .mark .django_db
372+ def test_team_update_fails_validation (
373+ api_client : APIClient ,
374+ user : UserType ,
375+ team : Team ,
376+ ):
377+ TeamMemberFactory (team = team , user = user , role = "owner" )
378+ api_client .force_authenticate (user )
379+
380+ new_bad_donation_link = "example.com"
381+
382+ response = api_client .patch (
383+ f"/api/cyberstorm/team/{ team .name } /update/" ,
384+ json .dumps ({"donation_link" : new_bad_donation_link }),
385+ content_type = "application/json" ,
386+ )
387+
388+ expected_response = {"donation_link" : ["Enter a valid URL." ]}
389+
390+ assert response .status_code == 400
391+ assert response .json () == expected_response
392+
393+
394+ @pytest .mark .django_db
395+ def test_team_update_fail_user_not_owner (
396+ api_client : APIClient ,
397+ user : UserType ,
398+ team : Team ,
399+ ):
400+ TeamMemberFactory (team = team , user = user , role = "member" )
401+ api_client .force_authenticate (user )
402+
403+ new_donation_link = "https://example.com"
404+
405+ response = api_client .patch (
406+ f"/api/cyberstorm/team/{ team .name } /update/" ,
407+ json .dumps ({"donation_link" : new_donation_link }),
408+ content_type = "application/json" ,
409+ )
410+
411+ expected_response = {"non_field_errors" : ["Must be an owner to edit team info" ]}
412+
413+ assert response .status_code == 403
414+ assert response .json () == expected_response
415+ assert Team .objects .get (pk = team .pk ).donation_link is None
416+
417+
418+ @pytest .mark .django_db
419+ def test_team_update_fail_team_does_not_exist (
420+ api_client : APIClient ,
421+ user : UserType ,
422+ ):
423+ api_client .force_authenticate (user )
424+
425+ new_donation_link = "https://example.com"
426+
427+ response = api_client .patch (
428+ "/api/cyberstorm/team/FakeTeam/update/" ,
429+ json .dumps ({"donation_link" : new_donation_link }),
430+ content_type = "application/json" ,
431+ )
432+
433+ expected_response = {"detail" : "Not found." }
434+
435+ assert response .status_code == 404
436+ assert response .json () == expected_response
437+
438+
439+ @pytest .mark .django_db
440+ def test_team_update_fail_user_not_team_member (
441+ api_client : APIClient ,
442+ user : UserType ,
443+ team : Team ,
444+ ):
445+ api_client .force_authenticate (user )
446+
447+ new_donation_link = "https://example.com"
448+
449+ response = api_client .patch (
450+ f"/api/cyberstorm/team/{ team .name } /update/" ,
451+ json .dumps ({"donation_link" : new_donation_link }),
452+ content_type = "application/json" ,
453+ )
454+
455+ expected_response = {"non_field_errors" : ["Must be a member to access team" ]}
456+
457+ assert response .status_code == 403
458+ assert response .json () == expected_response
0 commit comments