Skip to content

Commit

Permalink
Refactor tests for idea views
Browse files Browse the repository at this point in the history
  • Loading branch information
slomo committed Sep 1, 2016
1 parent 59e3974 commit ba816db
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 63 deletions.
6 changes: 0 additions & 6 deletions tests/ideas/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
from pytest_factoryboy import register
from tests.comments import factories as comment_factories
from tests.ideas import factories as idea_fatories
from tests.modules import factories as module_factories
from tests.organisations import factories as organisation_factories
from tests.projects import factories as project_factories
from tests.rates import factories as rate_factories

register(rate_factories.RateFactory)
register(comment_factories.CommentFactory)
register(organisation_factories.OrganisationFactory)
register(project_factories.ProjectFactory)
register(module_factories.ModuleFactory)
register(idea_fatories.IdeaFactory)
110 changes: 61 additions & 49 deletions tests/ideas/test_idea_views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pytest
from django.core.urlresolvers import reverse
from tests.utils import add_phase_to_project
from freezegun import freeze_time
from tests.helpers import redirect_target

from euth.ideas import models, phases

Expand All @@ -13,60 +14,71 @@ def test_detail_view(client, idea):


@pytest.mark.django_db
def test_create_view(client, module, user):
add_phase_to_project(module.project, phases.CollectPhase().identifier)
count = models.Idea.objects.all().count()
assert count == 0
url = reverse('idea-create', kwargs={'slug': module.slug})
response = client.get(url)
assert response.status_code == 302
login_url = reverse('login')
client.post(login_url, {'email': user.email, 'password': 'password'})
response = client.get(url)
assert response.status_code == 200
idea = {'name': 'Idea', 'description': 'description'}
client.post(url, idea)
assert response.status_code == 200
count = models.Idea.objects.all().count()
assert count == 1
@pytest.mark.parametrize('active_phase__type',
[phases.CollectPhase().identifier])
def test_create_view(client, active_phase, user):
module = active_phase.module
with freeze_time(active_phase.start_date):
count = models.Idea.objects.all().count()
assert count == 0
url = reverse('idea-create', kwargs={'slug': module.slug})
response = client.get(url)
assert response.status_code == 302
assert redirect_target(response) == 'login'
client.login(username=user.email, password='password')
response = client.get(url)
assert response.status_code == 200
idea = {'name': 'Idea', 'description': 'description'}
response = client.post(url, idea)
assert response.status_code == 302
assert redirect_target(response) == 'idea-detail'
count = models.Idea.objects.all().count()
assert count == 1


@pytest.mark.django_db
def test_create_view_wrong_phase(client, module, user):
add_phase_to_project(module.project, phases.RatePhase().identifier)
url = reverse('idea-create', kwargs={'slug': module.slug})
response = client.get(url)
assert response.status_code == 302
client.login(username=user.email, password='password')
response = client.get(url)
assert response.status_code == 403
@pytest.mark.parametrize('active_phase__type',
[phases.RatePhase().identifier])
def test_create_view_wrong_phase(client, active_phase, user):
module = active_phase.module
with freeze_time(active_phase.start_date):
url = reverse('idea-create', kwargs={'slug': module.slug})
response = client.get(url)
assert response.status_code == 302
client.login(username=user.email, password='password')
response = client.get(url)
assert response.status_code == 403


@pytest.mark.django_db
def test_update_view(client, idea, user):
add_phase_to_project(idea.project, phases.CollectPhase().identifier)
url = reverse('idea-update', kwargs={'slug': idea.slug})
response = client.get(url)
assert response.status_code == 302
login_url = reverse('login')
client.post(login_url, {'email': user.email, 'password': 'password'})
response = client.get(url)
assert response.status_code == 200
data = {'description': 'description', 'name': idea.name}
response = client.post(url, data)
id = idea.pk
updated_idea = models.Idea.objects.get(id=id)
assert updated_idea.description == 'description'
assert response.status_code == 302
@pytest.mark.parametrize('active_phase__type',
[phases.CollectPhase().identifier])
def test_update_view(client, active_phase, idea):
idea.module = active_phase.module
idea.save()
user = idea.creator
with freeze_time(active_phase.start_date):
url = reverse('idea-update', kwargs={'slug': idea.slug})
response = client.get(url)
assert response.status_code == 302
client.login(username=user.email, password='password')
response = client.get(url)
assert response.status_code == 200
data = {'description': 'description', 'name': idea.name}
response = client.post(url, data)
id = idea.pk
updated_idea = models.Idea.objects.get(id=id)
assert updated_idea.description == 'description'
assert response.status_code == 302


@pytest.mark.django_db
def test_delete_view(client, idea, user):
client.login(username=user.email, password='password')
url = reverse('idea-delete', kwargs={'slug': idea.slug})
response = client.post(url)
assert response.status_code == 403

add_phase_to_project(idea.project, phases.CollectPhase().identifier)
response = client.post(url)
assert response.status_code == 302
@pytest.mark.parametrize('active_phase__type',
[phases.CollectPhase().identifier])
def test_delete_view_wrong_user(client, active_phase, idea, user):
idea.module = active_phase.module
with freeze_time(active_phase.start_date):
client.login(username=user.email, password='password')
url = reverse('idea-delete', kwargs={'slug': idea.slug})
response = client.post(url)
assert response.status_code == 403
8 changes: 0 additions & 8 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +0,0 @@
def add_phase_to_project(project, type):
from euth.phases import models
phase = models.Phase.objects.create(
name='Phase name',
description='lorem ipsum',
type=type,
module=project.module_set.first())
return phase

0 comments on commit ba816db

Please sign in to comment.