Example #1
0
 def test_anonymous_user_cannot_get_draft_guide(self):
     guide = Guide(
         title="What to do in Montreal!", location="Montreal, Canada", owner=self.user1.get_profile(), is_draft=True
     )
     guide.save()
     response = self.client.get(guide.get_absolute_url())
     self.assertNotEqual(response.status_code, 200)
 def _test_guide_image(self,
                       delete_image = None,
                       delete_tip = None):
     guide = Guide(title = "What to do in Montreal!",
                   location = "Montreal, Canada",
                   owner = self.account,
                   is_draft = True)
     guide.save()
     tip = Tip(title = "this is a tip title",
               location = "this is a tip location",
               description = "this is a tip description",
               guide = guide)
     tip.save()
     image = Image(owner = guide.owner,
                   tip = tip,
                   is_uploaded = False)
     image.save()
     self.assertEqual(guide.image, None)
     image.is_uploaded = True
     image.save()
     self.assertEqual(guide.image.pk, image.pk)
     if delete_image:
         image.delete()
     elif delete_tip:
         tip.delete()
     self.assertEqual(guide.image, None)
 def test_has_slug(self):
     guide = Guide(title = "What to do in Montreal!",
                   location = "Montreal, Canada",
                   owner = self.account,
                   is_draft = True)
     guide.save()
     self.assertTrue(hasattr(guide, "slug"))
    def test_guide_tags_as_list(self):
        guide = Guide(title = "What to do in Montreal!",
                      location = "Montreal, Canada",
                      owner = self.account,
                      is_draft = True)
        guide.save()
        tip = Tip(title = "Peel Pub",
                  location = "Peel Street, near McGill campus",
                  guide = guide)
        tip.save()
        self.assertEqual(len(guide.tags_as_list), 0)

        tip.tags.add("pub", "student", "fun")
        self.assertEqual(len(guide.tags_as_list), 3)
        self.assertTrue("pub" in [tag.tag for tag in guide.tags_as_list])
        self.assertTrue("student" in [tag.tag for tag in guide.tags_as_list])
        self.assertTrue("fun" in [tag.tag for tag in guide.tags_as_list])

        tip.tags.remove("pub")
        self.assertEqual(len(guide.tags_as_list), 2)
        self.assertFalse("pub" in [tag.tag for tag in guide.tags_as_list])
        self.assertTrue("student" in [tag.tag for tag in guide.tags_as_list])
        self.assertTrue("fun" in [tag.tag for tag in guide.tags_as_list])

        tip.tags.clear()
        self.assertEqual(len(guide.tags_as_list), 0)

        tip.tags.set("pub", "student", "fun")
        self.assertEqual(len(guide.tags_as_list), 3)
        tip.delete()
        self.assertEqual(len(guide.tags_as_list), 0)
Example #5
0
 def test_owner_can_get_their_draft_guide(self):
     self.client.login(username=self.user1.username, password="******")
     guide = Guide(
         title="What to do in Montreal!", location="Montreal, Canada", owner=self.user1.get_profile(), is_draft=True
     )
     guide.save()
     response = self.client.get(guide.get_absolute_url())
     self.assertEqual(response.status_code, 200)
 def test_guide_creation(self):
     self.assertEqual(len(Guide.objects.all()), 0)
     guide = Guide(title = "What to do in Montreal!",
                   location = "Montreal, Canada",
                   owner = self.account,
                   is_draft = True)
     guide.save()
     self.assertEqual(len(Guide.objects.all()), 1)
     assert(hasattr(guide, "uuid"))
    def test_slugs_change_when_title_changes(self):
        guide1 = Guide(title = "What to do in Montreal!",
                       location = "Montreal, Canada",
                       owner = self.account,
                       is_draft = True)
        guide1.save()
        self.assertEqual(guide1.slug, "what-to-do-in-montreal")

        guide1.title = "Awesome jogging routes in Montreal!"
        guide1.save()
        self.assertEqual(guide1.slug, "awesome-jogging-routes-in-montreal")
 def test_tip_add(self):
     guide = Guide(title = "What to do in Montreal!",
                   location = "Montreal, Canada",
                   owner = self.account,
                   is_draft = True)
     guide.save()
     self.assertEqual(len(guide.tip_set.all()), 0)
     tip = Tip(title = "Peel Pub",
               location = "Peel Street, near McGill campus",
               guide = guide)
     tip.save()
     self.assertEqual(len(guide.tip_set.all()), 1)
 def test_manage_guides_with_one_public_guide(self):
     guide = Guide(title = "What to do in Montreal!",
                   location = "Montreal, Canada",
                   owner = self.user1.get_profile(),
                   is_draft = False)
     guide.save()
     self.client.login(username = self.user1.username,
                       password = "******")
     response = self.client.get(self.read_profile_uri, follow=True)
     self.assertEquals(response.status_code, 200)
     self.assertEquals(response.context["draft_guides_and_images"], [])
     self.assertEquals(len(response.context["public_guides_and_images"]), 1)
     self.assertEquals(response.context["public_guides_and_images"][0][0], guide)
Example #10
0
    def test_slugs_are_unique(self):
        guide1 = Guide(title = "What to do in Montreal!",
                       location = "Montreal, Canada",
                       owner = self.account,
                       is_draft = True)
        guide1.save()
        self.assertEqual(guide1.slug, "what-to-do-in-montreal")

        guide2 = Guide(title = "What to do in Montreal!",
                       location = "Montreal, Canada",
                       owner = self.account,
                       is_draft = True)
        guide2.save()
        self.assertEqual(guide2.slug, "what-to-do-in-montreal-2")
