def setUp(self): user = RushUser( email='*****@*****.**', first_name='auth', last_name='auth', is_active=True, username='******' ) user.set_password('password123') user.save() self.client.login(username='******', password='******')
class ToJSONTestCase(TestCase): def setUp(self): self.user = RushUser(email='*****@*****.**', first_name='Test', last_name='Anonymous', is_active=True) self.user.set_password('password123') self.user.save() def test_datetime_encoder(self): encoder = DatetimeEncoder() example_datetime = datetime.datetime(day=1, month=1, year=2000) encoded_datetime = encoder.default(example_datetime) self.assertEqual(encoded_datetime, '01-01-2000 00:00:00') example_date = datetime.date(day=1, month=1, year=2000) encoded_date = encoder.default(example_date) self.assertEqual(encoded_date, '01-01-2000') example_date = 'Wrong format' self.assertRaises(TypeError, encoder.default, example_date) def test_to_json(self): result = to_json(self.user) result_json = json.loads(result) result_json.pop('password') result_json.pop('id') self.assertEqual( result_json, { 'content_type': None, 'email': '*****@*****.**', 'first_name': 'Test', 'groups': [], 'is_active': True, 'is_admin': False, 'is_superuser': False, 'last_login': None, 'last_name': 'Anonymous', 'object_id': None, 'organization_address': '', 'organization_name': '', 'user_permissions': [], 'username': '' })
def test_delete(self): user = RushUser( email='*****@*****.**', first_name='Test', last_name='Anonymous', is_active=False ) user.set_password('password123') user.save() response = self.client.delete( reverse('contest:accounts', kwargs={'user_id': user.id}) ) self.assertFalse(RushUser.objects.filter(pk=user.id).exists()) self.assertEqual(response.status_code, 204) response = self.client.delete( reverse('contest:accounts', kwargs={'user_id': user.id}) ) self.assertEqual(response.status_code, 500)
def test_post(self): user = RushUser( email='*****@*****.**', first_name='Test', last_name='Anonymous', is_active=False ) user.set_password('password123') user.save() response = self.client.post( reverse('contest:accounts', kwargs={'user_id': user.id}) ) user = RushUser.objects.get(pk=user.id) self.assertEqual(response.status_code, 201) self.assertTrue(user.is_active) response = self.client.post( reverse('contest:accounts', kwargs={'user_id': 0}) ) self.assertEqual(response.status_code, 500)
class ContestantListViewTestCase(TestCase): def setUp(self): self.user = RushUser( email='*****@*****.**', username='******', password='******', is_active=True ) self.user.set_password('P@ssw0rd') self.user.save() self.client.login(username='******', password='******') club = Club.objects.create(name='adam', code=12345) self.contest = Contest.objects.create( date=make_aware(datetime(2050, 12, 31)), place='Szkoła', age_min=11, age_max=16, description='Opis', deadline=make_aware(datetime(2048, 11, 20)), content_type=ContentType.objects.get_for_model(club), object_id=club.pk ) self.contestant = Contestant.objects.create( moderator=self.user, first_name='Adam', last_name='Nowak', gender='M', age=14, school='S', styles_distances='100m motyl', contest=self.contest ) def test_get(self): response = self.client.get( reverse( 'contest:contestant-list', kwargs={'contest_id': self.contest.id} ) ) self.assertEqual(response.status_code, 200) self.assertEqual(len(response.context['contestants']), 1)
class EditContestantViewTestCase(TestCase): fixtures = [ 'contests.json', 'clubs.json', 'users.json', ] def setUp(self): self.user = RushUser( email='*****@*****.**', username='******', password='******', is_active=True ) self.user.set_password('R@ootroot') self.user.save() self.contestant = Contestant.objects.create( first_name='Adam', last_name='Nowak', gender='M', age=14, school='P', styles_distances='100m motyl', contest=Contest.objects.first(), moderator=self.user ) self.client.login(username='******', password='******') def test_get(self): response = self.client.get( reverse( 'contest:contestant-edit', kwargs={'contestant_id': self.contestant.id} ), ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['form'].initial['first_name'], 'Adam' ) self.assertEqual( response.context['form'].initial['last_name'], 'Nowak' ) self.assertEqual(response.context['form'].initial['gender'], 'M') self.assertEqual(response.context['form'].initial['school'], 'P') self.assertEqual( response.context['form'].initial['styles_distances'], '100m motyl' ) self.assertEqual(response.context['form'].initial['age'], 14) def test_post(self): response = self.client.post( reverse( 'contest:contestant-edit', kwargs={'contestant_id': self.contestant.id} ), data={ 'first_name': 'Karol', 'last_name': 'Kowalski', 'school': 'P', 'gender': 'F', 'age': 11, } ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['form'].cleaned_data['first_name'], 'Karol' ) self.assertEqual( response.context['form'].cleaned_data['last_name'], 'Kowalski') self.assertEqual( response.context['form'].cleaned_data['school'], 'P' ) self.assertEqual(response.context['form'].cleaned_data['gender'], 'F') self.assertEqual(response.context['form'].cleaned_data['age'], 11)
class ContestantAddViewTestCase(TestCase): fixtures = ['clubs.json'] def setUp(self): self.user = RushUser( email='*****@*****.**', username='******', password='******', is_active=True ) self.user.set_password('P@ssw0rd') self.user.save() self.client.login(username='******', password='******') self.form_data = { 'csrfmiddlewaretoken': 'A33GMETyB7NE1CknWDg2jVuS1Jsm5A9y', 'form-0-age': '11', 'form-0-first_name': 'Jan', 'form-0-gender': 'M', 'form-0-last_name': 'Kowalski', 'form-0-school': 'P', 'form-0-styles_distances': '1000m', 'form-0-organization': self.user.unit, 'form-1-age': '16', 'form-1-first_name': 'Anna', 'form-1-gender': 'F', 'form-1-last_name': 'Nowak', 'form-1-school': 'P', 'form-1-styles_distances': '500m', 'form-1-organization': self.user.unit, 'form-INITIAL_FORMS': '0', 'form-MAX_NUM_FORMS': '1000', 'form-MIN_NUM_FORMS': '0', 'form-TOTAL_FORMS': '2' } self.contest = Contest.objects.create( date=make_aware(datetime(2050, 12, 31)), place='Szkoła', age_min=11, age_max=16, description='Opis', deadline=make_aware(datetime(2048, 11, 20)) ) self.contest_done = Contest.objects.create( date=make_aware(datetime(2008, 12, 31)), place='Szkoła', age_min=11, age_max=16, description='Opis', deadline=make_aware(datetime(2008, 11, 20)) ) self.contest_deadline = Contest.objects.create( date=make_aware(datetime(2050, 12, 31)), place='Szkoła', age_min=11, age_max=16, description='Opis', deadline=make_aware(datetime(2008, 11, 20)) ) def test_get(self): response = self.client.get( reverse( 'contest:contestant-add', kwargs={'contest_id': self.contest.id} ) ) self.assertEqual(response.status_code, 200) self.assertIsInstance( response.context['formset'].forms[0], ContestantForm ) response = self.client.get( reverse('contest:contestant-add', kwargs={'contest_id': 865}), ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['message'], 'Takie zawody nie istnieją.' ) response = self.client.get( reverse( 'contest:contestant-add', kwargs={'contest_id': self.contest_done.id} ) ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['message'], 'Zawody się już skończyły.' ) response = self.client.get( reverse( 'contest:contestant-add', kwargs={'contest_id': self.contest_deadline.id} ) ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['message'], 'Czas na dodawanie zawodników już minął.' ) def test_post_with_success(self): response = self.client.post( reverse( 'contest:contestant-add', kwargs={'contest_id': self.contest.id} ), data=self.form_data ) self.assertEqual(response.status_code, 200) contestants = Contestant.objects.filter(moderator=self.user) self.assertEquals(len(contestants), 2) self.assertEqual(contestants[0].first_name, 'Jan') self.assertEqual(contestants[1].first_name, 'Anna') def test_post_with_validation_error(self): response = self.client.post( reverse('contest:contestant-add', kwargs={'contest_id': 865}), data=self.form_data ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['message'], 'Takie zawody nie istnieją.' ) self.form_data['form-0-age'] = 99 response = self.client.post( reverse( 'contest:contestant-add', kwargs={'contest_id': self.contest.id} ), data=self.form_data ) expected_error = { 'age': ['Zawodnik nie mieści się w wymaganym przedziale wiekowym.'] } self.assertEqual( response.context['formset'].errors, [expected_error, {}] ) self.form_data['form-0-age'] = 15 self.form_data['form-0-gender'] = 'WRONG' response = self.client.post( reverse( 'contest:contestant-add', kwargs={'contest_id': self.contest.id} ), data=self.form_data ) expected_error = { 'gender': [( 'Wybierz poprawną wartość. WRONG nie jest jednym z ' 'dostępnych wyborów.' )] } self.assertEqual(200, response.status_code) self.assertFalse(response.context['formset'].is_valid()) self.assertEqual( response.context['formset'].errors, [expected_error, {}] ) self.assertFalse( Contestant.objects.filter(moderator=self.user).exists() )
class SetResetPasswordViewTestCase(TestCase): def setUp(self): self.user_1 = RushUser( email='*****@*****.**', first_name='Łukasz', last_name='Ślązak', is_active=True ) self.user_1.save() self.user_2 = RushUser.objects.create( email='*****@*****.**', first_name='Ewa', last_name='Olczak', username='******' ) self.user_2.is_active = True self.user_2.set_password('Password_already_set') self.user_2.save() self.user1_uid = urlsafe_base64_encode(force_bytes(self.user_1.pk)) self.user1_token = default_token_generator.make_token(self.user_1) def test_correct_url(self): response = self.client.get( reverse( 'contest:set-password', kwargs={'uidb64': self.user1_uid, 'token': self.user1_token} ) ) self.assertEqual(response.status_code, 200) self.assertTrue( isinstance(response.context['form'], RushSetPasswordForm) ) def test_resend_on_wrong_token(self): wrong_token = default_token_generator.make_token(self.user_2) response = self.client.get( reverse( 'contest:set-password', kwargs={'uidb64': self.user1_uid, 'token': wrong_token} ) ) self.assertEqual(response.status_code, 200) def test_resend_on_wrong_uid(self): wrong_uid = urlsafe_base64_encode(force_bytes(self.user_2.pk)) response = self.client.get( reverse( 'contest:set-password', kwargs={'uidb64': wrong_uid, 'token': self.user1_token} ) ) self.assertEqual(response.status_code, 200) self.assertFalse(SetResetPasswordView._get_user('00')) response = self.client.get( reverse( 'contest:set-password', kwargs={'uidb64': 'XX', 'token': self.user1_token} ) ) self.assertEqual( response.context['message'], 'Użytkownik nie istnieje.' ) response = self.client.get( reverse( 'contest:set-password', kwargs={'uidb64': 'XX', 'token': self.user1_token} ) ) self.assertEqual( response.context['message'], 'Użytkownik nie istnieje.' ) response = self.client.get( reverse( 'contest:set-password', kwargs={'uidb64': 'XX', 'token': self.user1_token} ) ) self.assertEqual( response.context['message'], 'Użytkownik nie istnieje.' ) def test_password_already_set(self): self.client.login( username='******', password='******' ) response = self.client.get( reverse( 'contest:set-password', kwargs={'uidb64': self.user1_uid, 'token': self.user1_token} ) ) self.assertFalse(isinstance(response.context['user'], RushUser)) def test_setting_password(self): form_data = { 'new_password1': 'pass1234', 'new_password2': 'sad_panda', 'username': '******' } response = self.client.post( reverse( 'contest:set-password', kwargs={'uidb64': self.user1_uid, 'token': self.user1_token} ), data=form_data, ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['form'].errors, { 'username': ['Podana nazwa użytkownika jest już zajęta.'], 'new_password2': ['Hasła nie są identyczne.'] } ) form_data = { 'new_password1': 'pass1234', 'new_password2': 'pass1234', 'username': '******' } response = self.client.post( reverse( 'contest:set-password', kwargs={'uidb64': self.user1_uid, 'token': self.user1_token} ), data=form_data, ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['message'], 'Hasło ustawione, można się zalogować.' ) def test_sending_mail(self): RegisterView.send_email_with_new_user( 'Janek', 'Kowalski', ['*****@*****.**'], 'www.rush.pl' ) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, ['*****@*****.**']) def test_reset_password_successful_get(self): user_2_uid = urlsafe_base64_encode(force_bytes(self.user_2.pk)) user_2_token = default_token_generator.make_token(self.user_2) response = self.client.get( reverse( 'contest:reset-password', kwargs={'uidb64': user_2_uid, 'token': user_2_token} ) ) self.assertEqual(response.status_code, 200) self.assertIsInstance(response.context['form'], RushResetPasswordForm) def test_reset_password_expired_token_get(self): user_2_uid = urlsafe_base64_encode(force_bytes(self.user_2.pk)) with patch('django.contrib.auth.tokens.date') as mock_date: mock_date.today.return_value = date(2010, 1, 1) mock_date.side_effect = lambda *args, **kw: date(*args, **kw) user_2_token = default_token_generator.make_token(self.user_2) response = self.client.get( reverse( 'contest:reset-password', kwargs={'uidb64': user_2_uid, 'token': user_2_token} ) ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['message'], 'Link nie jest już aktywny. Jeśli masz problemy z zalogowaniem, ' 'skorzystaj z formularza resetowania hasła.' ) def test_reset_password_post(self): user_2_uid = urlsafe_base64_encode(force_bytes(self.user_2.pk)) user_2_token = default_token_generator.make_token(self.user_2) form_data = { 'new_password1': 'pass1234', 'new_password2': 'pass1234' } response = self.client.post( reverse( 'contest:reset-password', kwargs={'uidb64': user_2_uid, 'token': user_2_token} ), data=form_data, ) self.assertEqual(response.status_code, 200) self.assertEqual( response.context['message'], 'Hasło ustawione, można się zalogować.' )