-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2a7c97
commit 4e9a0f8
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
lacommunaute/documentation/tests/tests_category_create_view.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import pytest # noqa | ||
|
||
from django.urls import reverse | ||
|
||
from lacommunaute.documentation.models import Category | ||
from lacommunaute.users.factories import UserFactory | ||
|
||
|
||
@pytest.fixture(name="url") | ||
def fixture_url(): | ||
return reverse("documentation:category_create") | ||
|
||
|
||
@pytest.fixture(name="superuser") | ||
def fixture_superuser(db): | ||
return UserFactory(is_superuser=True) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"user,status_code", [(None, 302), (lambda: UserFactory(), 403), (lambda: UserFactory(is_superuser=True), 200)] | ||
) | ||
def test_user_passes_test_mixin(client, db, url, user, status_code): | ||
if user: | ||
client.force_login(user()) | ||
response = client.get(url) | ||
assert response.status_code == status_code | ||
|
||
|
||
@pytest.fixture(name="expected_context") | ||
def fixture_expected_context(): | ||
return { | ||
"title": "Créer une nouvelle catégorie", | ||
"back_url": reverse("documentation:category_list"), | ||
} | ||
|
||
|
||
def test_view(client, db, url, superuser, expected_context): | ||
client.force_login(superuser) | ||
response = client.get(url) | ||
assert response.status_code == 200 | ||
assert {k: response.context[k] for k in expected_context.keys()} == expected_context | ||
assert response.context["form"].fields.keys() == {"name", "short_description", "description", "image"} | ||
|
||
|
||
@pytest.fixture(name="post_data") | ||
def fixture_post_data(): | ||
return { | ||
"name": "Test Name", | ||
"short_description": "Test Short Description", | ||
"description": "## Test Description\n- with markdown", | ||
} | ||
|
||
|
||
@pytest.fixture(name="expected_values") | ||
def fixture_expected_values(): | ||
return { | ||
"name": "Test Name", | ||
"short_description": "Test Short Description", | ||
"_description_rendered": "<h2>Test Description</h2>\n\n<ul>\n<li>with markdown</li>\n</ul>", | ||
"slug": "test-name", | ||
} | ||
|
||
|
||
def test_create_category(client, db, url, superuser, post_data, expected_values): | ||
client.force_login(superuser) | ||
response = client.post(url, data=post_data) | ||
assert response.status_code == 302 | ||
|
||
category = Category.objects.get() | ||
assert {k: getattr(category, k) for k in expected_values.keys()} == expected_values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
lacommunaute/templates/documentation/category_create_or_update.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{% extends "board_base.html" %} | ||
{% load i18n %} | ||
{% block sub_title %} | ||
{{ title }} | ||
{% endblock sub_title %} | ||
{% block content %} | ||
<div class="row"> | ||
<div class="col-12"> | ||
<div class="card post-edit"> | ||
<div class="card-header"> | ||
<h3 class="m-0 h4 card-title">{{ title }}</h3> | ||
</div> | ||
<div class="card-body"> | ||
<form method="post" action="." class="form" enctype="multipart/form-data" novalidate> | ||
{% csrf_token %} | ||
{% for error in post_form.non_field_errors %} | ||
<div class="alert alert-danger"> | ||
<i class="icon-exclamation-sign"></i> {{ error }} | ||
</div> | ||
{% endfor %} | ||
{% include "partials/form_field.html" with field=form.name %} | ||
{% include "partials/form_field.html" with field=form.short_description %} | ||
{% include "partials/form_field.html" with field=form.description %} | ||
{% include "partials/form_field.html" with field=form.image %} | ||
<hr class="mb-5"> | ||
<div class="form-actions form-row"> | ||
<div class="form-group col-auto"> | ||
<input type="submit" class="btn btn-primary" value="{% trans "Submit" %}" /> | ||
<a href="{{ back_url }}" class="btn btn-outline-warning">{% trans "Cancel" %}</a> | ||
</div> | ||
</div> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{% endblock content %} |