From 5f3b6123d0b6416c9805309f95ccf1eec7e76de6 Mon Sep 17 00:00:00 2001 From: Katherine Michel Date: Fri, 26 Jan 2018 14:15:36 -0600 Subject: [PATCH 01/13] Fix kwarg --- pinax/teams/views.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pinax/teams/views.py b/pinax/teams/views.py index 0385280..39f8583 100644 --- a/pinax/teams/views.py +++ b/pinax/teams/views.py @@ -245,7 +245,7 @@ def get_form_success_data(self, form): "invite_form": self.get_unbound_form(), "team": self.team }, - context_instance=RequestContext(self.request) + request=self.request ) } @@ -269,7 +269,7 @@ def get_form_success_data(self, form): "membership": membership, "team": self.team }, - context_instance=RequestContext(self.request) + request=self.request ) } }) @@ -293,7 +293,7 @@ def form_invalid(self, form): "html": render_to_string("pinax/teams/_invite_form.html", { "invite_form": form, "team": self.team - }, context_instance=RequestContext(self.request)) + }, request=self.request) } return self.render_to_response(data) @@ -324,7 +324,7 @@ def team_member_resend_invite(request, pk): "membership": membership, "team": request.team }, - context_instance=RequestContext(request) + request=self.request ) } return HttpResponse(json.dumps(data), content_type="application/json") @@ -342,7 +342,7 @@ def team_member_promote(request, pk): "membership": membership, "team": request.team }, - context_instance=RequestContext(request) + request=self.request ) } return HttpResponse(json.dumps(data), content_type="application/json") @@ -360,7 +360,7 @@ def team_member_demote(request, pk): "membership": membership, "team": request.team }, - context_instance=RequestContext(request) + request=self.request ) } return HttpResponse(json.dumps(data), content_type="application/json") From 564d436c2a559ddea59ee21528d0dfa01cc8f8f7 Mon Sep 17 00:00:00 2001 From: Katherine Michel Date: Mon, 29 Jan 2018 18:35:33 -0600 Subject: [PATCH 02/13] Adding django-test-plus --- pinax/teams/tests/tests.py | 2 +- setup.py | 1 + tox.ini | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index a481585..3c18237 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -1,7 +1,7 @@ from __future__ import unicode_literals from django.contrib.auth.models import User -from django.test import TestCase +from test_plus.test import TestCase import pinax.teams.receivers # noqa - for django 1.6 tests from pinax.teams.models import Membership, Team, avatar_upload diff --git a/setup.py b/setup.py index ad7303e..cd62017 100644 --- a/setup.py +++ b/setup.py @@ -90,6 +90,7 @@ "six>=1.9.0" ], tests_require=[ + "django-test-plus", ], test_suite="runtests.runtests", zip_safe=False diff --git a/tox.ini b/tox.ini index d21eaad..350ae8a 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ inline-quotes = double [isort] multi_line_output=3 known_django=django -known_third_party=account,appconf,jsonfield,mock,pinax,pytz,reversion,six,slugify +known_third_party=account,appconf,jsonfield,mock,pinax,pytz,reversion,six,slugify,,test_plus sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER include_trailing_comma=True skip_glob=**/*/migrations/* From a297e888d05256a0097c0c8c385e308deb39c514 Mon Sep 17 00:00:00 2001 From: Katherine Michel Date: Mon, 29 Jan 2018 18:36:52 -0600 Subject: [PATCH 03/13] Removing extra comma --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 350ae8a..9ed5ddb 100644 --- a/tox.ini +++ b/tox.ini @@ -8,7 +8,7 @@ inline-quotes = double [isort] multi_line_output=3 known_django=django -known_third_party=account,appconf,jsonfield,mock,pinax,pytz,reversion,six,slugify,,test_plus +known_third_party=account,appconf,jsonfield,mock,pinax,pytz,reversion,six,slugify,test_plus sections=FUTURE,STDLIB,DJANGO,THIRDPARTY,FIRSTPARTY,LOCALFOLDER include_trailing_comma=True skip_glob=**/*/migrations/* From d43424f35626ec8864e70379f55ddb648bbf0516 Mon Sep 17 00:00:00 2001 From: Katherine Michel Date: Mon, 29 Jan 2018 19:03:52 -0600 Subject: [PATCH 04/13] Adding view tests --- pinax/teams/tests/tests.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index 3c18237..7f7b744 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -312,3 +312,33 @@ def test_can_join_declined(self): membership.state = Membership.STATE_DECLINED membership.save() self.assertFalse(team.can_join(paltman)) + +class ViewTests(TestCase): + + def test_team_member_resent_invite_view(self): + """verify no errors when posting bad data""" + user = self.make_user("carla") + post_data = { + } + with self.login(user): + self.post("pinax_teams:team_member_resend_invite", data=post_data) + self.response_200() + + def test_team_member_promote_view(self): + """verify no errors when posting good form data""" + user = self.make_user("amy") + post_data = { + "email_address": "amy@example.com" + } + with self.login(user): + self.post("pinax_teams:team_member_promote", data=post_data) + self.response_200() + + def test_team_member_demote_view(self): + """verify no errors when posting bad data""" + user = self.make_user("sandee") + post_data = { + } + with self.login(user): + self.post("pinax_teams:team_member_demote", data=post_data) + self.response_200() \ No newline at end of file From 8d9a2f30ddae7ff51d71d2ee240d2fcb0c58f661 Mon Sep 17 00:00:00 2001 From: Katherine Michel Date: Mon, 29 Jan 2018 23:32:58 -0600 Subject: [PATCH 05/13] Updating tests --- pinax/teams/tests/tests.py | 54 ++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 19 deletions(-) diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index 7f7b744..9dc017d 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -313,32 +313,48 @@ def test_can_join_declined(self): membership.save() self.assertFalse(team.can_join(paltman)) + +class ManagerPromoteMemberTests(BaseTeamTests): + + MANAGER_ACCESS = Team.MANAGER_ACCESS_INVITE + MEMBER_ACCESS = Team.MEMBER_ACCESS_INVITATION + + def test_promote_member(self): + team = self._create_team() + paltman = User.objects.create_user(username="paltman") + membership.promote(paltman, role=Membership.ROLE_MANAGER) + membership.save() + self.assertTrue(team.can_join(paltman)) + + +class ManagerDemoteMemberTests(BaseTeamTests): + + MANAGER_ACCESS = Team.MANAGER_ACCESS_INVITE + MEMBER_ACCESS = Team.MEMBER_ACCESS_INVITATION + + def test_demote_member(self): + team = self._create_team() + paltman = User.objects.create_user(username="paltman") + membership.demote(paltman, role=Membership.ROLE_MEMBER) + membership.save() + self.assertTrue(team.can_join(paltman)) + + class ViewTests(TestCase): - def test_team_member_resent_invite_view(self): - """verify no errors when posting bad data""" - user = self.make_user("carla") - post_data = { - } - with self.login(user): - self.post("pinax_teams:team_member_resend_invite", data=post_data) - self.response_200() - - def test_team_member_promote_view(self): + def test_team_member_resend_invite_view(self): """verify no errors when posting good form data""" user = self.make_user("amy") post_data = { "email_address": "amy@example.com" } - with self.login(user): - self.post("pinax_teams:team_member_promote", data=post_data) - self.response_200() - - def test_team_member_demote_view(self): + self.post("pinax_teams:team_member_resend_invite", data=post_data) + self.response_200() + + def test_team_member_resend_invite_view_bad_data(self): """verify no errors when posting bad data""" - user = self.make_user("sandee") + user = self.make_user("amy") post_data = { } - with self.login(user): - self.post("pinax_teams:team_member_demote", data=post_data) - self.response_200() \ No newline at end of file + self.post("pinax_teams:team_member_resend_invite", data=post_data) + self.response_200() \ No newline at end of file From 971d052a9a4ed8d2001534978c23e5e3c1086b88 Mon Sep 17 00:00:00 2001 From: Katherine Michel Date: Tue, 30 Jan 2018 15:57:37 -0600 Subject: [PATCH 06/13] Updating test --- pinax/teams/tests/tests.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index 9dc017d..7e26f8d 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -340,21 +340,25 @@ def test_demote_member(self): self.assertTrue(team.can_join(paltman)) -class ViewTests(TestCase): - +class ViewTests(BaseTeamTests): + + MANAGER_ACCESS = Team.MANAGER_ACCESS_INVITE + MEMBER_ACCESS = Team.MEMBER_ACCESS_INVITATION + def test_team_member_resend_invite_view(self): - """verify no errors when posting good form data""" - user = self.make_user("amy") - post_data = { - "email_address": "amy@example.com" - } - self.post("pinax_teams:team_member_resend_invite", data=post_data) - self.response_200() - - def test_team_member_resend_invite_view_bad_data(self): - """verify no errors when posting bad data""" - user = self.make_user("amy") + """Ensure invite is resend invite view is rendered properly""" + team = self._create_team() + paltman = User.objects.create_user(username="paltman") + membership = team.add_user(paltman, Membership.ROLE_MEMBER) post_data = { + "membership": membership, + "team": self.team } - self.post("pinax_teams:team_member_resend_invite", data=post_data) - self.response_200() \ No newline at end of file + + response = self.post( + "teams/_membership.html", + pk=comment.pk, + data=post_data, + extra=dict(HTTP_X_REQUESTED_WITH="XMLHttpRequest") + ) + self.assertEqual(response.status_code, 200) \ No newline at end of file From b59def00ee8dfee41b3597caeabec7ca1415612b Mon Sep 17 00:00:00 2001 From: Katherine Michel Date: Tue, 30 Jan 2018 21:45:40 -0600 Subject: [PATCH 07/13] Updating tests and views --- pinax/teams/tests/tests.py | 37 +++---------------------------------- pinax/teams/views.py | 8 ++++---- 2 files changed, 7 insertions(+), 38 deletions(-) diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index 7e26f8d..08d6441 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -313,52 +313,21 @@ def test_can_join_declined(self): membership.save() self.assertFalse(team.can_join(paltman)) - -class ManagerPromoteMemberTests(BaseTeamTests): - - MANAGER_ACCESS = Team.MANAGER_ACCESS_INVITE - MEMBER_ACCESS = Team.MEMBER_ACCESS_INVITATION - - def test_promote_member(self): - team = self._create_team() - paltman = User.objects.create_user(username="paltman") - membership.promote(paltman, role=Membership.ROLE_MANAGER) - membership.save() - self.assertTrue(team.can_join(paltman)) - - -class ManagerDemoteMemberTests(BaseTeamTests): - - MANAGER_ACCESS = Team.MANAGER_ACCESS_INVITE - MEMBER_ACCESS = Team.MEMBER_ACCESS_INVITATION - - def test_demote_member(self): - team = self._create_team() - paltman = User.objects.create_user(username="paltman") - membership.demote(paltman, role=Membership.ROLE_MEMBER) - membership.save() - self.assertTrue(team.can_join(paltman)) - class ViewTests(BaseTeamTests): MANAGER_ACCESS = Team.MANAGER_ACCESS_INVITE MEMBER_ACCESS = Team.MEMBER_ACCESS_INVITATION - def test_team_member_resend_invite_view(self): + def test_team_member_promote_view(self): """Ensure invite is resend invite view is rendered properly""" team = self._create_team() paltman = User.objects.create_user(username="paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) - post_data = { - "membership": membership, - "team": self.team - } response = self.post( - "teams/_membership.html", - pk=comment.pk, - data=post_data, + "pinax_teams:team_member_promote", + pk=membership.pk, extra=dict(HTTP_X_REQUESTED_WITH="XMLHttpRequest") ) self.assertEqual(response.status_code, 200) \ No newline at end of file diff --git a/pinax/teams/views.py b/pinax/teams/views.py index 39f8583..62f3720 100644 --- a/pinax/teams/views.py +++ b/pinax/teams/views.py @@ -324,7 +324,7 @@ def team_member_resend_invite(request, pk): "membership": membership, "team": request.team }, - request=self.request + request=request ) } return HttpResponse(json.dumps(data), content_type="application/json") @@ -333,7 +333,7 @@ def team_member_resend_invite(request, pk): @manager_required @require_POST def team_member_promote(request, pk): - membership = get_object_or_404(request.team.memberships.all(), pk=pk) + membership = get_object_or_404(request.membership.objects.all(), pk=pk) membership.promote(by=request.user) data = { "html": render_to_string( @@ -342,7 +342,7 @@ def team_member_promote(request, pk): "membership": membership, "team": request.team }, - request=self.request + request=request ) } return HttpResponse(json.dumps(data), content_type="application/json") @@ -360,7 +360,7 @@ def team_member_demote(request, pk): "membership": membership, "team": request.team }, - request=self.request + request=request ) } return HttpResponse(json.dumps(data), content_type="application/json") From 6b8300b4c94189f4caa92d0d62824d46a2963810 Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Tue, 30 Jan 2018 22:16:01 -0700 Subject: [PATCH 08/13] Test member promote view Add account urls (required for view tests). Add TEMPLATES settings (required for template rendering). Add expected HTML snippets (required for template rendering). Restore view queryset. --- README.md | 10 ++ pinax/teams/tests/settings.py | 19 ++++ .../templates/pinax/teams/_invite_form.html | 3 + .../templates/pinax/teams/_membership.html | 1 + pinax/teams/tests/tests.py | 98 +++++++++---------- pinax/teams/tests/urls.py | 1 + pinax/teams/views.py | 3 +- 7 files changed, 84 insertions(+), 51 deletions(-) create mode 100644 pinax/teams/tests/templates/pinax/teams/_invite_form.html create mode 100644 pinax/teams/tests/templates/pinax/teams/_membership.html diff --git a/README.md b/README.md index 119c1bb..40612a5 100644 --- a/README.md +++ b/README.md @@ -88,11 +88,21 @@ Add `pinax.teams` and other required apps to your `INSTALLED_APPS` setting: ] ``` +Optionally add `TeamMiddleware` to your `MIDDLEWARE` setting: + +```python + MIDDLEWARE = [ + # other middleware + "pinax.teams.middleware.TeamMiddleware", + ] +``` + Finally, add `pinax.teams.urls` to your project urlpatterns: ```python urlpatterns = [ # other urls + url(r"^account/", include("account.urls")), url(r"^teams/", include("pinax.teams.urls", namespace="pinax_teams")), ] ``` diff --git a/pinax/teams/tests/settings.py b/pinax/teams/tests/settings.py index 6e204cb..c826fe8 100644 --- a/pinax/teams/tests/settings.py +++ b/pinax/teams/tests/settings.py @@ -1,3 +1,7 @@ +import os + +PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__)) + DEBUG = True USE_TZ = True TIME_ZONE = "UTC" @@ -24,3 +28,18 @@ ] SITE_ID = 1 SECRET_KEY = "notasecret" +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [ + os.path.join(PACKAGE_ROOT, "templates"), + ], + "APP_DIRS": True, + "OPTIONS": { + "debug": True, + "context_processors": [ + "django.contrib.auth.context_processors.auth", + ] + } + }, +] diff --git a/pinax/teams/tests/templates/pinax/teams/_invite_form.html b/pinax/teams/tests/templates/pinax/teams/_invite_form.html new file mode 100644 index 0000000..4870a9b --- /dev/null +++ b/pinax/teams/tests/templates/pinax/teams/_invite_form.html @@ -0,0 +1,3 @@ +{{ csrf_token }} +{{ invite_form }} +{{ team }} \ No newline at end of file diff --git a/pinax/teams/tests/templates/pinax/teams/_membership.html b/pinax/teams/tests/templates/pinax/teams/_membership.html new file mode 100644 index 0000000..350fb00 --- /dev/null +++ b/pinax/teams/tests/templates/pinax/teams/_membership.html @@ -0,0 +1 @@ +{{ membership }} - {{ team }} \ No newline at end of file diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index 08d6441..cb086f1 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -1,10 +1,9 @@ from __future__ import unicode_literals from django.contrib.auth.models import User -from test_plus.test import TestCase -import pinax.teams.receivers # noqa - for django 1.6 tests from pinax.teams.models import Membership, Team, avatar_upload +from test_plus.test import TestCase class BaseTeamTests(TestCase): @@ -21,7 +20,7 @@ def _create_team(self): ) def setUp(self): - self.user = User.objects.create_user(username="jtauber") + self.user = self.make_user("jtauber") class AvatarUploadTests(TestCase): @@ -59,19 +58,19 @@ def test_team_role_for(self): def test_unknown_user(self): team = self._create_team() - other_user = User.objects.create_user(username="paltman") + other_user = self.make_user("paltman") self.assertIsNone(team.for_user(other_user)) self.assertIsNone(team.role_for(other_user)) def test_user_is_member(self): team = self._create_team() - other_user = User.objects.create_user(username="paltman") + other_user = self.make_user("paltman") team.add_user(other_user, Membership.ROLE_MEMBER) self.assertTrue(team.is_on_team(other_user)) def test_member_can_leave(self): team = self._create_team() - other_user = User.objects.create_user(username="paltman") + other_user = self.make_user("paltman") team.add_user(other_user, Membership.ROLE_MEMBER) self.assertTrue(team.can_leave(other_user)) @@ -91,23 +90,23 @@ class ManagerAddMemberOpenTests(BaseTeamTests): def test_cannot_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.ROLE_MEMBER) self.assertFalse(team.can_apply(paltman)) def test_can_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_apply(paltman)) def test_can_join_non_member(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertTrue(team.can_join(paltman)) def test_can_join_invited(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_INVITED membership.save() @@ -115,7 +114,7 @@ def test_can_join_invited(self): def test_can_join_declined(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.STATE_DECLINED) self.assertFalse(team.can_join(paltman)) @@ -127,23 +126,23 @@ class ManagerAddMemberApplicationTests(BaseTeamTests): def test_cannot_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.ROLE_MEMBER) self.assertFalse(team.can_apply(paltman)) def test_can_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertTrue(team.can_apply(paltman)) def test_can_join_non_member(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_join(paltman)) def test_can_join_invited(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_INVITED membership.save() @@ -151,7 +150,7 @@ def test_can_join_invited(self): def test_can_join_declined(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.STATE_DECLINED) self.assertFalse(team.can_join(paltman)) @@ -163,23 +162,23 @@ class ManagerAddMemberInvitationTests(BaseTeamTests): def test_cannot_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.ROLE_MEMBER) self.assertFalse(team.can_apply(paltman)) def test_can_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_apply(paltman)) def test_can_join_non_member(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_join(paltman)) def test_can_join_invited(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_INVITED membership.save() @@ -187,7 +186,7 @@ def test_can_join_invited(self): def test_can_join_declined(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.STATE_DECLINED) self.assertFalse(team.can_join(paltman)) @@ -199,23 +198,23 @@ class ManagerInviteMemberOpenTests(BaseTeamTests): def test_cannot_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.ROLE_MEMBER) self.assertFalse(team.can_apply(paltman)) def test_can_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_apply(paltman)) def test_can_join_non_member(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertTrue(team.can_join(paltman)) def test_can_join_invited(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_INVITED membership.save() @@ -223,7 +222,7 @@ def test_can_join_invited(self): def test_can_join_declined(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_DECLINED membership.save() @@ -237,23 +236,23 @@ class ManagerInviteMemberApplicationTests(BaseTeamTests): def test_cannot_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.ROLE_MEMBER) self.assertFalse(team.can_apply(paltman)) def test_can_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertTrue(team.can_apply(paltman)) def test_can_join_non_member(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_join(paltman)) def test_can_join_invited(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_INVITED membership.save() @@ -261,7 +260,7 @@ def test_can_join_invited(self): def test_can_join_declined(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_DECLINED membership.save() @@ -270,7 +269,7 @@ def test_can_join_declined(self): def test_add_member_twice_does_not_duplicate(self): team = self._create_team() self.assertEqual(team.memberships.count(), 1) - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_member(paltman) team.add_member(paltman, role=Membership.STATE_APPLIED) self.assertEqual(team.memberships.count(), 2) @@ -283,23 +282,23 @@ class ManagerInviteMemberInvitationTests(BaseTeamTests): def test_cannot_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") team.add_user(paltman, Membership.ROLE_MEMBER) self.assertFalse(team.can_apply(paltman)) def test_can_apply(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_apply(paltman)) def test_can_join_non_member(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") self.assertFalse(team.can_join(paltman)) def test_can_join_invited(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_INVITED membership.save() @@ -307,27 +306,28 @@ def test_can_join_invited(self): def test_can_join_declined(self): team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) membership.state = Membership.STATE_DECLINED membership.save() self.assertFalse(team.can_join(paltman)) - -class ViewTests(BaseTeamTests): - + +class ViewTests(BaseTeamTests): + MANAGER_ACCESS = Team.MANAGER_ACCESS_INVITE MEMBER_ACCESS = Team.MEMBER_ACCESS_INVITATION - + def test_team_member_promote_view(self): - """Ensure invite is resend invite view is rendered properly""" + """Ensure view returns 200""" team = self._create_team() - paltman = User.objects.create_user(username="paltman") + paltman = self.make_user("paltman") membership = team.add_user(paltman, Membership.ROLE_MEMBER) - response = self.post( - "pinax_teams:team_member_promote", - pk=membership.pk, - extra=dict(HTTP_X_REQUESTED_WITH="XMLHttpRequest") - ) - self.assertEqual(response.status_code, 200) \ No newline at end of file + with self.login(self.user): + response = self.post( + "pinax_teams:team_member_promote", + slug=team.slug, + pk=membership.pk, + ) + self.assertEqual(response.status_code, 200) diff --git a/pinax/teams/tests/urls.py b/pinax/teams/tests/urls.py index 995d5ee..38ac843 100644 --- a/pinax/teams/tests/urls.py +++ b/pinax/teams/tests/urls.py @@ -1,5 +1,6 @@ from django.conf.urls import include, url urlpatterns = [ + url(r"^account/", include("account.urls")), url(r"^", include("pinax.teams.urls", namespace="pinax_teams")), ] diff --git a/pinax/teams/views.py b/pinax/teams/views.py index 62f3720..373b2e2 100644 --- a/pinax/teams/views.py +++ b/pinax/teams/views.py @@ -10,7 +10,6 @@ JsonResponse, ) from django.shortcuts import get_object_or_404, redirect, render -from django.template import RequestContext from django.template.loader import render_to_string from django.utils.decorators import method_decorator from django.views.decorators.http import require_POST @@ -333,7 +332,7 @@ def team_member_resend_invite(request, pk): @manager_required @require_POST def team_member_promote(request, pk): - membership = get_object_or_404(request.membership.objects.all(), pk=pk) + membership = get_object_or_404(request.team.memberships.all(), pk=pk) membership.promote(by=request.user) data = { "html": render_to_string( From c568c8567e3f8ef1432e7bc507c1ecf29223a2c0 Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Tue, 30 Jan 2018 22:19:22 -0700 Subject: [PATCH 09/13] Add demote and remove view tests --- pinax/teams/tests/tests.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index cb086f1..ce20ca3 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -331,3 +331,31 @@ def test_team_member_promote_view(self): pk=membership.pk, ) self.assertEqual(response.status_code, 200) + + def test_team_member_demote_view(self): + """Ensure view returns 200""" + team = self._create_team() + paltman = self.make_user("paltman") + membership = team.add_user(paltman, Membership.ROLE_MEMBER) + + with self.login(self.user): + response = self.post( + "pinax_teams:team_member_demote", + slug=team.slug, + pk=membership.pk, + ) + self.assertEqual(response.status_code, 200) + + def test_team_member_remove_view(self): + """Ensure view returns 200""" + team = self._create_team() + paltman = self.make_user("paltman") + membership = team.add_user(paltman, Membership.ROLE_MEMBER) + + with self.login(self.user): + response = self.post( + "pinax_teams:team_member_remove", + slug=team.slug, + pk=membership.pk, + ) + self.assertEqual(response.status_code, 200) From 37148b820b5225e52d9c801895a40d818a27b2bd Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Tue, 30 Jan 2018 22:29:01 -0700 Subject: [PATCH 10/13] pin django-test-plus --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index cd62017..e6fe73b 100644 --- a/setup.py +++ b/setup.py @@ -90,7 +90,7 @@ "six>=1.9.0" ], tests_require=[ - "django-test-plus", + "django-test-plus>=1.0.22", ], test_suite="runtests.runtests", zip_safe=False From 14fa03f1cd94796889b7e1cd9e4118b4af194f82 Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Tue, 30 Jan 2018 22:37:13 -0700 Subject: [PATCH 11/13] Add tests for resend and revoke invite views Add pinax-templates for `account` template support. --- pinax/teams/tests/settings.py | 2 ++ pinax/teams/tests/tests.py | 26 ++++++++++++++++++++++++++ setup.py | 1 + 3 files changed, 29 insertions(+) diff --git a/pinax/teams/tests/settings.py b/pinax/teams/tests/settings.py index c826fe8..83e4553 100644 --- a/pinax/teams/tests/settings.py +++ b/pinax/teams/tests/settings.py @@ -24,6 +24,7 @@ "django.contrib.sites", "account", "pinax.invitations", + "pinax.templates", "pinax.teams", ] SITE_ID = 1 @@ -43,3 +44,4 @@ } }, ] +PINAX_INVITATIONS_DEFAULT_INVITE_ALLOCATION = -1 diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index ce20ca3..00ac6c0 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -359,3 +359,29 @@ def test_team_member_remove_view(self): pk=membership.pk, ) self.assertEqual(response.status_code, 200) + + def test_team_member_resend_invite(self): + """Ensure view returns 200""" + team = self._create_team() + membership = team.invite_user(self.user, "jiggy@widit.com", Membership.ROLE_MEMBER) + + with self.login(self.user): + response = self.post( + "pinax_teams:team_member_resend_invite", + slug=team.slug, + pk=membership.pk, + ) + self.assertEqual(response.status_code, 200) + + def test_team_member_revoke_invite(self): + """Ensure view returns 200""" + team = self._create_team() + membership = team.invite_user(self.user, "jiggy@widit.com", Membership.ROLE_MEMBER) + + with self.login(self.user): + response = self.post( + "pinax_teams:team_member_revoke_invite", + slug=team.slug, + pk=membership.pk, + ) + self.assertEqual(response.status_code, 200) diff --git a/setup.py b/setup.py index e6fe73b..47c332a 100644 --- a/setup.py +++ b/setup.py @@ -91,6 +91,7 @@ ], tests_require=[ "django-test-plus>=1.0.22", + "pinax-templates>=1.0.4", ], test_suite="runtests.runtests", zip_safe=False From f8de2c0507d1ec7a883e437e05a94e0a1d373316 Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Tue, 30 Jan 2018 23:02:20 -0700 Subject: [PATCH 12/13] Add team invite view tests --- pinax/teams/tests/tests.py | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index 00ac6c0..829e9ed 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals +import json from django.contrib.auth.models import User @@ -385,3 +386,41 @@ def test_team_member_revoke_invite(self): pk=membership.pk, ) self.assertEqual(response.status_code, 200) + + def test_team_member_invite(self): + """Ensure view returns 200""" + team = self._create_team() + + post_data = { + "invitee": "jiggy@widit.com", + "role": Membership.ROLE_MEMBER, + } + with self.login(self.user): + response = self.post( + "pinax_teams:team_invite", + slug=team.slug, + data=post_data, + ) + self.assertEqual(response.status_code, 200) + json_data = json.loads(self.last_response.content) + self.assertIn("html", json_data) + self.assertIn("append-fragments", json_data) + + def test_team_member_invite_bad_data(self): + """Ensure view returns 200""" + team = self._create_team() + + post_data = { + "invitee": "jiggy@widit.com", + "role": "lackey", # invalid role, believe it or not! + } + with self.login(self.user): + response = self.post( + "pinax_teams:team_invite", + slug=team.slug, + data=post_data, + ) + self.assertEqual(response.status_code, 200) + json_data = json.loads(self.last_response.content) + self.assertIn("html", json_data) + self.assertNotIn("append-fragments", json_data) From 371458ef2fc028e7953449a8ea939b5406cddb87 Mon Sep 17 00:00:00 2001 From: Graham Ullrich Date: Tue, 30 Jan 2018 23:39:02 -0700 Subject: [PATCH 13/13] Fix CI test problems --- pinax/teams/tests/settings.py | 47 -------------------------------- pinax/teams/tests/tests.py | 7 +++-- runtests.py | 51 ++++++++++++++++++++++++++++++++++- 3 files changed, 55 insertions(+), 50 deletions(-) delete mode 100644 pinax/teams/tests/settings.py diff --git a/pinax/teams/tests/settings.py b/pinax/teams/tests/settings.py deleted file mode 100644 index 83e4553..0000000 --- a/pinax/teams/tests/settings.py +++ /dev/null @@ -1,47 +0,0 @@ -import os - -PACKAGE_ROOT = os.path.abspath(os.path.dirname(__file__)) - -DEBUG = True -USE_TZ = True -TIME_ZONE = "UTC" -DATABASES = { - "default": { - "ENGINE": "django.db.backends.sqlite3", - "NAME": ":memory:" - } -} -MIDDLEWARE = [ - "django.contrib.sessions.middleware.SessionMiddleware", - "django.contrib.auth.middleware.AuthenticationMiddleware", - "django.contrib.messages.middleware.MessageMiddleware" -] -ROOT_URLCONF = "pinax.teams.tests.urls" -INSTALLED_APPS = [ - "django.contrib.auth", - "django.contrib.contenttypes", - "django.contrib.sessions", - "django.contrib.sites", - "account", - "pinax.invitations", - "pinax.templates", - "pinax.teams", -] -SITE_ID = 1 -SECRET_KEY = "notasecret" -TEMPLATES = [ - { - "BACKEND": "django.template.backends.django.DjangoTemplates", - "DIRS": [ - os.path.join(PACKAGE_ROOT, "templates"), - ], - "APP_DIRS": True, - "OPTIONS": { - "debug": True, - "context_processors": [ - "django.contrib.auth.context_processors.auth", - ] - } - }, -] -PINAX_INVITATIONS_DEFAULT_INVITE_ALLOCATION = -1 diff --git a/pinax/teams/tests/tests.py b/pinax/teams/tests/tests.py index 829e9ed..d43adc8 100644 --- a/pinax/teams/tests/tests.py +++ b/pinax/teams/tests/tests.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals + import json from django.contrib.auth.models import User @@ -402,7 +403,8 @@ def test_team_member_invite(self): data=post_data, ) self.assertEqual(response.status_code, 200) - json_data = json.loads(self.last_response.content) + # convert content to a string, some Python versions do not accept `bytes` + json_data = json.loads(self.last_response.content.decode("utf-8")) self.assertIn("html", json_data) self.assertIn("append-fragments", json_data) @@ -421,6 +423,7 @@ def test_team_member_invite_bad_data(self): data=post_data, ) self.assertEqual(response.status_code, 200) - json_data = json.loads(self.last_response.content) + # convert content to a string, some Python versions do not accept `bytes` + json_data = json.loads(self.last_response.content.decode("utf-8")) self.assertIn("html", json_data) self.assertNotIn("append-fragments", json_data) diff --git a/runtests.py b/runtests.py index 5d9a1af..30c6892 100644 --- a/runtests.py +++ b/runtests.py @@ -4,9 +4,58 @@ import django +from django.conf import settings + + +DEFAULT_SETTINGS = dict( + DEBUG = True, + USE_TZ = True, + TIME_ZONE = "UTC", + DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": ":memory:" + } + }, + MIDDLEWARE = [ + "django.contrib.sessions.middleware.SessionMiddleware", + "django.contrib.auth.middleware.AuthenticationMiddleware", + "django.contrib.messages.middleware.MessageMiddleware" + ], + ROOT_URLCONF = "pinax.teams.tests.urls", + INSTALLED_APPS = [ + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.sites", + + "account", + "pinax.invitations", + "pinax.templates", + "pinax.teams", + "pinax.teams.tests", + ], + SITE_ID = 1, + SECRET_KEY = "notasecret", + TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "APP_DIRS": True, + "OPTIONS": { + "debug": True, + "context_processors": [ + "django.contrib.auth.context_processors.auth", + ] + } + }, + ], + PINAX_INVITATIONS_DEFAULT_INVITE_ALLOCATION = -1, +) def runtests(*test_args): - os.environ.setdefault("DJANGO_SETTINGS_MODULE", "pinax.teams.tests.settings") + if not settings.configured: + settings.configure(**DEFAULT_SETTINGS) + django.setup() parent = os.path.dirname(os.path.abspath(__file__))