def test_add_admin_conditions(self, from_admin, add_admin, expected, setup_client_auth, prof_w_perm, make_request, role_model_instances, actual_perm_const): d1, s1, c1 = role_model_instances() advisor, client1 = prof_w_perm(c1, 1, perms=['Advisor']) editor, client2 = prof_w_perm(c1, 2, perms=['Editor']) client_map = { 'Advisor': client1, 'Editor': client2, } user_map = { 'Advisor': advisor, 'Editor': editor, } path = reverse(self.add_admin_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) body = {'email': '*****@*****.**', 'permissions': [add_admin]} response: Response = make_request(client_map[from_admin], 'post', data=body, path=path, format='json') assert response.status_code == expected
def test_user_invited_not_exist(self, role_model_instances, prof_w_perm, make_request, actual_perm_const): d1, s1, c1 = role_model_instances() profile, client = prof_w_perm(c1, perms=['Advisor']) path = reverse(self.add_admin_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) body = { 'email': '*****@*****.**', 'permissions': ['Editor'] } response: Response = make_request(client, 'post', data=body, path=path, format='json') assert response.status_code == status.HTTP_202_ACCEPTED assert len(mail.outbox) == 1 assert mail.outbox[0].subject == "You were invited to xroads!" assert mail.outbox[0].from_email == settings.DJANGO_NO_REPLY assert mail.outbox[0].to == [body['email']]
def test_get_questions(self, setup_client_auth, prof_w_perm, make_request, role_model_instances): d1, s1, c1 = role_model_instances() profile, client = prof_w_perm(c1, perms=[]) path = reverse(self.get_questions_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) question1 = Question.objects.create(question='yo yo', asker=profile, club=c1) question2 = Question.objects.create(question='yo yo2', asker=profile, club=c1) response: Response = make_request(client, 'get', path=path, format='json') assert response.status_code == status.HTTP_200_OK assert response.data == GetQuestionSerializer( [question1, question2], many=True).data
def test_leave_club(self, role_model_instances, make_request, setup_client_auth, prof_w_perm, perm_const_override): d1, s1, c1 = role_model_instances() profile, client = setup_client_auth(1) profile.district = d1 profile.school = s1 c1.join(profile) path = reverse(self.club_leave_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) response: Response = make_request(client, 'post', path=path, format='json') assert response.status_code == status.HTTP_202_ACCEPTED profile.refresh_from_db() assert c1 not in list(profile.joined_clubs) club_editor, editor_client = prof_w_perm( c1, 2, perms=['answer-questions'])
def test_answer_question_invalid(self, setup_client_auth, prof_w_perm, make_request, role_model_instances, create_test_prof, perm_const_override): d1, s1, c1 = role_model_instances() profile, client = prof_w_perm(c1, perms=['answer-questions']) prof2 = create_test_prof(2) path = reverse(self.answer_question_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) question = Question.objects.create(question='', asker=prof2, club=c1) data = {'question': 'blah', 'answer': 'This is the answer'} response: Response = make_request(client, 'post', data=data, path=path, format='json') assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_methods_disabled_retrieve(self, method, make_request, prof_w_perm, role_model_instances): d1, s1, c1 = role_model_instances() profile, client = prof_w_perm(d1) path = reverse(self.retrieve_url_name, kwargs={'pk': d1.pk}) response = make_request(client, method, path=path, format='json') assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
def test_methods_disabled_list(self, method, make_request, prof_w_perm, role_model_instances): d1, s1, c1 = role_model_instances() user, client = prof_w_perm(d1) path = reverse(self.list_url_name) response = make_request(client, method, path=path, format='json') assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED
def test_retrieve_data(self, role_model_instances, prof_w_perm, make_request): d1, s1, c1 = role_model_instances() user, client = prof_w_perm(d1) path = reverse(self.retrieve_url_name, kwargs={'pk': d1.pk}) response = make_request(client, 'get', path=path, format='json') expected_data = DistrictSerializer(d1).data assert response.data == expected_data
def test_does_not_exist(self, setup_client_auth, make_request): d1 = District.objects.create(name='d1') user, client = setup_client_auth() path = reverse(self.retrieve_url_name, args={ 'district_pk': d1.id, 'pk': 1 }) response = make_request(client, 'get', path=path, format='json') assert response.status_code == status.HTTP_404_NOT_FOUND
def test_list_data(self, make_request, role_model_instances, prof_w_perm): d1, s1, c1 = role_model_instances() d2, s2, c2 = role_model_instances() user, client = prof_w_perm(d1) path = reverse(self.list_url_name) response = make_request(client, 'get', path=path, format='json') expected_data = DistrictSerializer([d1, d2], many=True).data assert response.data == expected_data
def test_does_not_exist(self, perm_const_override, setup_client_auth, make_request, role_model_instances): d1, s1, c1 = role_model_instances() c1.delete() user, client = setup_client_auth() path = reverse(self.retrieve_url_name, args={ 'district_pk': c1.district.id, 'school_pk': c1.school.pk, 'pk': 1 }) response = make_request(client, 'get', path=path, format='json') assert response.status_code == status.HTTP_404_NOT_FOUND
def test_retrieves_club_list_no_auth(self, role_model_instances, make_request, setup_client_no_auth): d1, s1, c1 = role_model_instances() client = setup_client_no_auth() path = reverse(self.club_path_name, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id }) response: Response = make_request(client, 'get', path=path, format='json') assert response.status_code == status.HTTP_401_UNAUTHORIZED
def test_toggle_hide_w_perm(self, perm_const_override, prof_w_perm, make_request, role_model_instances): d1, s1, c1 = role_model_instances() assert c1.is_visible is False profile, client = prof_w_perm(c1, ['hide-club']) path = reverse(self.toggle_hide_url_name, kwargs={ 'district_pk': c1.district.id, 'school_pk': c1.school.pk, 'pk': c1.pk }) response = make_request(client, 'post', path=path, format='json') assert response.status_code == status.HTTP_202_ACCEPTED c1.refresh_from_db() assert c1.is_visible is True
def test_toggle_hide_without_perm(self, setup_client_auth, prof_w_perm, make_request, role_model_instances): d1, s1, c1 = role_model_instances() assert c1.is_visible is False path = reverse(self.toggle_hide_url_name, kwargs={ 'district_pk': c1.district.id, 'school_pk': c1.school.pk, 'pk': c1.pk }) # User without any permissions prof1, client1 = setup_client_auth() response = make_request(client1, 'post', path=path, format='json') assert response.status_code == status.HTTP_403_FORBIDDEN c1.refresh_from_db() assert c1.is_visible is False
def test_join_school(self, role_model_instances, setup_client_auth, make_request): d1, s1, c1 = role_model_instances() profile, client = setup_client_auth() profile.district = d1 path = reverse(self.join_school_path, kwargs={ 'district_pk': d1.id, 'pk': s1.id }) response: Response = make_request(client, 'post', path=path, format='json') assert response.status_code == status.HTTP_202_ACCEPTED profile.refresh_from_db() assert s1 == profile.school
def test_ask_question_invalid(self, role_model_instances, make_request, setup_client_auth): d1, s1, c1 = role_model_instances() profile, client = setup_client_auth() c1.join(profile) path = reverse(self.ask_question_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) data = {'question': None} response: Response = make_request(client, 'post', data=data, path=path, format='json') assert response.status_code == status.HTTP_400_BAD_REQUEST
def test_answer_question(self, setup_client_auth, prof_w_perm, make_request, role_model_instances, create_test_prof, perm_const_override): d1, s1, c1 = role_model_instances() profile, client = prof_w_perm(c1, perms=['answer-questions']) prof2 = create_test_prof(2) path = reverse(self.answer_question_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) question = Question.objects.create(question='', asker=prof2, club=c1) data = {'question': question.id, 'answer': 'This is the answer'} response: Response = make_request(client, 'post', data=data, path=path, format='json') assert response.status_code == status.HTTP_200_OK question.refresh_from_db() assert question.answer == data['answer'] assert question.id == data['question'] # test that emails are sent to admins assert len(mail.outbox) == 1, "Email not sent" assert mail.outbox[ 0].subject == f'Your question about {c1.name} was answered!' assert mail.outbox[0].from_email == settings.DJANGO_NO_REPLY assert mail.outbox[0].to == [prof2.email ], 'Email not sent to asker'
def test_ask_question(self, role_model_instances, make_request, setup_client_auth, prof_w_perm, perm_const_override): d1, s1, c1 = role_model_instances() profile, client = setup_client_auth(1) c1.join(profile) club_editor, editor_client = prof_w_perm(c1, 2) assert club_editor in list(c1.editors) path = reverse(self.ask_question_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) data = {'question': 'What does this club do?'} response: Response = make_request(client, 'post', data=data, path=path, format='json') assert response.status_code == status.HTTP_201_CREATED question = Question.objects.get(question=data['question']) assert question.asker == profile assert question.question == data['question'] assert question.answer == None assert question.club == c1 # test that emails are sent to admins assert len(mail.outbox) == 1, "Email not sent" assert mail.outbox[ 0].subject == f'Somebody asked a question about {question.club.name}!' assert mail.outbox[0].from_email == settings.DJANGO_NO_REPLY assert mail.outbox[0].to == [club_editor.email ], 'Email not sent to editor'
def test_retrieves_club_list_w_auth(self, role_model_instances, make_request, setup_client_auth, create_club): d1, s1, c1 = role_model_instances() for i in range(c1.id + 1, c1.id + 4): club = create_club(id=i) s1.add_club(club, save=False) s1.make_save(save=True) profile, client = setup_client_auth() path = reverse(self.club_path_name, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id }) response: Response = make_request(client, 'get', path=path, format='json') assert response.status_code == status.HTTP_200_OK assert response.data == BasicClubInfoSerial(s1.clubs, many=True).data
def test_does_not_exist(self, setup_client_auth, make_request): user, client = setup_client_auth() path = reverse(self.retrieve_url_name, args={'pk': 1}) response = make_request(client, 'get', path=path, format='json') assert response.status_code == status.HTTP_404_NOT_FOUND