Example #11
0
 def _test_mark_guide_as_draft(
     self,
     request_is_get=None,
     request_is_post=None,
     starts_as_draft=None,
     is_owner=None,
     is_another_user=None,
     is_collaborator=None,
     is_anonymous=None,
     expect_success=None,
 ):
     assert starts_as_draft is not None
     assert any(request is not None for request in [request_is_get, request_is_post])
     guide = Guide(
         title="What to do in Montreal!",
         location="Montreal, Canada",
         owner=self.user1.get_profile(),
         is_draft=starts_as_draft,
     )
     guide.save()
     if is_owner == True:
         self.client.login(username=self.user1.username, password="******")
     elif is_collaborator == True:
         guide.collaborators.add(self.user2.get_profile())
         self.client.login(username=self.user2.username, password="******")
     elif is_another_user == True:
         self.client.login(username=self.user2.username, password="******")
     elif is_anonymous == True:
         pass
     mark_guide_as_draft_url = reverse("apps.guides.views.mark_guide_as_draft", kwargs={"slug": guide.slug})
     read_guide_url = guide.get_absolute_url()
     if request_is_get:
         response = self.client.get(mark_guide_as_draft_url, follow=True)
     elif request_is_post:
         response = self.client.post(mark_guide_as_draft_url, follow=True)
     if expect_success == True:
         guide2 = Guide.objects.get(pk=guide.id)
         self.assertEquals(guide2.is_draft, True)
         self.assertEqual(response.request["PATH_INFO"], read_guide_url)
     else:
         if not starts_as_draft:
             guide2 = Guide.objects.get(pk=guide.id)
             self.assertEqual(guide2.is_draft, False)
         self.assertNotEqual(response.request["PATH_INFO"], read_guide_url)
Example #12
0
    def test_collaborators_can_read_draft_guide(self):
        guide = Guide(
            title="What to do in Montreal!", location="Montreal, Canada", owner=self.user1.get_profile(), is_draft=True
        )
        guide.save()
        guide.collaborators.add(self.user2.get_profile())
        guide.save()
        user3_account = self.user3.get_profile()
        user3_account.collaborated_guide.add(guide)

        # ---------------------------------------------------------------------
        #   Both user2 and user3 should be able to read the draft guide.
        # ---------------------------------------------------------------------
        self.client.login(username=self.user2.username, password="******")
        response2 = self.client.get(guide.get_absolute_url())
        self.assertEqual(response2.status_code, 200)
        self.client.logout()
        self.client.login(username=self.user3.username, password="******")
        response3 = self.client.get(guide.get_absolute_url())
        self.assertEqual(response3.status_code, 200)
Example #13
0
 def _test_delete_guide(
     self,
     is_draft=None,
     is_owner=None,
     is_collaborator=None,
     is_another_user=None,
     is_anonymous=None,
     expect_success=None,
 ):
     assert is_draft is not None
     guide = Guide(
         title="What to do in Montreal!",
         location="Montreal, Canada",
         owner=self.user1.get_profile(),
         is_draft=is_draft,
     )
     guide.save()
     if is_owner == True:
         self.client.login(username=self.user1.username, password="******")
     elif is_collaborator == True:
         guide.collaborators.add(self.user2.get_profile())
         self.client.login(username=self.user2.username, password="******")
     elif is_another_user == True:
         self.client.login(username=self.user2.username, password="******")
     elif is_anonymous == True:
         pass
     delete_url = reverse("apps.guides.views.delete_guide", kwargs={"slug": guide.slug})
     response = self.client.get(delete_url)
     if expect_success == True:
         self.assertEqual(response.status_code, 200)
     else:
         self.assertNotEqual(response.status_code, 200)
     response2 = self.client.post(delete_url, data={}, follow=True)
     if expect_success == True:
         self.assertEqual(response.status_code, 200)
         self.assertEqual(Guide.objects.count(), 0)
     else:
         self.assertNotEqual(response.status_code, 200)
         self.assertEqual(Guide.objects.count(), 1)
Example #14
0
class ImageTestCase(unittest.TestCase):
    def setUp(self):
        self.root_path = settings.PROJECT_DIR
        self.test_image_dir = os.path.join(self.root_path, "incoming")

        # --------------------------------------------------------------------
        #   All Images need an owner Account. Accounts are associated
        #   with Users. Hence set up a User, whose post_save will give us
        #   an Account.
        # --------------------------------------------------------------------
        self.user = User.objects.create_user(username = '******',
                                             email = '*****@*****.**',
                                             password = '******')
        self.user.save()
        self.account = self.user.get_profile()
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        #   Images must be associated with a Tip, which themselves
        #   require a Guide.
        # --------------------------------------------------------------------
        self.guide = Guide(title = "What to do in Montreal!",
                           location = "Montreal, Canada",
                           owner = self.account,
                           is_draft = True)
        self.guide.save()
        self.tip = Tip(title = "Peel Pub",
                       location = "Peel Street, near McGill campus",
                       guide = self.guide)
        self.tip.save()
        # --------------------------------------------------------------------

    def tearDown(self):
        if default_storage.exists("dog_shelf.jpeg"):
            default_storage.delete("dog_shelf.jpeg")

    def test_exists_after_creation(self):
        """ Test that image creation and upload to S3 works. """
        images = Image.objects.all()
        self.assertTrue(len(images) == 0)

        i = Image(description = "cute dog!", owner=self.account)
        i.tip = self.tip
        i.image_large.name = "dog_shelf.jpeg"
        input_file_path = os.path.join(self.test_image_dir, "dog_shelf.jpeg")
        with open(input_file_path, "rb") as f_in:
            content = ImageFile(f_in)
            default_storage.save(i.image_large.name, content)
        i.save()

        # --------------------------------------------------------------------
        #   Verify that the Django ORM representation exists.
        # --------------------------------------------------------------------
        images = Image.objects.all()
        self.assertTrue(len(images) == 1)
        image = images[0]
        self.assertEqual(i.image_large.name, "dog_shelf.jpeg")
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        #   Verify that an HTTP GET of the image URL works.
        # --------------------------------------------------------------------
        r = requests.get(i.get_absolute_url())
        self.assertEqual(r.status_code, 200)
        self.assertEqual(r.headers.get("Content-Type", None), "image/jpeg")
