def add_exercise_user_fail(self): """ Helper function to test adding exercises by users that aren't authorized """ # Add an exercise count_before = Exercise.objects.count() response = self.client.post(reverse('exercise:exercise:add'), {'name_original': random_string(), 'license': 1, 'exercise_base': { 'category': 2, 'muscles': [1, 2]} }) count_after = Exercise.objects.count() self.assertIn(response.status_code, STATUS_CODES_FAIL) # Exercise was not added self.assertEqual(count_before, count_after)
def add_exercise_success(self, admin=False): """ Tests adding/editing an exercise with a user with enough rights to do this """ # Add an exercise count_before = Exercise.objects.count() description = 'a nice, long and accurate description for the exercise' name_original = random_string() response = self.client.post( reverse('exercise:exercise:add'), { 'name_original': name_original, 'license': 1, 'description': description, 'category': 2, 'muscles': [1, 2] }) count_after = Exercise.objects.count() self.assertEqual(response.status_code, 302) new_location = response['Location'] self.assertEqual(count_before + 1, count_after, 'Exercise was not added') response = self.client.get(new_location) exercise_id = response.context['exercise'].id # Exercise was saved exercise = Exercise.objects.get(pk=exercise_id) if admin: self.assertEqual(exercise.license_author, 'testserver') self.assertEqual(exercise.status, Exercise.STATUS_ACCEPTED) else: self.assertEqual(exercise.license_author, 'test') self.assertEqual(exercise.status, Exercise.STATUS_PENDING) response = self.client.get( reverse('exercise:exercise:view', kwargs={'id': exercise_id})) self.assertEqual(response.status_code, 200) # Navigation tab self.assertEqual(response.context['active_tab'], WORKOUT_TAB) exercise_1 = Exercise.objects.get(pk=exercise_id) self.assertEqual(exercise_1.name, name_original) # Wrong category - adding response = self.client.post( reverse('exercise:exercise:add'), { 'category': 111, 'name_original': random_string(), 'license': 1, 'category': 111, 'muscles': [1, 2] }) self.assertTrue(response.context['form'].errors['category']) # Wrong category - editing response = self.client.post( reverse('exercise:exercise:edit', kwargs={'pk': '1'}), { 'category': 111, 'name_original': random_string(), 'license': 1, 'category': 111, 'muscles': [1, 2] }) if admin: self.assertTrue(response.context['form'].errors['category']) else: self.assertIn(response.status_code, STATUS_CODES_FAIL) # No muscles - adding response = self.client.post( reverse('exercise:exercise:add'), { 'category': 1, 'name_original': random_string(), 'license': 1, 'category': 1, 'muscles': [] }) self.assertEqual(response.status_code, 302) # No muscles - editing response = self.client.post( reverse('exercise:exercise:edit', kwargs={'pk': '1'}), { 'category': 1, 'name_original': random_string(), 'license': 1, 'category': 1, 'muscles': [] }) if admin: self.assertEqual(response.status_code, 302) else: self.assertIn(response.status_code, STATUS_CODES_FAIL)