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_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_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_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_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_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_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_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_set_slides(self, role_model_instances, prof_w_perm, make_request, actual_perm_const, temp_img, create_test_slide): d1, s1, c1 = role_model_instances() profile, client = prof_w_perm(c1, perms=['Advisor']) profile.school = s1 path = reverse(self.set_slide_path, kwargs={ 'district_pk': d1.id, 'school_pk': s1.id, 'pk': c1.id }) # create the slides slides = [] for i in range(4): template, params = create_test_slide() slide = c1.add_slide(template.temp_id, **params) slide.video_url = "https://www.youtube.com" slide.save() slides.append(slide) input_slide_data = SlideSerializer(slides, many=True).data data = [] for item in input_slide_data: temp_file = tempfile.NamedTemporaryFile(suffix=".jpg") test_image = temp_img(temp_file) item['img'] = str( base64.b64encode( SimpleUploadedFile(name=test_image.name, content=open(test_image.name, 'rb').read(), content_type='image/jpeg').read()), "utf-8") data.append(dict(item)) body = data response = client.post(path, body, format="json") assert c1.slides.count() == len(slides) assert list(c1.slides) != slides assert response.status_code == status.HTTP_201_CREATED
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_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'