def test_invitation_does_not_exist(rf, get_object_or_404): """Not-existing invitations give 404, also for anonymous users""" request = rf.get("/?next=/success/") get_object_or_404.side_effect = Http404 with pytest.raises(Http404): views.accept_invitation(request, "foo") get_object_or_404.assert_called_with(Invitation, slug="foo")
def test_invitation_expired(rf, get_object_or_404, invitation, invited_user): """Expired invitations give 403""" request = rf.get("/?next=/success/") request.user = invited_user get_object_or_404.return_value = invitation invitation.created_at = timezone.now() - timedelta(days=15) with pytest.raises(PermissionDenied, match=".*has expired.*"): views.accept_invitation(request, "foo") get_object_or_404.assert_called_with(Invitation, slug="foo")
def test_invitation_used(rf, get_object_or_404, invitation, invited_user): """Non-acceptable invitations give 403""" request = rf.get("/?next=/success/") request.user = invited_user get_object_or_404.return_value = invitation invitation.status = Invitation.ACCEPTED with pytest.raises(PermissionDenied, match=".*has been used already.*"): views.accept_invitation(request, "foo") get_object_or_404.assert_called_with(Invitation, slug="foo") assert not invitation.accept.called
def test_invitation_expired_anonymous_user(rf, get_object_or_404, invitation): """Expired invitations give a redirect to login for anonymous users""" request = rf.get("/?next=/success/") request.user = AnonymousUser() get_object_or_404.return_value = invitation invitation.created_at = timezone.now() - timedelta(days=15) response = views.accept_invitation(request, "foo") assert response.status_code == 302
def test_invitation_accept_no_verified_email(rf, get_object_or_404, invitation, invited_user): request = rf.get("/?next=/success/") request.user = invited_user invited_user.email = "" # no verified email results in a blank user.email get_object_or_404.return_value = invitation response = views.accept_invitation(request, "foo") assert response.status_code == 302 assert response.url == "/success/"
def test_invitation_used_anonymous_user(rf, get_object_or_404, invitation): """Non-acceptable invitations give a redirect to login for anonymous users """ request = rf.get("/?next=/success/") request.user = AnonymousUser() get_object_or_404.return_value = invitation invitation.status = Invitation.ACCEPTED response = views.accept_invitation(request, "foo") assert response.status_code == 302
def test_invitation_accept(rf, get_object_or_404, invitation, invited_user): request = rf.get("/?next=/success/") request.user = invited_user get_object_or_404.return_value = invitation response = views.accept_invitation(request, "foo") assert response.status_code == 302 assert response.url == "/success/" get_object_or_404.assert_called_with(Invitation, slug="foo") invitation.accept.assert_called_with(request.user)
def test_invitation_not_logged_in(rf, get_object_or_404, invitation): request = rf.get("/some/url/") request.user = AnonymousUser() get_object_or_404.return_value = invitation response = views.accept_invitation(request, "foo") assert response.status_code == 302 _, _, path, _, query, _ = urlparse(response.url) assert path == "/login/" query_parsed = parse_qs(query) assert query_parsed["invitation"] == ["foo"] assert query_parsed["next"] == ["/some/url/"] get_object_or_404.assert_called_with(Invitation, slug="foo")