def test_get_or_invite_users(self): """ User.objects.test_get_or_invite_users() """ # 4 email addresses emails = [fake.email() for _ in range(4)] # Turn the first 2 into real users to test the "get" functionality ActiveUserFactory(email=emails[0]) ActiveUserFactory(email=emails[1]) # Create fake request inviter = ActiveUserFactory() request = RequestFactory().get('/') request.user = inviter # Call function users = get_user_model().objects.get_or_invite_users(emails, request) # Test that each user is in the results for user in users: self.assertIn(user.email, emails) # Ensure that 2 emails were sent self.assertEqual(len(mail.outbox), 2) # Test the subject line of the first email self.assertTrue( mail.outbox[0].subject.endswith( ' invited you to join GardenHub' ))
def test_get_orders(self): """ user.get_orders() """ user = ActiveUserFactory() orders = OrderFactory.create_batch( 5, start_date=localdate(2017, 1, 1), end_date=localdate(2017, 1, 5), plot__gardeners=[user] ) self.assertEqual(set(user.get_orders()), set(orders))
def test_email_user(self): """ user.email_user() """ user = ActiveUserFactory() user.email_user( "Hello, user!", "This is a beautiful test. The best test.") # Ensure that 1 email was sent self.assertEqual(len(mail.outbox), 1) # Test the subject line of the email self.assertEqual(mail.outbox[0].subject, 'Hello, user!')
def test_is_picker(self): """ user.is_picker() """ # Test a normal user self.assertFalse(ActiveUserFactory().is_anything()) # Create garden and assign a picker garden = GardenFactory() picker = ActiveUserFactory() garden.pickers.add(picker) # Test that the user is a picker self.assertTrue(picker.is_picker())
def test_is_order_picker(self): """ user.is_order_picker() """ picker = PickerFactory() plot = PlotFactory(garden=Garden.objects.get(pickers__id=picker.id)) order = OrderFactory(plot=plot) self.assertTrue(picker.is_order_picker(order)) self.assertFalse(ActiveUserFactory().is_order_picker(order))
def test_access(self): """ Only GM's can access the page """ client = Client() # Normal users can't access client.force_login(ActiveUserFactory()) response = client.get(reverse('plot-create')) self.assertEqual(response.status_code, 302)
def test_is_garden_manager(self): """ user.is_garden_manager() """ # Test a garden manager self.assertTrue(GardenManagerFactory().is_garden_manager()) # Test *not* garden managers self.assertFalse(GardenerFactory().is_garden_manager()) self.assertFalse(ActiveUserFactory().is_garden_manager())
def test_is_anything(self): """ user.is_anything() """ # Test a normal user self.assertFalse(ActiveUserFactory().is_anything()) # Test a gardener and garden manager self.assertTrue(GardenerFactory().is_anything()) self.assertTrue(GardenManagerFactory().is_anything())
def test_inquirers(self): """ pick.inquirers() """ picker = ActiveUserFactory() requester = ActiveUserFactory() gardeners = ActiveUserFactory.create_batch(2) garden = GardenFactory() plot = PlotFactory(garden=garden) plot.gardeners.add(gardeners[0]) plot.gardeners.add(gardeners[1]) Order.objects.create( plot=plot, start_date=today()-timedelta(days=5), end_date=today()+timedelta(days=5), requester=requester ) pick = Pick.objects.create(picker=picker, plot=plot) self.assertIn(gardeners[0], list(pick.inquirers())) self.assertIn(gardeners[1], list(pick.inquirers())) self.assertIn(requester, list(pick.inquirers()))
def test_is_gardener(self): """ user.is_gardener() """ # Test a gardener of a single plot self.assertTrue(GardenerFactory().is_gardener()) # Test that a garden manager is also considered a gardener # if the garden has plots manager = GardenManagerFactory() garden = manager.get_gardens().first() PlotFactory.create_batch(3, garden=garden) self.assertTrue(manager.is_gardener()) # Create and test a *not* gardener self.assertFalse(ActiveUserFactory().is_gardener())
def test_has_orders(self): """ user.has_orders() """ # Create plot garden = GardenFactory() plot = PlotFactory(garden=garden) # Create orders requester = ActiveUserFactory() OrderFactory.create_batch( 5, plot=plot, requester=requester ) # Create user and assign it to the plot user = ActiveUserFactory() plot.gardeners.add(user) # Test orders self.assertTrue(user.has_orders()) self.assertFalse(ActiveUserFactory().has_orders())
def test_can_edit_garden(self): """ user.can_edit_garden() """ # Garden manager can edit the garden manager = GardenManagerFactory() garden = manager.get_gardens().first() self.assertTrue(manager.can_edit_garden(garden)) # Gardener can't edit the garden gardener = GardenerFactory() garden = gardener.get_plots().first().garden self.assertFalse(gardener.can_edit_garden(garden)) # Normal user can't edit the garden self.assertFalse(ActiveUserFactory().can_edit_garden(GardenFactory()))
def test_get_picker_orders(self): """ user.get_picker_orders() """ picker = PickerFactory() plot = PlotFactory(garden=picker.get_picker_gardens().first()) # Create orders orders = OrderFactory.create_batch( 3, plot=plot, start_date=localdate(2017, 1, 1), end_date=localdate(2017, 1, 5), requester=ActiveUserFactory() ) self.assertEqual(set(picker.get_picker_orders()), set(orders)) self.assertEqual(picker.get_picker_orders().count(), 3)
def test_can_edit_plot(self): """ user.can_edit_plot() """ # Gardener can edit the plot gardener = GardenerFactory() plot = gardener.get_plots().first() self.assertTrue(gardener.can_edit_plot(plot)) # Garden manager can edit the plot manager = GardenManagerFactory() garden = manager.get_gardens().first() plot = PlotFactory(garden=garden) self.assertTrue(manager.can_edit_plot(plot)) # Normal user can't edit the plot self.assertFalse(ActiveUserFactory().can_edit_plot(PlotFactory()))
def test_can_edit_order(self): """ user.can_edit_order() """ # Gardener can edit the order gardener = GardenerFactory() order = OrderFactory(plot=gardener.get_plots().first()) self.assertTrue(gardener.can_edit_order(order)) # Garden manager can edit the order manager = GardenManagerFactory() garden = manager.get_gardens().first() plot = PlotFactory(garden=garden) order = OrderFactory(plot=plot) self.assertTrue(manager.can_edit_order(order)) # Normal user can't edit the order self.assertFalse(ActiveUserFactory().can_edit_order(OrderFactory()))
def test_get_peers(self): """ user.get_peers() """ # Create test Gardens gardens = GardenFactory.create_batch(4) # Create test Plots plots = [ PlotFactory(garden=gardens[0]), PlotFactory(garden=gardens[2]), PlotFactory(garden=gardens[3]), PlotFactory(garden=gardens[3]) ] # Create test Users users = ActiveUserFactory.create_batch(10) # Garden with 2 managers and 1 plot with 1 gardener gardens[0].managers.add(users[0], users[1]) plots[0].gardeners.add(users[2]) self.assertEqual(set(users[0].get_peers()), set([users[1], users[2]])) self.assertEqual(set(users[1].get_peers()), set([users[0], users[2]])) self.assertEqual(set(users[2].get_peers()), set([])) # Garden with 2 managers and no plots gardens[1].managers.add(users[3], users[4]) self.assertEqual(set(users[3].get_peers()), set([users[4]])) self.assertEqual(set(users[4].get_peers()), set([users[3]])) # Garden with 0 managers and 1 plot with 2 gardeners gardens[2].managers.add(users[5], users[6]) self.assertEqual(set(users[5].get_peers()), set([users[6]])) self.assertEqual(set(users[6].get_peers()), set([users[5]])) # Garden with 1 manager and 2 plots, each with 1 gardener gardens[3].managers.add(users[7]) plots[2].gardeners.add(users[8]) plots[3].gardeners.add(users[9]) self.assertEqual(set(users[7].get_peers()), set([users[8], users[9]])) self.assertEqual(set(users[8].get_peers()), set([])) self.assertEqual(set(users[9].get_peers()), set([]))
def test_set_gardeners(self): """ Test setting gardeners """ gardeners = [ActiveUserFactory()] plot = PlotFactory(gardeners=gardeners) self.assertEqual(set(gardeners), set(plot.gardeners.all()))
def test_set_pickers(self): """ Test setting pickers """ pickers = [ActiveUserFactory()] garden = GardenFactory(pickers=pickers) self.assertEqual(set(pickers), set(garden.pickers.all()))
def test_set_managers(self): """ Test setting managers """ managers = [ActiveUserFactory()] garden = GardenFactory(managers=managers) self.assertEqual(set(managers), set(garden.managers.all()))
def test_create_user(self): """ Create a user """ user = ActiveUserFactory() self.assertIn(user, get_user_model().objects.all())
def test_get_short_name(self): """ user.get_short_name() """ user = ActiveUserFactory(first_name="Ada", last_name="Lovelace") self.assertEqual(user.get_short_name(), "Ada")
def test_get_gardens(self): """ user.get_gardens() """ user = ActiveUserFactory() gardens = GardenFactory.create_batch(4, managers=[user]) self.assertEqual(set(user.get_gardens()), set(gardens))
def test_get_plots(self): """ user.get_plots() """ # Exclusively a gardener on a plot gardener = ActiveUserFactory() plot = PlotFactory(gardeners=[gardener]) self.assertEqual(set([plot]), set(gardener.get_plots())) # Exclusively a manager on a garden manager = ActiveUserFactory() garden = GardenFactory(managers=[manager]) plots = PlotFactory.create_batch(5, garden=garden) self.assertEqual(set(plots), set(manager.get_plots())) # Neither a gardener on a plot nor a manager on a garden nobody = ActiveUserFactory() self.assertEqual(nobody.get_plots().count(), 0) # Both a gardener on a plot and a manager on a garden godlike = ActiveUserFactory() garden = GardenFactory(managers=[godlike]) plots = PlotFactory.create_batch(5, garden=garden) plot = PlotFactory(gardeners=[godlike]) self.assertEqual(set(plots + [plot]), set(godlike.get_plots()))
def test_create(self): """ Test creation """ self.assertTrue(ActiveUserFactory())
def test_is_active(self): """ Ensure the user is active """ self.assertTrue(ActiveUserFactory().is_active)
def test_get_picker_gardens(self): """ user.get_picker_gardens() """ picker = ActiveUserFactory() gardens = GardenFactory.create_batch(4, pickers=[picker]) GardenFactory.create_batch(3) self.assertEqual(set(picker.get_picker_gardens()), set(gardens))