def test_no_self_notification(self): """ Test that emails are not sent to the person who created the sighting. """ user_1 = UserFactory.create() user_2 = UserFactory.create() user_3 = UserFactory.create() beer_1 = BeerFactory.create() user_1_watched_1 = WatchedBeerFactory.create(user=user_1, beer=beer_1) user_2_watched_1 = WatchedBeerFactory.create(user=user_2, beer=beer_1) beer_1_sighting = SightingFactory.create(user=user_1, beer=beer_1, venue=self.venue) mail.outbox = [] send_watchlist_email.delay(beer_1_sighting.id) self.assertEqual(len(mail.outbox), 1) for m in mail.outbox: self.assertEqual(m.to, [user_2.email])
def test_send_emails(self): """ Tests the celery task specifically. Testing that emails are sent when a sighting is created with no regard to the method will be done separately. """ user_1 = UserFactory.create() user_2 = UserFactory.create() user_3 = UserFactory.create() beer_1 = BeerFactory.create() beer_2 = BeerFactory.create() user_1_watched_1 = WatchedBeerFactory.create(user=user_1, beer=beer_1) user_2_watched_1 = WatchedBeerFactory.create(user=user_2, beer=beer_1) user_2_watched_1 = WatchedBeerFactory.create(user=user_2, beer=beer_2) beer_1_sighting = SightingFactory.create(user=user_3, beer=beer_1, venue=self.venue) beer_2_sighting = SightingFactory.create(user=user_3, beer=beer_2, venue=self.venue) mail.outbox = [] send_watchlist_email.delay(beer_1_sighting.id) self.assertEqual(len(mail.outbox), 2) recipients = [m.to for m in mail.outbox] self.assertItemsEqual(recipients, [[user_1.email], [user_2.email]]) mail.outbox = [] send_watchlist_email.delay(beer_2_sighting.id) self.assertEqual(len(mail.outbox), 1) for m in mail.outbox: self.assertEqual(m.to, [user_2.email]) user_1.send_watchlist_email = False user_1.save() mail.outbox = [] send_watchlist_email.delay(beer_1_sighting.id) self.assertEqual(len(mail.outbox), 1) for m in mail.outbox: self.assertEqual(m.to, [user_2.email])
def test_get(self): # to ensure that only the correct user's watched beers shows up user2 = UserFactory.create() user2_watched = WatchedBeerFactory.create(user=user2, beer=self.beer) self.client.login(username=self.user.username, password='******') watched_beer = WatchedBeerFactory.create(user=self.user, beer=self.beer) response = self.client.get('/api/watchlist/') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data['results']), 1) self.assertEqual(response.data['results'][0]['id'], watched_beer.id)
def test_delete(self): user2 = UserFactory.create() user2_watched = WatchedBeerFactory.create(user=user2, beer=self.beer) self.client.login(username=self.user.username, password='******') watched_beer = WatchedBeerFactory.create(user=self.user, beer=self.beer) response = self.client.delete('/api/watchlist/{0}/'.format(watched_beer.id)) self.assertEqual(response.status_code, 204) self.assertFalse(WatchedBeer.objects.filter(id=watched_beer.id).exists()) # now try to delete someone else's response = self.client.delete('/api/watchlist/{0}/'.format(user2_watched.id)) self.assertEqual(response.status_code, 404)
def setUp(self): self.venue = VenueFactory() self.user = UserFactory() self.user_2 = UserFactory.create() self.beer = BeerFactory() self.watched_beer = WatchedBeerFactory(user=self.user, beer=self.beer)