class EditGuideLiveTestCase(LiveServerTestCase):
    fixtures = ['user-data.json']

    def setUp(self):
        for user in User.objects.all():
            user.delete()

        self.user1 = self._create_and_return_user(username="******", password="******")
        self.user2 = self._create_and_return_user(username="******", password="******")
        self.guide1 = Guide(title = "What to do in Montreal!",
                            location = "Montreal, Canada",
                            owner = self.user1.get_profile(),
                            is_draft = True)
        self.guide1.save()
        self.guide1.collaborators.add(self.user2.get_profile())
        self.tip1_guide1 = Tip(title = "Peel Pub",
                               location = "Peel Street, near McGill campus",
                               description = "Fun times!",
                               guide = self.guide1)
        self.tip1_guide1.save()

    @classmethod
    def setUpClass(cls):
        cls.selenium1 = webdriver.Chrome()
        cls.selenium2 = webdriver.Firefox()
        super(EditGuideLiveTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(EditGuideLiveTestCase, cls).tearDownClass()
        cls.selenium1.quit()
        cls.selenium2.quit()

    def _selenium_wait(self, selenium):
        WebDriverWait(selenium, 5).until(
            lambda driver: driver.find_element_by_tag_name('body'))

    def _create_and_return_user(self,
                                username,
                                password,
                                email = '*****@*****.**'):
        user = User.objects.create_user(username = username,
                                        password = password,
                                        email = email)
        user.save()
        return user

    def _login(self, selenium, user):
        # --------------------------------------------------------------------
        #   Go to the root URI, then find any link called "Login" and click
        #   it.
        # --------------------------------------------------------------------
        selenium.get(self.live_server_url)
        self._selenium_wait(selenium)
        login_link = selenium.find_element_by_link_text("Login")
        login_link.click()
        self._selenium_wait(selenium)

        # --------------------------------------------------------------------
        #   Login.
        # --------------------------------------------------------------------
        for (field_name, field_contents) in [("username", user.username),
                                             ("password", "password")]:
            input_element = selenium.find_element_by_name(field_name)
            input_element.send_keys(field_contents)
        selenium.find_element_by_xpath(".//*[@id='login_button']").click()
        self._selenium_wait(selenium)
        # --------------------------------------------------------------------

    def test_edit_guide_then_view_guide_by_owner_and_collaborator(self):
        # --------------------------------------------------------------------
        #   Login two Selenium instances.
        # --------------------------------------------------------------------
        self._login(self.selenium1, self.user1)
        self._login(self.selenium2, self.user2)
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        #   - Owner opens guide for editing.
        #   - Collaborator opens guide for editing.
        # --------------------------------------------------------------------
        guide_url = self.live_server_url + self.guide1.get_absolute_url()
        for selenium in [self.selenium1, self.selenium2]:
            # Get guide.
            selenium.get(guide_url)
            self._selenium_wait(selenium)

            # Find guide heading.
            heading = selenium.find_element_by_tag_name("h1")
            self.assertIsNotNone(heading)
            self.assertEquals(heading.text, self.guide1.title)

            # Click link to edit guide.
            edit_guide_link = selenium.find_element_by_link_text("Edit this guide")
            self.assertIsNotNone(edit_guide_link)
            edit_guide_link.click()
            self._selenium_wait(selenium)
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        #   - Collaborator makes edit. Succeeds. Reads guide. Sees changes.
        # --------------------------------------------------------------------
        owner_tip_title_element = [element for element in self.selenium1.find_elements_by_tag_name("input")
                                   if element.get_attribute("value") == self.tip1_guide1.title][0]
        owner_tip_title_element.clear()
        owner_tip_title_element.send_keys("new tip title 1")
        owner_tip_title_element.submit()
        self.selenium1.get(guide_url)
        self._selenium_wait(self.selenium1)

        tip_heading = self.selenium1.find_elements_by_css_selector(".tip_title")[0]
        self.assertIsNotNone(tip_heading)
        self.assertEquals(tip_heading.text, "new tip title 1")
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        #   - Owner makes edit. Fails. Reads guide. Does not see changes.
        # --------------------------------------------------------------------
        collaborator_tip_title_element = [element for element in self.selenium2.find_elements_by_tag_name("input")
                                          if element.get_attribute("value") == self.tip1_guide1.title][0]
        collaborator_tip_title_element.clear()
        collaborator_tip_title_element.send_keys("new tip title 2")
        collaborator_tip_title_element.submit()
        self.selenium2.get(guide_url)
        self._selenium_wait(self.selenium2)

        tip_heading = self.selenium1.find_elements_by_css_selector(".tip_title")[0]
        self.assertIsNotNone(tip_heading)
        self.assertEquals(tip_heading.text, "new tip title 1")
class EditGuideViewTestCase(unittest.TestCase):
    def setUp(self):
        self.client = Client()
        self.user1 = self._create_and_return_user(username="******", password="******")
        self.user2 = self._create_and_return_user(username="******", password="******")
        self.user3 = self._create_and_return_user(username="******", password="******")
        self.user4 = self._create_and_return_user(username="******", password="******")
        self.guide_user1_draft = Guide(title = "What to do in Montreal!",
                                       location = "Montreal, Canada",
                                       description = "Description",
                                       owner = self.user1.get_profile(),
                                       is_draft = True)
        self.guide_user1_draft.save()
        self.tip_guide1 = Tip(title = "Old tip title",
                              location = "Old tip location",
                              description = "Old tip description",
                              guide = self.guide_user1_draft)
        self.tip_guide1.save()

    def tearDown(self):
        for user in User.objects.all():
            user.delete()

    def _create_and_return_user(self,
                                username,
                                password,
                                email = '*****@*****.**'):
        user = User.objects.create_user(username = username,
                                        password = password,
                                        email = email)
        user.save()
        return user

    # -------------------------------------------------------------------------
    #   Test 'edit_guide' view.
    #
    #   This needs to test both Guide metadata and child Tips. Rather than
    #   make this function too unwieldy there's a callback,
    #   "verify_guide_after_edit", which'll call a single-argument function
    #   with the new guide.
    # -------------------------------------------------------------------------
    def _test_edit_guide(self,
                         guide = None,
                         post_data = None,
                         verify_guide_after_edit = None,
                         is_owner = None,
                         is_another_user = None,
                         is_collaborator = None,
                         is_anonymous = None):
        if is_owner == True:
            self.client.login(username = self.user1.username,
                              password = "******")
        elif is_collaborator == True:
            guide.collaborators.add(self.user2.get_profile())
            self.client.login(username = self.user2.username,
                              password = "******")
        elif is_another_user == True:
            self.client.login(username = self.user2.username,
                              password = "******")
        elif is_anonymous == True:
            pass
        edit_guide_url = reverse('apps.guides.views.edit_guide', kwargs={'slug': guide.slug})
        response = self.client.get(edit_guide_url, follow=True)
        response2 = self.client.post(edit_guide_url, data=post_data, follow=True)
        guide2 = Guide.objects.get(pk=guide.id)
        verify_guide_after_edit(guide2)
    # -------------------------------------------------------------------------

    # -------------------------------------------------------------------------
    #   'edit_guide' for guide metadata.
    # -------------------------------------------------------------------------
    def test_owner_can_edit_guide_metadata(self):
        guide = self.guide_user1_draft
        old_title = guide.title
        old_location = guide.location
        old_description = guide.description
        def verify_guide_after_edit(guide):
            self.assertEqual(guide.title, "new title")
            self.assertEqual(guide.location, "new location")
            self.assertEqual(guide.description, "new description")
        post_data = {"object_being_modified": "guide_%s" % guide.uuid,
                     "object_version": guide.version,
                     "title": "new title",
                     "location": "new location",
                     "description": "new description"}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_owner = True)

    def test_another_user_cannot_edit_guide_metadata(self):
        guide = self.guide_user1_draft
        old_title = guide.title
        old_location = guide.location
        old_description = guide.description
        def verify_guide_after_edit(guide):
            self.assertEqual(guide.title, old_title)
            self.assertEqual(guide.location, old_location)
            self.assertEqual(guide.description, old_description)
        post_data = {"object_being_modified": "guide_%s" % guide.uuid,
                     "object_version": guide.version,
                     "title": "new title",
                     "location": "new location",
                     "description": "new description"}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_another_user = True)

    def test_collaborator_cannot_edit_guide_metadata(self):
        guide = self.guide_user1_draft
        old_title = guide.title
        old_location = guide.location
        old_description = guide.description
        def verify_guide_after_edit(guide):
            self.assertEqual(guide.title, old_title)
            self.assertEqual(guide.location, old_location)
            self.assertEqual(guide.description, old_description)
        post_data = {"object_being_modified": "guide_%s" % guide.uuid,
                     "object_version": guide.version,
                     "title": "new title",
                     "location": "new location",
                     "description": "new description"}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_collaborator = True)

    def test_anonymous_cannot_edit_guide_metadata(self):
        guide = self.guide_user1_draft
        old_title = guide.title
        old_location = guide.location
        old_description = guide.description
        def verify_guide_after_edit(guide):
            self.assertEqual(guide.title, old_title)
            self.assertEqual(guide.location, old_location)
            self.assertEqual(guide.description, old_description)
        post_data = {"object_being_modified": "guide_%s" % guide.uuid,
                     "object_version": guide.version,
                     "title": "new title",
                     "location": "new location",
                     "description": "new description"}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_anonymous = True)
    # -------------------------------------------------------------------------

    # -------------------------------------------------------------------------
    #   'edit_guide' for creating tips.
    # -------------------------------------------------------------------------
    def test_owner_can_create_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            new_tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertTrue(new_tip.exists())
            self.assertTrue(new_tip.count(), 1)
            new_tip = new_tip[0]
            self.assertEqual(new_tip.title, tip_title)
            self.assertEqual(new_tip.location, tip_location)
            self.assertEqual(new_tip.description, tip_description)
        post_data = {"object_being_created": "tip",
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_owner = True)

    def test_another_user_cannot_create_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            new_tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertFalse(new_tip.exists())
        post_data = {"object_being_created": "tip",
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_another_user = True)

    def test_collaborator_can_create_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            new_tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertTrue(new_tip.exists())
            self.assertTrue(new_tip.count(), 1)
            new_tip = new_tip[0]
            self.assertEqual(new_tip.title, tip_title)
            self.assertEqual(new_tip.location, tip_location)
            self.assertEqual(new_tip.description, tip_description)
        post_data = {"object_being_created": "tip",
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_collaborator = True)

    def test_anonymous_cannot_create_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            new_tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertFalse(new_tip.exists())
        post_data = {"object_being_created": "tip",
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_anonymous = True)
    # -------------------------------------------------------------------------

    # -------------------------------------------------------------------------
    #   'edit_guide' for modifying tips.
    # -------------------------------------------------------------------------
    def test_owner_can_modify_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertTrue(tip.exists())
            self.assertTrue(tip.count(), 1)
            tip = tip[0]
            self.assertEqual(tip.title, tip_title)
            self.assertEqual(tip.location, tip_location)
            self.assertEqual(tip.description, tip_description)
        post_data = {"object_being_modified": "tip_%s" % self.tip_guide1.uuid,
                     "object_version": self.tip_guide1.version,
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_owner = True)

    def test_another_user_cannot_modify_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertFalse(tip.exists())
        post_data = {"object_being_modified": "tip_%s" % self.tip_guide1.uuid,
                     "object_version": self.tip_guide1.version,
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_another_user = True)

    def test_collaborator_can_modify_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertTrue(tip.exists())
            self.assertTrue(tip.count(), 1)
            tip = tip[0]
            self.assertEqual(tip.title, tip_title)
            self.assertEqual(tip.location, tip_location)
            self.assertEqual(tip.description, tip_description)
        post_data = {"object_being_modified": "tip_%s" % self.tip_guide1.uuid,
                     "object_version": self.tip_guide1.version,
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_collaborator = True)

    def test_collaborator_cannot_modify_different_version_of_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertFalse(tip.exists())
        post_data = {"object_being_modified": "tip_%s" % self.tip_guide1.uuid,
                     "object_version": "0" * 32,
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_collaborator = True)

    def test_anonymous_cannot_modify_tip(self):
        guide = self.guide_user1_draft
        tip_title = "New tip title"
        tip_location = "New tip location"
        tip_description = "New tip description"
        def verify_guide_after_edit(guide):
            tip = guide.tip_set.filter(title__exact = tip_title)
            self.assertFalse(tip.exists())
        post_data = {"object_being_modified": "tip_%s" % self.tip_guide1.uuid,
                     "object_version": self.tip_guide1.version,
                     "title": tip_title,
                     "location": tip_location,
                     "description": tip_description}
        self._test_edit_guide(guide = guide,
                              post_data = post_data,
                              verify_guide_after_edit = verify_guide_after_edit,
                              is_anonymous = True)
