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() )