50 lines
1.3 KiB
Python
50 lines
1.3 KiB
Python
|
|
from django.urls import reverse
|
|
import pytest
|
|
|
|
from longs.models import Long
|
|
|
|
|
|
def create_question():
|
|
return Long.objects.create(
|
|
description="Test Question",
|
|
)
|
|
|
|
def create_user(db, django_user_model):
|
|
return django_user_model.objects.create_user(
|
|
"user1", "user1@user.com", "password"
|
|
)
|
|
|
|
@pytest.mark.django_db
|
|
def test_question_creation(client, db, django_user_model):
|
|
question = create_question()
|
|
assert question.description == "Test Question"
|
|
assert question.get_absolute_url() == f"/longs/question/{question.pk}/"
|
|
|
|
# Test access
|
|
user = create_user(db, django_user_model)
|
|
client.force_login(user)
|
|
|
|
# View access
|
|
response = client.get(reverse("longs:question_detail", kwargs={"pk": question.pk}), follow=True)
|
|
assert response.status_code == 403
|
|
|
|
response = client.get(reverse("longs:long_update", kwargs={"pk": question.pk}), follow=True)
|
|
assert response.status_code == 403
|
|
|
|
|
|
# Add author
|
|
question.add_author(user)
|
|
|
|
# Test author access
|
|
response = client.get(reverse("longs:question_detail", kwargs={"pk": question.pk}), follow=True)
|
|
assert response.status_code == 200
|
|
|
|
response = client.get(reverse("longs:long_update", kwargs={"pk": question.pk}), follow=True)
|
|
assert response.status_code == 200
|
|
|
|
|
|
|
|
|
|
|