class DeleteTipViewTestCase(unittest.TestCase):
    def setUp(self):
        self.client = Client()
        self.user1 = self._create_and_return_user(username="******", password="******")
        self.user2 = self._create_and_return_user(username="******", password="******")
        self.user3 = self._create_and_return_user(username="******", password="******")
        self.user4 = self._create_and_return_user(username="******", password="******")
        self.guide_user1_draft = Guide(title = "What to do in Montreal!",
                                       location = "Montreal, Canada",
                                       description = "Description",
                                       owner = self.user1.get_profile(),
                                       is_draft = True)
        self.guide_user1_draft.save()
        self.tip_guide1 = Tip(title = "Old tip title",
                              location = "Old tip location",
                              description = "Old tip description",
                              guide = self.guide_user1_draft)
        self.tip_guide1.save()

    def tearDown(self):
        for user in User.objects.all():
            user.delete()

    def _create_and_return_user(self,
                                username,
                                password,
                                email = '*****@*****.**'):
        user = User.objects.create_user(username = username,
                                        password = password,
                                        email = email)
        user.save()
        return user

    # -------------------------------------------------------------------------
    #   Test 'delete_tip' view.
    # -------------------------------------------------------------------------
    def _test_delete_tip(self,
                         tip = None,
                         is_owner = None,
                         is_another_user = None,
                         is_collaborator = None,
                         is_anonymous = None,
                         expect_success = None):
        if is_owner == True:
            self.client.login(username = self.user1.username,
                              password = "******")
        elif is_collaborator == True:
            tip.guide.collaborators.add(self.user2.get_profile())
            self.client.login(username = self.user2.username,
                              password = "******")
        elif is_another_user == True:
            self.client.login(username = self.user2.username,
                              password = "******")
        elif is_anonymous == True:
            pass
        tip_id = tip.id
        guide = tip.guide
        delete_tip_url = reverse('apps.guides.views.delete_tip', kwargs={'tip_uuid': tip.uuid})
        edit_guide_url = reverse('apps.guides.views.edit_guide', kwargs={'slug': guide.slug})
        response = self.client.get(delete_tip_url, follow=True)
        response2 = self.client.post(delete_tip_url, follow=True)
        if expect_success == True:
            self.assertEqual(response2.request['PATH_INFO'], edit_guide_url)
            self.assertFalse(Tip.objects.filter(id=tip_id).exists())
        else:
            self.assertTrue(Tip.objects.filter(id=tip_id).exists())
    # -------------------------------------------------------------------------

    def test_owner_can_delete_tip(self):
        self._test_delete_tip(tip = self.tip_guide1,
                              is_owner = True,
                              expect_success = True)

    def test_another_user_cannot_delete_tip(self):
        self._test_delete_tip(tip = self.tip_guide1,
                              is_another_user = True,
                              expect_success = False)

    def test_collaborator_can_delete_tip(self):
        self._test_delete_tip(tip = self.tip_guide1,
                              is_collaborator = True,
                              expect_success = True)

    def test_anonymous_cannot_delete_tip(self):
        self._test_delete_tip(tip = self.tip_guide1,
                              is_anonymous = True,
                              expect_success = False)
