Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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())
Ejemplo n.º 4
0
 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)
Ejemplo n.º 5
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)
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
    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)
Ejemplo n.º 10
0
 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()
Ejemplo n.º 11
0
    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()
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
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")
Ejemplo n.º 14
0
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)
Ejemplo n.º 15
0
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)
Ejemplo n.º 16
0
    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()])
Ejemplo n.º 17
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")