def test_login_bad_password(client: Client) -> None: """Assert that logging in with an incorrect password does not work.""" create_test_user() response = client.post("/login/", { "email": guy.email, "password": "******" }) assert response.status_code == 302 assert response.wsgi_request.user.is_anonymous assert not response.wsgi_request.user.is_authenticated
def test_after_login_redirect(client: Client) -> None: """Verify that users are redirected to the page they were attempting to reach.""" create_test_user(is_grafeas_staff=True) response = client.post( "/login/?next=/admin/", { "email": guy.email, "password": guy.password }, follow=True, ) assert response.wsgi_request.path == "/admin/"
def test_homepage_navbar_contents_logged_in_admin( client: Client, django_user_model: BlossomUser) -> None: """Verify that the navbar behaves appropriately when logged in as staff.""" user = create_test_user(django_user_model, is_grafeas_staff=True) client.force_login(user) resp = client.get("/") assert b'id="adminView"' in resp.content
def test_logout(client: Client) -> None: """Assert that logging out successfully logs the user out.""" user = create_test_user() client.force_login(user) assert client.request().context.get("user").is_authenticated client.get("/logout/") assert not client.request().context.get("user").is_authenticated
def test_adduser_form_load(client: Client) -> None: """Verify that the correct form loads when adding a user.""" superuser = create_test_user(superuser=True) client.force_login(superuser) result = client.get(reverse("user_create")) assert result.status_code == 200 for field in result.context["form"].fields: assert field in AddUserForm.declared_fields
def test_post_form_load(client: Client) -> None: """Verify that the correct form loads when creating a post.""" user = create_test_user(is_grafeas_staff=True) client.force_login(user) result = client.get(reverse("post_create")) assert result.status_code == 200 for field in result.context["form"].fields: assert field in PostAddForm.Meta.fields
def test_homepage_navbar_contents_logged_in( client: Client, django_user_model: BlossomUser) -> None: """Verify that the navbar behaves appropriately when logged in as non-staff.""" user = create_test_user(django_user_model) client.force_login(user) resp = client.get("/") assert len(resp.context["navbar"]) == 2 assert b'id="adminView"' not in resp.content # not staff assert b'id="logoutButton"' in resp.content
def test_post_edit_context(client: Client) -> None: """Verify that the appropriate context is loaded when editing a post.""" user = create_test_user(is_grafeas_staff=True) client.force_login(user) result = client.get(Post.objects.get(id=1).get_absolute_url() + "edit/") assert result.status_code == 200 # if this is populated, it means that the additional context gathering # fired appropriately assert "enable_trumbowyg" in result.context
def test_post_context(client: Client) -> None: """Verify that the appropriate context is returned when viewing a post.""" user = create_test_user() client.force_login(user) result = client.get(Post.objects.get(id=1).get_absolute_url()) assert result.status_code == 200 # if this is populated, it means that the additional context gathering # fired appropriately assert result.context["navbar"]
def test_login(client: Client) -> None: """Assert that logging in works as expected.""" user = create_test_user() response = client.post("/login/", { "email": guy.email, "password": guy.password }) assert response.status_code == 302 assert response.wsgi_request.user == user assert response.wsgi_request.user.is_authenticated
def test_post_create(client: Client) -> None: """Verify that post creation works as expected.""" user = create_test_user(is_grafeas_staff=True) client.force_login(user) data = { "title": "A", "body": "B", "published": "on", } assert Post.objects.filter(title="A").count() == 0 client.post(reverse("post_create"), data) assert Post.objects.filter(title="A").count() == 1
def test_single_post(client: Client, django_user_model: BlossomUser) -> None: """Verify that the regular site renders a single post correctly.""" user = create_test_user(django_user_model) post_obj = Post.objects.create(title="a", body="b", author=user, engineeringblogpost=True, published=True) result = client.get(reverse("blog_index")) assert result.status_code == 200 assert result.context["posts"][0] == post_obj
def test_adduser_form_duplicate_email(client: Client) -> None: """Verify that an error is returned when creating a user with a duplicate email.""" superuser = create_test_user(superuser=True) client.force_login(superuser) data = { "username": jane.username, # username that isn't already in the db "password": jane.password, "email": guy.email, # email that is the same as our superuser "is_superuser": "******", } result = client.post(reverse("user_create"), data) assert result.context["form"].errors["email"][0] == "Email already exists"
def test_adduser_form_add_user(client: Client) -> None: """Verify that a superuser is able to add users.""" superuser = create_test_user(superuser=True) client.force_login(superuser) data = { "username": jane.username, "password": jane.password, "email": jane.email, "is_superuser": "******", # why the hell is it not True or False‽ } assert BlossomUser.objects.filter(username=jane.username).count() == 0 client.post(reverse("user_create"), data) assert BlossomUser.objects.filter(username=jane.username).count() == 1
def test_post_name(client: Client) -> None: """Verify that post names are returned in the proper format.""" user = create_test_user(is_grafeas_staff=True) client.force_login(user) data = { "title": "A", "body": "B", "published": "on", } client.post(reverse("post_create"), data) a_post = Post.objects.get(title="A") assert str(a_post) == "A" a_post.standalone_section = True a_post.header_order = 99 a_post.save() a_post = Post.objects.get(title="A") assert str(a_post) == "Section | Header order 99: A"
def test_homepage_post_view(client: Client, django_user_model: BlossomUser) -> None: """Verify that the homepage works appropriately.""" user = create_test_user(django_user_model) resp = client.get("/") assert len(resp.context["posts"]) == 0 p1 = Post.objects.create(title="TestPost1", body="testpost1", author=user, published=True) resp = client.get("/") assert len(resp.context["posts"]) == 1 assert p1 in resp.context["posts"] p2 = Post.objects.create(title="TestPost2", body="testpost2", author=user, published=True) resp = client.get("/") assert len(resp.context["posts"]) == 2 assert p1 in resp.context["posts"] assert p2 in resp.context["posts"]
def test_adduser_form_insufficient_privileges(client: Client) -> None: """Verify that a normal user cannot access the adduser form.""" user = create_test_user() client.force_login(user) result = client.get(reverse("user_create")) assert result.status_code == 302