def setUp(self):
        self.org = Organization(
            account_num=1,
            org_name="Test Org",
            exclude_from_website=0,
            is_member=True,
            member_type="Business",
            business_member_level="Business Leader",
            org_type="Hospitality",
            longitude="-71.117111",
            latitude="42.376266",
            city="Cambridge",
            state="MA",
            country="United States",
            primary_email="*****@*****.**")
        self.org.save()
        self.client = Client()
        self.user = User.objects.create_user(
            username='******', email='*****@*****.**', password='******')
        self.client.login(username='******', password='******')

        self.member = Member(organization=self.org)
        self.member.save()

        self.category1 = SustainabilityTopic(name='Test1', slug='test1')
        self.category1.save()
        self.category2 = SustainabilityTopic(name='Test2', slug='test2')
        self.category2.save()
        self.category3 = SustainabilityTopic(name='Test3', slug='test3')
        self.category3.save()

        self.authorized_user = AuthorizedUser(
            email='*****@*****.**', member=self.member)
        self.authorized_user.save()

        link1 = Link()
        link1.member = self.member
        link1.name = 'Google'
        link1.url = 'http://www.google.com/'
        link1.save()
        link2 = Link()
        link2.member = self.member
        link2.name = 'Yahoo'
        link2.url = 'http://www.yahoo.com/'
        link2.save()
class ViewTestCase(TestCase):
    def setUp(self):
        self.org = Organization(
            account_num=1,
            org_name="Test Org",
            exclude_from_website=0,
            is_member=True,
            member_type="Business",
            business_member_level="Business Leader",
            org_type="Hospitality",
            longitude="-71.117111",
            latitude="42.376266",
            city="Cambridge",
            state="MA",
            country="United States",
            primary_email="*****@*****.**")
        self.org.save()
        self.client = Client()
        self.user = User.objects.create_user(
            username='******', email='*****@*****.**', password='******')
        self.client.login(username='******', password='******')

        self.member = Member(organization=self.org)
        self.member.save()

        self.category1 = SustainabilityTopic(name='Test1', slug='test1')
        self.category1.save()
        self.category2 = SustainabilityTopic(name='Test2', slug='test2')
        self.category2.save()
        self.category3 = SustainabilityTopic(name='Test3', slug='test3')
        self.category3.save()

        self.authorized_user = AuthorizedUser(
            email='*****@*****.**', member=self.member)
        self.authorized_user.save()

        link1 = Link()
        link1.member = self.member
        link1.name = 'Google'
        link1.url = 'http://www.google.com/'
        link1.save()
        link2 = Link()
        link2.member = self.member
        link2.name = 'Yahoo'
        link2.url = 'http://www.yahoo.com/'
        link2.save()

    def test_directory_view(self):
        url = reverse('member_directory')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_detail_view(self):
        url = reverse('member_detail_view', args=(self.member.slug,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_edit_view(self):
        url = reverse('member_edit_view', args=(self.member.slug,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

        filepath = os.path.join(os.path.dirname(__file__), 'image-300x237.png')
        with open(filepath) as upload:
            self.client.post(url, {'logo': upload})

        # get the member from the db again (because it's cached)
        member = Member.objects.get(pk=self.member.id)
        # allow for local testing
        if settings.USE_S3:
            self.assertRegexpMatches(member.logo.url, '.*s3.amazonaws.com.*')
        else:
            self.assertIsNotNone(member.logo.url)

    def test_links(self):
        links = Link.objects.filter(member=self.member)
        # Check that there are 2 lines created in the link table for the member
        self.assertEqual(len(links), 2)
        # Check that Member model method get_links works
        links = self.member.get_links()
        self.assertEqual(len(links), 2)
        # Load detail page and check that it's displaying links table
        url = reverse('member_detail_view', args=(self.member.slug,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Links')

    def test_category(self):
        self.member.category_1 = SustainabilityTopic.objects.get(name='Test1')
        self.member.category_2 = SustainabilityTopic.objects.get(name='Test2')
        self.member.category_3 = SustainabilityTopic.objects.get(name='Test3')
        self.member.save()
        url = reverse('member_detail_view', args=(self.member.slug,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, 'Test1')
        self.assertContains(
            response,
            '/sustainable-campus-partners-directory/'
            '?search_terms=&category=Test1&submit=Search')

    def test_search(self):
        self.member.category_1 = SustainabilityTopic.objects.get(name='Test1')
        self.member.save()
        url = reverse('member_directory')
        url += "?search_terms=&category=Test1&submit=Search"

        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "1 result found.")

        url = reverse('member_directory')
        url += "?search_terms=&category=Test2&submit=Search"
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "No results found.")

        url = reverse('member_directory')
        url += "?search_terms=Cambridge&category=&submit=Search"
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "1 result found.")

    def test_add_link_view(self):
        url = reverse('add_link', args=(self.member.slug,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_delete_link_view(self):

        url = reverse('delete_link',
                      kwargs={'business_slug': self.member.slug, 'id': 1})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_edit_link_view(self):
        link1 = Link()
        link1.member = self.member
        link1.name = 'Google'
        link1.url = 'http://www.google.com/'
        link1.save()

        url = reverse('link_edit_view',
                      kwargs={'business_slug': self.member.slug, 'id': 1})
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)

    def test_rss_feed(self):
        url = reverse('all-topics-feed')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(
            response,
            '/sustainable-campus-partners-directory/test-org-ma/</link>')

        self.member.category_1 = SustainabilityTopic.objects.get(name='Test1')
        self.member.save()
        url = reverse('sustainability-topic-feed', args=('test1',))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(
            response,
            '/sustainable-campus-partners-directory/test-org-ma/</link>')

        url = reverse('sustainability-topic-feed', args=('test2',))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertNotContains(
            response,
            '/sustainable-campus-partners-directory/test-org-ma/</link>')

    def test_primary_email(self):
        # Test to be sure rules work properly
        # with new primary email attribute when no authorized
        # user object is created.
        self.authorized_user.delete()
        queryset = AuthorizedUser.objects.all()
        self.assertFalse(queryset)
        url = reverse('member_edit_view', args=(self.member.slug,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)