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_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_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)
 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)
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")