def users(): users = { 'anonymous': AnonymousUser(), 'authenticated': UserFactory.build(), 'staff': UserFactory.build(is_staff=True) } users['author'] = users['authenticated'] return users
def user(db): """Add a test user to the database.""" user_ = UserFactory.create( name=TESTUSER, email=TESTEMAIL, password=make_password(TESTPASSWORD), ) return user_
def test_post_to_update_view_preserves_the_original_author( self, db, rf, mocker): mocker.patch('um.exercises.views.Exercise.render_html') mocker.patch('um.exercises.views.Exercise.render_tex') # GIVEN an existing exercise user = UserFactory.create() ex = factories.ExerciseFactory.create() original_author = ex.author assert user != original_author # WHEN making a post request to the create view url = reverse('exercises:update', kwargs={'pk': ex.id}) request = rf.post(url, data=exercise_data) request.user = UserFactory.create() views.ExerciseUpdateView.as_view()(request, pk=ex.id) # THEN the author is preserved updated_ex = models.Exercise.objects.get(id=ex.id) assert updated_ex.author == original_author
def test_post_to_create_view_adds_author_to_object(self, db, rf, users, mocker): # GIVEN an existing exercise AND a user ex = ExerciseFactory.create() user = UserFactory.create() # WHEN making a post request to the create view url = reverse('sheets:create') request = rf.post(url, data={'exercises': ex.id}) request.user = user views.SheetCreateView.as_view()(request) # THEN the user gets attached to the sheet as the author sheet = models.Sheet.objects.last() assert sheet.author == user
def test_post_to_create_view_redirects_to_detail_view( self, db, rf, users, mocker): # GIVEN an existing exercise AND a user ex = ExerciseFactory.create() user = UserFactory.create() # WHEN making a post request to the create view url = reverse('sheets:create') request = rf.post(url, data={'exercises': ex.id}) request.user = user response = views.SheetCreateView.as_view()(request, url) # THEN it redirects to the detail view sheet = models.Sheet.objects.last() assert response.status_code == 302 assert response.url == sheet.url
def test_post_to_update_view_redirects_to_home_page(self, db, rf, mocker): mocker.patch('um.exercises.views.Exercise.render_html') mocker.patch('um.exercises.views.Exercise.render_tex') # GIVEN an existing exercise user = UserFactory.create() ex = factories.ExerciseFactory.create(author=user) # WHEN making a post request to the create view url = reverse('exercises:update', kwargs={'pk': ex.id}) request = rf.post(url, data=exercise_data) request.user = user response = views.ExerciseUpdateView.as_view()(request, pk=ex.id) # THEN it redirects back to the home page assert response.status_code == 302 assert response.url == '/'
def test_post_to_create_view_redirects_to_home_page( self, db, rf, users, mocker): mocker.patch('um.exercises.views.Exercise.render_html') mocker.patch('um.exercises.views.Exercise.render_tex') # GIVEN any state and a user user = UserFactory.create() # WHEN making a post request to the create view url = reverse('exercises:create') request = rf.post(url, data=exercise_data) request.user = user response = views.ExerciseCreateView.as_view()(request, url) # THEN it redirects back to the home page assert response.status_code == 302 assert response.url == '/'
def test_post_to_create_view_adds_author_to_object(self, db, rf, mocker): mocker.patch('um.exercises.views.Exercise.render_html') mocker.patch('um.exercises.views.Exercise.render_tex') # GIVEN an empty database assert models.Exercise.objects.count() == 0 # AND a user user = UserFactory.create() # WHEN making a post request to the create view url = reverse('exercises:create') request = rf.post(url, data=exercise_data) request.user = user views.ExerciseCreateView.as_view()(request) # THEN the user gets attached to the exercise as the author ex = models.Exercise.objects.last() assert ex.author == user
def test_post_to_update_view_redirects_to_detail_view( self, db, rf, mocker): mocker.patch('um.exercises.views.Exercise.render_html') mocker.patch('um.exercises.views.Exercise.render_tex') # GIVEN an existing exercise user = UserFactory.create() ex = factories.ExerciseFactory.create(author=user) exercise_data['author'] = user.id # WHEN making a post request to the create view url = reverse('exercises:update', kwargs={'slug': ex.slug}) request = rf.post(url, data=exercise_data) request.user = user response = views.ExerciseUpdateView.as_view()(request, slug=ex.slug) # THEN it redirects to the detail view assert response.status_code == 302 assert response.url == ex.url
def test_creating_new_original_exercise_clears_source_information( self, db, rf, mocker): mocker.patch('um.exercises.views.Exercise.render_html') mocker.patch('um.exercises.views.Exercise.render_tex') # GIVEN a user and extended exercise_data user = UserFactory.create() exercise_data['is_original'] = True exercise_data['original_author'] = user.id exercise_data['source_url'] = 'http://example.com' # WHEN creating the new exercise url = reverse('exercises:create') request = rf.post(url, data=exercise_data) request.user = user views.ExerciseCreateView.as_view()(request) # THEN the source information is wiped ex = models.Exercise.objects.last() assert not ex.original_author assert not ex.source_url
def test_post_request_with_continue_parameter_returns_to_update_view( self, db, rf, mocker): mocker.patch('um.exercises.views.Exercise.render_html') mocker.patch('um.exercises.views.Exercise.render_tex') # GIVEN an existing exercise user = UserFactory.create() ex = factories.ExerciseFactory.create(author=user) exercise_data['author'] = user.id exercise_data['continue'] = 'continue' # WHEN making a post request to the create view url = reverse('exercises:update', kwargs={'slug': ex.slug}) request = rf.post(url, data=exercise_data, name='continue') request.user = user response = views.ExerciseUpdateView.as_view()(request, slug=ex.slug) # THEN it returns to the update view assert response.status_code == 302 assert response.url == url
def test_get_with_url_parameter_prepopulates_text(self, db, rf, mocker): # GIVEN an existing exercise mocker.patch('um.exercises.factories.Exercise.render_html') mocker.patch('um.exercises.factories.Exercise.render_tex') ex = factories.ExerciseFactory.create() # AND a user user = UserFactory.create() # WHEN making a GET request to the create view with the exercise id as url parameter url = reverse('exercises:create') + f'?template={ex.id}' request = rf.get(url) request.user = user response = views.ExerciseCreateView.as_view()(request, url) # THEN it's there assert response.status_code == 200 # AND the input field is pre-populated with the existing exercise's text response.render() html = response.content.decode() assert ex.text in html