Example #18
0
class TipModelTestCase(unittest.TestCase):

    def setUp(self):
        # --------------------------------------------------------------------
        #   All Tips need an owner Guide, which need an owner Account.
        # --------------------------------------------------------------------
        self.user = User.objects.create_user(username = '******',
                                             email = '*****@*****.**',
                                             password = '******')
        self.user.save()
        self.account = self.user.get_profile()

        self.guide = Guide(title = "What to do in Montreal!",
                           location = "Montreal, Canada",
                           owner = self.account,
                           is_draft = False)
        self.guide.save()

        self.draft_guide = Guide(title = "What to do in Montreal!",
                                 location = "Montreal, Canada",
                                 owner = self.account,
                                 is_draft = True)
        self.draft_guide.save()
        # --------------------------------------------------------------------

        cache.clear()

    def tearDown(self):
        for pk in [self.guide.pk, self.draft_guide.pk]:
            if Guide.objects.filter(pk__exact = self.guide.pk).exists():
                self.guide.delete()
        self.user.delete()

    def test_tip_creation(self):
        self.assertEqual(len(Tip.objects.all()), 0)
        self.assertEqual(len(self.guide.tip_set.all()), 0)
        tip = Tip(title = "Peel Pub",
                  location = "Peel Street, near McGill campus",
                  guide = self.guide)
        tip.save()
        self.assertEqual(len(Tip.objects.all()), 1)
        self.assertEqual(len(self.guide.tip_set.all()), 1)

    def test_guide_deletion_deletes_tip(self):
        tip = Tip(title = "Peel Pub",
                  location = "Peel Street, near McGill campus",
                  guide = self.guide)
        tip.save()
        self.assertTrue(Tip.objects.filter(pk__exact = tip.pk).exists())
        self.guide.delete()
        self.assertFalse(Tip.objects.filter(pk__exact = tip.pk).exists())

    def test_tip_add_remove_tags(self):
        tip = Tip(title = "Peel Pub",
                  location = "Peel Street, near McGill campus",
                  guide = self.guide)
        tip.save()
        self.assertEqual(tip.tags.count(), 0)
        tip.tags.add("drink", "pub", "student")
        self.assertEqual(tip.tags.count(), 3)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "drink").count(), 1)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "pub").count(), 1)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "student").count(), 1)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "museum").count(), 0)
        tip.tags.remove("drink", "pub")
        self.assertEqual(tip.tags.count(), 1)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "drink").count(), 0)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "pub").count(), 0)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "student").count(), 1)
        self.assertEqual(Tip.objects.filter(tags__name__exact = "museum").count(), 0)

    # -------------------------------------------------------------------------
    #   Account.has_favourited_tip. Tested here as the m2m field is
    #   Tip.favouritors.
    # -------------------------------------------------------------------------
    def _test_account_has_favourited_tip(self,
                                         is_public_guide = None,
                                         is_draft_guide = None,
                                         remove_favourited_tip = None,
                                         delete_tip = None,
                                         delete_guide = None,
                                         toggle_guide_visibility = None):
        assert(any(guide for guide in [is_public_guide, is_draft_guide]))
        if is_public_guide:
            tip = Tip(title = "Peel Pub",
                      location = "Peel Street, near McGill campus",
                      guide = self.guide)
        elif is_draft_guide:
            tip = Tip(title = "Peel Pub",
                      location = "Peel Street, near McGill campus",
                      guide = self.draft_guide)
        tip.save()
        account = tip.guide.owner
        print "adding tip"
        account.add_favourited_tip(tip)
        if is_public_guide:
            self.assertTrue(account.has_favourited_tip(tip))
        else:
            self.assertFalse(account.has_favourited_tip(tip))
        if remove_favourited_tip:
            print "removing tip"
            account.remove_favourited_tip(tip)
        elif delete_tip:
            print "deleting tip"
            tip.delete()
        elif delete_guide:
            print "deleting guide"
            tip.guide.delete()
        elif toggle_guide_visibility:
            print "toggling guide visibility"
            tip.guide.is_draft = not tip.guide.is_draft
            tip.guide.save()
        if is_public_guide:
            self.assertFalse(account.has_favourited_tip(tip))
        elif is_draft_guide and not toggle_guide_visibility:
            self.assertFalse(account.has_favourited_tip(tip))
        elif is_draft_guide and toggle_guide_visibility:
            self.assertTrue(account.has_favourited_tip(tip))

    def test_account_has_favourited_tip_after_removing_favourited_tip_for_public_guide(self):
        self._test_account_has_favourited_tip(is_public_guide = True,
                                              remove_favourited_tip = True)

    def test_account_has_favourited_tip_after_removing_favourited_tip_for_draft_guide(self):
        self._test_account_has_favourited_tip(is_draft_guide = True,
                                              remove_favourited_tip = True)

    def test_account_has_favourited_tip_after_deleting_tip_for_public_guide(self):
        self._test_account_has_favourited_tip(is_public_guide = True,
                                              delete_tip = True)

    def test_account_has_favourited_tip_after_deleting_tip_for_draft_guide(self):
        self._test_account_has_favourited_tip(is_draft_guide = True,
                                              delete_tip = True)

    def test_account_has_favourited_tip_after_deleting_guide_for_public_guide(self):
        self._test_account_has_favourited_tip(is_public_guide = True,
                                              delete_guide = True)

    def test_account_has_favourited_tip_after_deleting_guide_for_draft_guide(self):
        self._test_account_has_favourited_tip(is_draft_guide = True,
                                              delete_guide = True)

    def test_account_has_favourited_tip_after_toggling_guide_visibility_for_draft_guide(self):
        self._test_account_has_favourited_tip(is_draft_guide = True,
                                              toggle_guide_visibility = True)

    # -------------------------------------------------------------------------
    #   Account.favourite_tips. Tested here as the m2m field is
    #   Tip.favouritors.
    # -------------------------------------------------------------------------
    def _test_account_favourite_tips(self,
                                     is_public_guide = None,
                                     is_draft_guide = None,
                                     remove_favourited_tip = None,
                                     delete_tip = None,
                                     delete_guide = None,
                                     toggle_guide_visibility = None):
        assert(any(guide for guide in [is_public_guide, is_draft_guide]))
        if is_public_guide:
            tip = Tip(title = "Peel Pub",
                      location = "Peel Street, near McGill campus",
                      guide = self.guide)
        elif is_draft_guide:
            tip = Tip(title = "Peel Pub",
                      location = "Peel Street, near McGill campus",
                      guide = self.draft_guide)
        tip.save()
        account = tip.guide.owner
        print "adding tip"
        account.add_favourited_tip(tip)
        if is_public_guide:
            self.assertTrue(account.has_favourited_tip(tip))
        else:
            self.assertFalse(account.has_favourited_tip(tip))
        print "check favourite tips on account"
        if is_public_guide:
            self.assertTrue(tip in account.favourite_tips)
        else:
            self.assertFalse(tip in account.favourite_tips)
        if remove_favourited_tip:
            print "removing tip"
            account.remove_favourited_tip(tip)
        elif delete_tip:
            print "deleting tip"
            tip.delete()
        elif delete_guide:
            print "deleting guide"
            tip.guide.delete()
        elif toggle_guide_visibility:
            print "toggling guide visibility"
            tip.guide.is_draft = not tip.guide.is_draft
            tip.guide.save()
        if is_public_guide:
            self.assertFalse(tip in account.favourite_tips)
        elif is_draft_guide and not toggle_guide_visibility:
            self.assertFalse(tip in account.favourite_tips)
        elif is_draft_guide and toggle_guide_visibility:
            self.assertTrue(tip in account.favourite_tips)

    def test_account_favourite_tips_after_removing_favourited_tip_for_public_guide(self):
        self._test_account_favourite_tips(is_public_guide = True,
                                          remove_favourited_tip = True)

    def test_account_favourite_tips_after_removing_favourited_tip_for_draft_guide(self):
        self._test_account_favourite_tips(is_draft_guide = True,
                                          remove_favourited_tip = True)

    def test_account_favourite_tips_after_deleting_tip_for_public_guide(self):
        self._test_account_favourite_tips(is_public_guide = True,
                                          delete_tip = True)

    def test_account_favourite_tips_after_deleting_tip_for_draft_guide(self):
        self._test_account_favourite_tips(is_draft_guide = True,
                                          delete_tip = True)

    def test_account_favourite_tips_after_deleting_guide_for_public_guide(self):
        self._test_account_favourite_tips(is_public_guide = True,
                                          delete_guide = True)

    def test_account_favourite_tips_after_deleting_guide_for_draft_guide(self):
        self._test_account_favourite_tips(is_draft_guide = True,
                                          delete_guide = True)

    def test_account_favourite_tips_after_toggling_guide_visibility_for_draft_guide(self):
        self._test_account_favourite_tips(is_draft_guide = True,
                                          toggle_guide_visibility = True)
    # -------------------------------------------------------------------------

    # -------------------------------------------------------------------------
    #   Account.get_favourite_tag_generator. Tested here as the m2m field is
    #   Tip.favouritors. Recall this function returns TagWithURL objects.
    # -------------------------------------------------------------------------
    def _test_account_get_favourite_tag_generator(self,
                                                  is_public_guide = None,
                                                  is_draft_guide = None,
                                                  tip_before_tags = None,
                                                  remove_favourited_tip = None,
                                                  delete_tip = None,
                                                  delete_guide = None,
                                                  toggle_guide_visibility = None):
        assert(any(guide for guide in [is_public_guide, is_draft_guide]))
        if is_public_guide:
            tip = Tip(title = "Peel Pub",
                      location = "Peel Street, near McGill campus",
                      guide = self.guide)
        else:
            tip = Tip(title = "Peel Pub",
                      location = "Peel Street, near McGill campus",
                      guide = self.draft_guide)
        tip.save()
        account = tip.guide.owner
        if tip_before_tags == True:
            print "adding tip as favourite"
            account.add_favourited_tip(tip)
            print "add tags"
            tip.tags.add("pub", "student", "fun")
        else:
            print "add tags"
            tip.tags.add("pub", "student", "fun")
            print "adding tip as favourite"
            account.add_favourited_tip(tip)
        print "check favourite tags on account"
        if is_public_guide:
            self.assertTrue("pub" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertTrue("student" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertTrue("fun" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
        else:
            self.assertFalse("pub" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertFalse("student" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertFalse("fun" in [tag.tag.name for tag in account.get_favourite_tag_generator()])

        if remove_favourited_tip:
            print "removing tip"
            account.remove_favourited_tip(tip)
        elif delete_tip:
            print "deleting tip"
            tip.delete()
        elif delete_guide:
            print "deleting guide"
            self.guide.delete()
        elif toggle_guide_visibility:
            print "toggling guide visibility"
            tip.guide.is_draft = not tip.guide.is_draft
            tip.guide.save()
        if is_public_guide:
            self.assertFalse("pub" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertFalse("student" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertFalse("fun" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
        elif is_draft_guide and not toggle_guide_visibility:
            self.assertFalse("pub" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertFalse("student" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertFalse("fun" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
        elif is_draft_guide and toggle_guide_visibility:
            self.assertTrue("pub" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertTrue("student" in [tag.tag.name for tag in account.get_favourite_tag_generator()])
            self.assertTrue("fun" in [tag.tag.name for tag in account.get_favourite_tag_generator()])

    def test_account_get_favourite_tag_generator_tip_before_tags_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = True,
                                                       remove_favourited_tip = True)

    def test_account_get_favourite_tag_generator_tip_before_tags_for_public_guide(self):
        self._test_account_get_favourite_tag_generator(is_public_guide = True,
                                                       tip_before_tags = True,
                                                       remove_favourited_tip = True)

    def test_account_get_favourite_tag_generator_tags_before_tip_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = False,
                                                       remove_favourited_tip = True)

    def test_account_get_favourite_tag_generator_tags_before_tip_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_public_guide = True,
                                                       tip_before_tags = False,
                                                       remove_favourited_tip = True)

    def test_account_get_favourite_tag_generator_during_tip_deletion_tip_before_tags_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = True,
                                                       delete_tip = True)

    def test_account_get_favourite_tag_generator_during_tip_deletion_tip_before_tags_for_public_guide(self):
        self._test_account_get_favourite_tag_generator(is_public_guide = True,
                                                       tip_before_tags = True,
                                                       delete_tip = True)

    def test_account_get_favourite_tag_generator_during_tip_deletion_tags_before_tips_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = False,
                                                       delete_tip = True)

    def test_account_get_favourite_tag_generator_during_tip_deletion_tags_before_tips_for_public_guide(self):
        self._test_account_get_favourite_tag_generator(is_public_guide = True,
                                                       tip_before_tags = False,
                                                       delete_tip = True)

    def test_account_get_favourite_tag_generator_during_guide_deletion_tip_before_tags_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = True,
                                                       delete_guide = True)

    def test_account_get_favourite_tag_generator_during_guide_deletion_tip_before_tags_for_public_guide(self):
        self._test_account_get_favourite_tag_generator(is_public_guide = True,
                                                       tip_before_tags = True,
                                                       delete_guide = True)

    def test_account_get_favourite_tag_generator_during_guide_deletion_tags_before_tips_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = False,
                                                       delete_guide = True)

    def test_account_get_favourite_tag_generator_during_guide_deletion_tags_before_tips_for_public_guide(self):
        self._test_account_get_favourite_tag_generator(is_public_guide = True,
                                                       tip_before_tags = False,
                                                       delete_guide = True)

    def test_account_get_favourite_tag_generator_during_toggle_guide_visibility_tags_before_tips_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = False,
                                                       toggle_guide_visibility = True)

    def test_account_get_favourite_tag_generator_during_toggle_guide_visibility_tips_before_tags_for_draft_guide(self):
        self._test_account_get_favourite_tag_generator(is_draft_guide = True,
                                                       tip_before_tags = True,
                                                       toggle_guide_visibility = True)
    # -------------------------------------------------------------------------

    # -------------------------------------------------------------------------
    #   Test Tip.tags_as_list
    # -------------------------------------------------------------------------
    def test_tip_tags_as_list(self):
        tip = Tip(title = "Peel Pub",
                  location = "Peel Street, near McGill campus",
                  guide = self.guide)
        tip.save()
        self.assertEqual(len(tip.tags_as_list), 0)

        tip.tags.add("pub", "student", "fun")
        self.assertEqual(len(tip.tags_as_list), 3)
        self.assertTrue("pub" in [tag.tag for tag in tip.tags_as_list])
        self.assertTrue("student" in [tag.tag for tag in tip.tags_as_list])
        self.assertTrue("fun" in [tag.tag for tag in tip.tags_as_list])

        tip.tags.remove("pub")
        self.assertEqual(len(tip.tags_as_list), 2)
        self.assertFalse("pub" in [tag.tag for tag in tip.tags_as_list])
        self.assertTrue("student" in [tag.tag for tag in tip.tags_as_list])
        self.assertTrue("fun" in [tag.tag for tag in tip.tags_as_list])

        tip.tags.clear()
        self.assertEqual(len(tip.tags_as_list), 0)
    # -------------------------------------------------------------------------

    def test_image_add(self):
        tip = Tip(title = "Peel Pub",
                  location = "Peel Street, near McGill campus",
                  guide = self.guide)
        tip.save()
        self.assertEqual(len(tip.image_set.all()), 0)
        i = Image(description = "cute dog!", owner=self.account)
        i.tip = tip
        i.save()
        self.assertEqual(len(tip.image_set.all()), 1)
class ReadProfileLiveTestCase(LiveServerTestCase):
    fixtures = ['user-data.json']

    def setUp(self):
        for user in User.objects.all():
            user.delete()
            user.save()

        self.user1 = self._create_and_return_user(username="******", password="******")
        self.guide = Guide(title = "What to do in Montreal!",
                           location = "Montreal, Canada",
                           owner = self.user1.get_profile(),
                           is_draft = True)
        self.guide.save()

    @classmethod
    def setUpClass(cls):
        cls.selenium = webdriver.Chrome() #.Firefox()
        #cls.selenium = webdriver.Firefox()
        super(ReadProfileLiveTestCase, cls).setUpClass()

    @classmethod
    def tearDownClass(cls):
        super(ReadProfileLiveTestCase, cls).tearDownClass()
        cls.selenium.quit()

    def _selenium_wait(self):
        WebDriverWait(self.selenium, 5).until(
            lambda driver: driver.find_element_by_tag_name('body'))

    def _create_and_return_user(self,
                                username,
                                password,
                                email = '*****@*****.**'):
        user = User.objects.create_user(username = username,
                                        password = password,
                                        email = email)
        user.save()
        return user

    def test_read_profile_then_view_your_draft_guide(self):
        # --------------------------------------------------------------------
        #   Go to the root URI, then find any link called "Login" and click
        #   it.
        # --------------------------------------------------------------------
        self.selenium.get(self.live_server_url)
        self._selenium_wait()
        login_link = self.selenium.find_element_by_link_text("Login")
        login_link.click()
        self._selenium_wait()

        # --------------------------------------------------------------------
        #   Login.
        # --------------------------------------------------------------------
        for (field_name, field_contents) in [("username", self.user1.username),
                                             ("password", "password")]:
            input_element = self.selenium.find_element_by_name(field_name)
            input_element.send_keys(field_contents)
        self.selenium.find_element_by_xpath(".//*[@id='login_button']").click()
        self._selenium_wait()
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        #   -   Navigate to "Profile".
        #   -   Navigate to "Draft guides (X)".
        #   -   Click on the title of your guide.
        #   Find any link with the title of the guide and click it. Since
        #   the guide is a draft it is not visible to anyone except
        #   you.
        # --------------------------------------------------------------------
        for link_text in ["Profile", "Draft guides (1)", self.guide.title]:
            print "looking for link_text: %s" % link_text
            link = self.selenium.find_element_by_link_text(link_text)
            self.assertIsNotNone(link)
            link.click()
            self._selenium_wait()
        # --------------------------------------------------------------------

        # --------------------------------------------------------------------
        #   Confirm that we're looking at the guide.
        # --------------------------------------------------------------------
        heading = self.selenium.find_element_by_tag_name("h1")
        self.assertIsNotNone(heading)
        self.assertEquals(heading.text, self.guide.title)