def test_remove_user_from_list_as_participant(self):
        self.client.login(username=self.participant1.username, password=self.password)
        # Remove owner of shopping list
        remove_user_url = reverse('remove-user-from-shopping-list', args=[str(self.shopping_list1.id), self.owner])
        response = self.client.post(remove_user_url, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertRedirects(response, self.detail_shopping_list1_url)
        # Check if owner has not been removed from the shopping list
        bool_is_removed = self.owner != self.shopping_list1.owner and self.shopping_list1 not in ShoppingList.get_user_shopping_lists(
            self.owner)
        self.assertFalse(bool_is_removed)

        # Remove admin
        remove_user_url = reverse('remove-user-from-shopping-list', args=[str(self.shopping_list1.id), self.admin])
        response = self.client.post(remove_user_url, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertRedirects(response, self.detail_shopping_list1_url)
        # Check if admin has been removed from the shopping list
        bool_is_removed = (self.admin not in self.shopping_list1.admins.all()) and (
                    self.shopping_list1 not in ShoppingList.get_user_shopping_lists(self.admin))
        self.assertFalse(bool_is_removed)

        # Leave shopping list
        remove_user_url = reverse('remove-user-from-shopping-list',
                                  args=[str(self.shopping_list1.id), self.participant1])
        response = self.client.post(remove_user_url, follow=True)
        self.assertEqual(response.status_code, 200)
        self.assertRedirects(response, self.index_url)
        # Check if participant1 has been removed from the shopping list
        bool_is_removed = (self.participant1 not in self.shopping_list1.participants.all()) and (
                    self.shopping_list1 not in ShoppingList.get_user_shopping_lists(self.participant1))
        self.assertTrue(bool_is_removed)
    def test_change_owner_as_owner(self):
        # As owner of shopping list, transfer ownership to participant
        change_owner_url = reverse('change-owner', args=[str(self.shopping_list1.id), self.participant1])
        response = self.client.post(change_owner_url)
        # Check redirection of page and status code
        self.assertEquals(response.status_code, 302)
        self.assertRedirects(response, self.detail_shopping_list1_url)
        # Check if ownership has been transferred
        bool_owner_is_removed = (self.shopping_list1 not in ShoppingList.get_user_shopping_lists(self.owner)) \
                                and (self.shopping_list1 not in ShoppingList.objects.filter(owner=self.owner))
        bool_participant1_is_owner = (self.shopping_list1 in ShoppingList.get_user_shopping_lists(self.participant1)) \
                              and (self.shopping_list1 in ShoppingList.objects.filter(owner=self.participant1)) \
                              and (self.participant1 not in self.shopping_list1.participants.all())
        self.assertFalse(bool_owner_is_removed)
        self.assertFalse(bool_participant1_is_owner)

        # As owner of shopping list (self.owner), transfer ownership to self.admin
        change_owner_url = reverse('change-owner', args=[str(self.shopping_list1.id), self.admin])
        response = self.client.post(change_owner_url)
        # Check redirection of page and status code
        self.assertEquals(response.status_code, 302)
        self.assertRedirects(response, self.index_url)
        # Check if ownership has been transferred
        bool_owner_is_removed = (self.shopping_list1 not in ShoppingList.get_user_shopping_lists(self.owner)) \
                                and (self.shopping_list1 not in ShoppingList.objects.filter(owner=self.owner))
        bool_admin_is_owner = (self.shopping_list1 in ShoppingList.get_user_shopping_lists(self.admin)) \
                              and (self.shopping_list1 in ShoppingList.objects.filter(owner=self.admin)) \
                              and (self.admin not in self.shopping_list1.admins.all())
        self.assertTrue(bool_owner_is_removed)
        self.assertTrue(bool_admin_is_owner)
 def test_change_owner_as_outsider(self):
     self.client.login(username=self.outsider.username, password=self.password)
     # Change owner
     change_owner_url = reverse('change-owner', args=[str(self.shopping_list1.id), self.admin])
     response = self.client.post(change_owner_url, follow=True)
     # Check status code
     self.assertEquals(response.status_code, 200)
     # Check if ownership has not been transferred
     bool_owner_is_removed = (self.shopping_list1 not in ShoppingList.get_user_shopping_lists(self.owner)) \
                             and (self.shopping_list1 not in ShoppingList.objects.filter(owner=self.owner))
     bool_admin_is_owner = (self.shopping_list1 in ShoppingList.get_user_shopping_lists(self.admin)) \
                           and (self.shopping_list1 in ShoppingList.objects.filter(owner=self.admin)) \
                           and (self.admin not in self.shopping_list1.admins.all())
     self.assertFalse(bool_owner_is_removed)
     self.assertFalse(bool_admin_is_owner)
Esempio n. 4
0
    def test_get_user_shopping_lists(self):
        # Create shopping list where self.owner is participant
        shopping_list = ShoppingList.objects.create(title='en tittel',
                                                    owner=self.participant)
        shopping_list.participants.add(self.owner)
        # Create shopping list where self.owner is admin
        shopping_list2 = ShoppingList.objects.create(title='en tittel',
                                                     owner=self.participant)
        shopping_list2.admins.add(self.owner)

        owned_shopping_lists = ShoppingList.objects.filter(owner=self.owner)
        other_shopping_lists = ShoppingList.objects.filter(
            participants=self.owner)
        other2_shopping_lists = ShoppingList.objects.filter(admins=self.owner)
        my_shopping_lists = other_shopping_lists | owned_shopping_lists | other2_shopping_lists

        self.assertQuerysetEqual(my_shopping_lists.distinct().order_by('id'),
                                 ShoppingList.get_user_shopping_lists(
                                     self.owner),
                                 transform=lambda x: x)