def test_redirects_to_visible_event(self): EventFactory.create_batch(4, visible=False) visible_event = EventFactory(visible=True) self.client.force_login(UserFactory()) response = self.client.get("/") self.assertRedirects( response, visible_event.get_absolute_url(), status_code=302 )
def test_cannot_view_hidden_event(self): event = EventFactory(visible=False) self.client.force_login(UserFactory()) response = self.client.get(event.get_absolute_url()) self.assertTemplateUsed(response, "jeugdraad/alert.html") self.assertEqual( response.context["message"], "You are not allowed to view this event" )
def test_typeahead_thumbprint_never(self): event = EventFactory() self.client.force_login(UserFactory()) response = self.client.get(event.get_absolute_url()) self.assertEqual( response.context["typeahead_thumbprint"], "never", "typeahead thumbprint is not correct", )
def test_only_hidden_events_with_perm(self, mock_has_perm): mock_has_perm.return_value = True event = EventFactory(visible=False) user = UserFactory() self.client.force_login(user) response = self.client.get("/") self.assertRedirects(response, event.get_absolute_url(), status_code=302) mock_has_perm.assert_any_call(user, "booking.view_event", event)
def test_view_event(self): event = EventFactory() self.client.force_login(UserFactory()) response = self.client.get(event.get_absolute_url()) self.assertEqual( response.context["current_event"], event, "the rendered event is not the requested event", ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed("booking/event/event.html")
def test_defaults_to_group_from_user(self): event = EventFactory() users = UserFactory.create_batch(5) self.client.force_login(users[2]) response = self.client.get(event.get_absolute_url(), follow=True) self.assertEqual( response.context["current_group"], users[2].group, "the rendered group is not the user's own group", ) self.assertTemplateUsed(response, "booking/event/event.html")
def test_delete_event_no_leftovers(self): event = EventFactory() booking = BookingFactory(game__event=event) self.assertEqual(Event.objects.count(), 1, "not one event was created") self.assertEqual(Game.objects.count(), 1, "not one game was created") self.assertEqual(Booking.objects.count(), 1, "not one booking was created") event.delete() self.assertEqual(Event.objects.count(), 0, "event was not deleted") self.assertEqual(Game.objects.count(), 0, "game was not deleted") self.assertEqual(Booking.objects.count(), 0, "booking was not deleted")
def test_typeahead_thumbprint_not_none(self): materials = MaterialFactory.create_batch(3) last_material = MaterialFactory() self.assertNotEqual(materials[0].last_modified, last_material.last_modified) event = EventFactory() self.client.force_login(UserFactory()) response = self.client.get(event.get_absolute_url()) self.assertEqual( response.context["typeahead_thumbprint"], last_material.last_modified.isoformat(), "typeahead thumbprint is not correct", )
def test_exception_if_user_has_no_group_and_no_perm(self): event = EventFactory() users = UserFactory.create_batch(5) user_without_group = UserFactory(group=None) self.client.force_login(user_without_group) response = self.client.get(event.get_absolute_url(), follow=True) self.assertTemplateUsed(response, "jeugdraad/alert.html") self.assertEqual( response.context["message"], "You are not assigned to a group. Please contact a board member to resolve " "this issue.", )
def test_get_context_data_not_empty(self): latest_event = EventFactory() other_event = EventFactory(event_end=latest_event.event_end - timedelta(5)) booking = BookingFactory(game__event=latest_event) # Creates a material request = RequestFactory().get("/non-existent") request.user = UserFactory() context = self.DummyView(request=request).get_context_data() self.assertEqual(list(context["events"]), [latest_event, other_event]) self.assertEqual(context["current_event"], latest_event) self.assertEqual( context["typeahead_thumbprint"], booking.material.last_modified.isoformat() )
def test_view_hidden_event_with_perm(self, mock_has_perm): mock_has_perm.return_value = True event = EventFactory(visible=False) user = UserFactory() self.client.force_login(user) response = self.client.get(event.get_absolute_url()) self.assertEqual( response.context["current_event"], event, "the rendered event is not the requested event", ) self.assertEqual(response.status_code, 200) self.assertTemplateUsed("booking/event/event.html") mock_has_perm.assert_any_call(user, "booking.view_event", event)
def test_defaults_to_all_if_user_has_no_group_and_perm(self, mock_has_perm): mock_has_perm.return_value = True event = EventFactory() users = UserFactory.create_batch(5) user_without_group = UserFactory(group=None) self.client.force_login(user_without_group) response = self.client.get(event.get_absolute_url(), follow=True) self.assertEqual( response.context["current_group"], None, "the rendered group is not 'all'" ) self.assertTemplateUsed(response, "booking/event/event.html") mock_has_perm.assert_any_call( user_without_group, "booking.view_others_groups_bookings", None )
def test_can_view_bookings_from_other_group_with_perm(self, mock_has_perm): mock_has_perm.return_value = True event = EventFactory() users = UserFactory.create_batch(2) self.client.force_login(users[0]) for group_slug, group in zip( ["all", users[1].group.slug], [None, users[1].group] ): response = self.client.get( reverse( "booking:event_games_group", kwargs={ "event_slug": event.slug, "group_slug": group_slug, }, ), follow=True, ) self.assertEqual( response.context["current_group"], group, "the rendered group is not the requested group", ) self.assertTemplateUsed(response, "booking/event/event.html") mock_has_perm.assert_any_call( users[0], "booking.view_others_groups_bookings", None )
def test_all_games_from_same_group(self, mock_has_perm): mock_has_perm.return_value = True event = EventFactory() user = UserFactory() other_group = GroupFactory() games_user_group = GameFactory.create_batch( 3, group=user.group, creator=user, event=event ) games_other_group = GameFactory.create_batch( 3, group=other_group, creator=user, event=event ) self.client.force_login(user) response = self.client.get( reverse( "booking:event_games_group", kwargs={ "event_slug": event.slug, "group_slug": user.group.slug, }, ), ) self.assertEqual(response.context["current_group"], user.group) for game in games_other_group: self.assertNotIn( game, response.context["games"][game.day][game.part_of_day], "game from other group is part of context", ) for game in games_user_group: self.assertIn( game, response.context["games"][game.day][game.part_of_day], "game from own group is not part of context", ) # Now check with reversed groups response = self.client.get( reverse( "booking:event_games_group", kwargs={ "event_slug": event.slug, "group_slug": other_group.slug, }, ), ) self.assertEqual(response.context["current_group"], other_group) for game in games_other_group: self.assertIn( game, response.context["games"][game.day][game.part_of_day], "game from other group is not part of context", ) for game in games_user_group: self.assertNotIn( game, response.context["games"][game.day][game.part_of_day], "game from own group is part of context", ) mock_has_perm.assert_any_call(user, "booking.view_others_groups_bookings", None)
def _get_sibling_games(n=5, part_of_day="AF", **kwargs): if "event" not in kwargs.keys(): kwargs["event"] = EventFactory() if "group" not in kwargs.keys(): kwargs["group"] = GroupFactory() return GameFactory.create_batch( n, day=kwargs["event"].event_start, part_of_day=part_of_day, **kwargs, )
def test_navigation_search_material_and_material_alias(self): self.browser.set_window_size(1024, 768) event = EventFactory() material = MaterialFactory() # Bob is a logged in user bob = UserFactory() self.create_pre_authenticated_session(bob) # Bob opens the event page self.browser.get(self.live_server_url + event.get_absolute_url()) self.check_if_typeahead_loaded() # Bob clicks the search bar and enters the name of the material search_input = self.browser.find_element(By.ID, "navSearch") search_input.send_keys(material.name) search_input.send_keys(Keys.ENTER) # Bob sees the details for the typed material catalog_view_page = CatalogViewPage(self) catalog_view_page.verify_material_attributes( self.browser.find_element(By.ID, "catalogModal"), material ) # Bob closes the modal by pressing escape ActionChains(self.browser).send_keys(Keys.ESCAPE).perform() # Bob clicks the search bar and enters the alias of the material search_input = self.browser.find_element(By.ID, "navSearch") for _ in range(len(material.name)): search_input.send_keys(Keys.BACK_SPACE) search_input.send_keys(str(material.aliases.first())) search_input.send_keys(Keys.ENTER) # Choose the suggestion and submit form # Bob sees the details for the typed material self.wait_for( lambda: catalog_view_page.verify_material_attributes( self.browser.find_element(By.ID, "catalogModal"), material ) )
def test_game_view(self): event = EventFactory() user = UserFactory() self.client.force_login(user) response = self.client.get( reverse( "booking:event_games_group", kwargs={"event_slug": event.slug, "group_slug": user.group.slug}, ), ) self.assertTemplateUsed(response, "booking/event/game-view.html") self.assertTemplateNotUsed(response, "booking/event/list-view.html")
def test_can_open_catalog_from_event_page(self): event = EventFactory() # Bob is a logged in user bob = UserFactory() self.create_pre_authenticated_session(bob) # Bob opens the event page self.browser.get(self.live_server_url + event.get_absolute_url()) # Bob finds the button for switching to the catalog self.wait_for( lambda: self.assertCSSElementExists(".nav-link[href='/catalog/']") ) # Bob switches to the catalog self.browser.find_element( By.CSS_SELECTOR, ".nav-link[href='/catalog/']" ).click() # Bob sees the catalog masonry self.wait_for( lambda: self.assertCSSElementExists( ".catalog-masonry", msg="no masonry with materials was found on the page", ) ) # Bob finds beschuiten (rol) on the page data_attr = f"/catalog/{self.material.pk}/modal" selector = f"[data-catalog-item='{data_attr}']" self.wait_for( lambda: self.assertCSSElementExists( selector, msg="link to catalog item was not found", ) )
def test_redirect_listview_group_all(self, mock_has_perm): mock_has_perm.return_value = True event = EventFactory() user = UserFactory() self.client.force_login(user) response = self.client.get( reverse( "booking:event_games_group", kwargs={"event_slug": event.slug, "group_slug": "all"}, ), follow=True, ) self.assertTemplateUsed(response, "booking/event/list-view.html") self.assertTemplateNotUsed(response, "booking/event/game-view.html") mock_has_perm.assert_any_call(user, "booking.view_others_groups_bookings", None)
def test_cannot_view_bookings_from_other_group(self): event = EventFactory() users = UserFactory.create_batch(2) self.client.force_login(users[0]) for group_slug in ["all", users[1].group.slug]: response = self.client.get( reverse( "booking:event_games_group", kwargs={"event_slug": event.slug, "group_slug": group_slug}, ), follow=True, ) # The view should default to the users own group self.assertEqual( response.context["current_group"], users[0].group, "the rendered group is not the user's own group", ) self.assertTemplateUsed(response, "booking/event/event.html")
def test_redirects_to_login(self): event = EventFactory() response = self.client.get("/") self.assertRedirects(response, reverse(settings.LOGIN_URL) + "?next=/")
def test_error_if_there_are_only_hidden_events(self): hidden_event = EventFactory(visible=False) self.client.force_login(UserFactory()) response = self.client.get("/") self.assertTemplateUsed(response, "jeugdraad/alert.html") self.assertEqual(response.context["message"], "There are no open events.")
def test_redirects_to_latest_event(self): EventFactory.create_batch(4, event_end=now() + timedelta(days=24)) latest_event = EventFactory(event_end=now() + timedelta(days=50)) self.client.force_login(UserFactory()) response = self.client.get("/") self.assertRedirects(response, latest_event.get_absolute_url(), status_code=302)
def test_events_in_context(self): material = MaterialFactory() event = EventFactory() self.client.force_login(UserFactory()) response = self.client.get(material.get_absolute_url()) self.assertEqual(list(response.context["events"]), [event])
def test_redirects_to_event(self): # There is only one event event = EventFactory() self.client.force_login(UserFactory()) response = self.client.get("/") self.assertRedirects(response, event.get_absolute_url(), status_code=302)
def setUp(self): super().setUp() self.active_event = EventFactory() self.materials = MaterialFactory.create_batch(size=10) MaterialFactory(name="beschuiten (rol)") MaterialFactory(name="EHBO doos")