from django.test import TestCase
from users.models import Invitation, OCUser
from communities.tests.common import create_sample_community


class AcceptInvitationViewTest(TestCase):
    def setUp(self):
        (self.community, self.members, self.chairmen) = create_sample_community()
        self.invitation = Invitation.objects.create(community=self.community,
                                                    created_by=self.chairmen[0],
                                                    email="<EMAIL>")

    def test_accept_get(self):
        response = self.client.get(self.invitation.get_absolute_url())
        self.assertIn("<EMAIL>", response.content)

    def test_accept(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "the users name",
            "password1": "<PASSWORD>",
            "password2": "<PASSWORD>",
            "signup": 1
        }, follow=False)
        self.assertEqual(response.status_code, 302)
        self.assertIn(self.community.get_absolute_url(), response._headers["location"][1])
        user = OCUser.objects.get(email="<EMAIL>")
        self.assertEqual(user.display_name, "the users name")

    def test_no_display_name(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "",
            "password1": "<PASSWORD>",
            "password2": "<PASSWORD>"
        })
        self.assertEqual(response.status_code, 200)
        self.assertIn("This field is required", response.content)
        self.assertEqual(OCUser.objects.filter(email="<EMAIL>").count(), 0)

    def test_no_password(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "zxczxc",
            "password1": "",
            "password2": ""
        })
        self.assertEqual(response.status_code, 200)
        self.assertIn("This field is required", response.content)
        self.assertEqual(OCUser.objects.filter(email="<EMAIL>").count(), 0)

    def test_mismatch_password(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "zxczxc",
            "password1": "<PASSWORD>",
            "password2": "<PASSWORD>"
        })
        self.assertEqual(response.status_code, 200)
        self.assertIn("Passwords don&#39;t match", response.content)
        self.assertEqual(OCUser.objects.filter(email="<EMAIL>").count(), 0)
from django.test import TestCase
from users.models import Invitation, OCUser
from communities.tests.common import create_sample_community


class AcceptInvitationViewTest(TestCase):
    def setUp(self):
        (self.community, self.members, self.chairmen) = create_sample_community()
        self.invitation = Invitation.objects.create(community=self.community,
                                                    created_by=self.chairmen[0],
                                                    email="<EMAIL>")

    def test_accept_get(self):
        response = self.client.get(self.invitation.get_absolute_url())
        self.assertIn("<EMAIL>", response.content)

    def test_accept(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "the users name",
            "password1": "<PASSWORD>",
            "password2": "<PASSWORD>",
            "signup": 1
        }, follow=False)
        self.assertEqual(response.status_code, 302)
        self.assertIn(self.community.get_absolute_url(), response._headers["location"][1])
        user = OCUser.objects.get(email="<EMAIL>")
        self.assertEqual(user.display_name, "the users name")

    def test_no_display_name(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "",
            "password1": "<PASSWORD>",
            "password2": "<PASSWORD>"
        })
        self.assertEqual(response.status_code, 200)
        self.assertIn("This field is required", response.content)
        self.assertEqual(OCUser.objects.filter(email="<EMAIL>").count(), 0)

    def test_no_password(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "zxczxc",
            "password1": "",
            "password2": ""
        })
        self.assertEqual(response.status_code, 200)
        self.assertIn("This field is required", response.content)
        self.assertEqual(OCUser.objects.filter(email="<EMAIL>").count(), 0)

    def test_mismatch_password(self):
        response = self.client.post(self.invitation.get_absolute_url(), {
            "display_name": "zxczxc",
            "password1": "<PASSWORD>",
            "password2": "<PASSWORD>"
        })
        self.assertEqual(response.status_code, 200)
        self.assertIn("Passwords don&#39;t match", response.content)
        self.assertEqual(OCUser.objects.filter(email="<EMAIL>").count(), 0)
#39;t match", response.content)