def test_filtered_bookings_not_in_context(self): game = GameFactory() bookings_gm = BookingFactory.create_batch(3, game=game, material__gm=True) bookings_no_gm = BookingFactory.create_batch(2, game=game, material__gm=False) self.client.force_login(game.creator) response = self.client.get( reverse( "booking:event_list_group", kwargs={ "event_slug": game.event.slug, "group_slug": game.creator.group.slug, }, ) + "?gm=True", ) # There are no ListViewFilters, hence all bookings should be part of the # leftover ListView. lv, lv_bookings = response.context["list_views"][game.day][ game.part_of_day][0] for booking in bookings_gm: self.assertIn(booking, lv_bookings, "filtered booking is not in listview") for booking in bookings_no_gm: self.assertNotIn(booking, lv_bookings, "excluded booking is part of listview") self.assertTemplateUsed(response, "booking/event/list-view.html")
def test_bookings_keep_belonging_to_original_group_if_user_moves_group( self): user = UserFactory() booking = BookingFactory(requester=user, game__creator=user) original_group = user.group self.assertEqual(booking.group, original_group) new_group = GroupFactory() user.group = new_group booking.refresh_from_db() self.assertEqual(booking.group, original_group)
def test_delete_material_with_bookings(self): material = MaterialFactory() material_name = material.name booking = BookingFactory(material=material) material.delete() booking.refresh_from_db() self.assertEqual(Material.objects.count(), 0, "material was not deleted") self.assertEqual(Booking.objects.count(), 1, "booking was deleted") self.assertEqual(booking.material, None, "booking still refers to material") self.assertEqual( booking.custom_material, material_name, "booking does not have material name", )
def test_can_view_material_details_modal_game_view(self): # Tests the material details modal from the booking game page. booking = BookingFactory(material__categories=[CategoryFactory()]) # Bob is a logged in user bob = booking.requester self.create_pre_authenticated_session(bob) # Bob opens the event page self.browser.get(self.live_server_url + booking.game.event.get_absolute_url()) # Bob finds the preloaded booking game_view_page = GameViewPage(self) booking_id = game_view_page.get_newest_booking_id() self.assertEqual(booking_id, booking.pk) # Bob clicks on the material name in the booking game_view_page.open_catalog_modal(booking_id) # Bob sees the details for the booked material catalog_view_page = CatalogViewPage(self) catalog_view_page.verify_material_attributes( game_view_page.get_catalog_modal(), booking.material ) # Bob clicks the category in the modal... game_view_page.get_catalog_modal().find_elements( By.CSS_SELECTOR, ".category-list a" )[0].click() # ... to show the material in the catalog self.wait_for( lambda: self.assertCSSElementExists( ".catalog-masonry", msg="no masonry with materials was found on the page", ) ) # Bob clicks the category filter that is active to show all materials self.browser.find_element( By.CSS_SELECTOR, "button[name=categories].btn-secondary" ).click() # Bob finds beschuiten (rol) on the page as well data_attr = f"/catalog/{self.material.pk}/modal" selector = f"[data-catalog-item='{data_attr}']" self.wait_for( lambda: self.assertCSSElementExists( selector, "material was not found", ) ) # Bob inspects the properties of the beschuiten (rol) self.browser.find_element(By.CSS_SELECTOR, selector).click() catalog_view_page.verify_material_attributes( game_view_page.get_catalog_modal(), self.material )
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_exclude_gm(self): _ = BookingFactory.create_batch(20) _filter = ListViewFilterFactory(gm=False) bookings_qs = Booking.objects.all() in_set, out_set = _filter.filter_bookings(bookings_qs) self.assertEqual( in_set.count() + out_set.count(), 20, "not all bookings are part of output" ) for booking in in_set: self.assertEqual(booking.material.gm, False) for booking in out_set: self.assertEqual(booking.material.gm, True)
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 create_bookings_with_set_of_categories(categories, n=20): """ Creates bookings and assigns their materials randomly to one or more of the supplied categories. :param int n: number of bookings to create :param Category[] categories: categories to assign to the bookings :return: Booking[] """ bookings = BookingFactory.create_batch(n) for category in categories: num = random.randint(0, n) for booking in random.sample(bookings, num): booking.material.categories.add(category) return bookings
def test_bookings_from_different_groups_combined_in_group_all( self, mock_has_perm): mock_has_perm.return_value = True # Create two bookings and games at the same moment and for the same event, but # with a different group. booking1 = BookingFactory() booking2 = BookingFactory( game__event=booking1.game.event, game__day=booking1.game.day, game__part_of_day=booking1.game.part_of_day, ) self.assertNotEqual(booking1.game.group, booking2.game.group) bookings = [booking1, booking2] user = UserFactory() self.client.force_login(user) response = self.client.get( reverse( "booking:event_list_group", kwargs={ "event_slug": booking1.game.event.slug, "group_slug": "all", }, )) # There are no ListViewFilters, hence all bookings should be part of the # leftover ListView. lv, lv_bookings = response.context["list_views"][booking1.game.day][ booking1.game.part_of_day][0] self.assertCountEqual( lv_bookings, bookings, "bookings for different groups are not combined in the list view", ) self.assertTemplateUsed(response, "booking/event/list-view.html") mock_has_perm.assert_any_call(user, "booking.view_others_groups_bookings", None)
def test_cannot_export_excel_not_allowed(self): other_group = GroupFactory() game = GameFactory(group=other_group) bookings = BookingFactory.create_batch(5, game=game) self.client.force_login(UserFactory()) # not from 'other_group' response = self.client.get( reverse( "booking:api:excel_event", kwargs={ "event_slug": game.event.slug, "group_slug": game.creator.group.slug, }, ) ) self.assertEqual(response.status_code, 403)
def test_can_export_excel(self): game = GameFactory() bookings = BookingFactory.create_batch(5, game=game) self.client.force_login(game.creator) response = self.client.get( reverse( "booking:api:excel_event", kwargs={ "event_slug": game.event.slug, "group_slug": game.creator.group.slug, }, ) ) self.assertEqual( response["content-type"], "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", )
def test_lists_and_bookings_in_context(self): game = GameFactory() bookings = BookingFactory.create_batch(5, game=game) self.client.force_login(game.creator) response = self.client.get( reverse( "booking:event_list_group", kwargs={ "event_slug": game.event.slug, "group_slug": game.creator.group.slug, }, ), ) # There are no ListViewFilters, hence all bookings should be part of the # leftover ListView. lv, lv_bookings = response.context["list_views"][game.day][ game.part_of_day][0] self.assertCountEqual( bookings, lv_bookings, "not all bookings are part of the leftover listview") self.assertTemplateUsed(response, "booking/event/list-view.html")
def test_can_view_material_details_modal_list_view(self): # Test the material details model from the booking list page. booking = BookingFactory(material__categories=[CategoryFactory()]) # Bob is a logged in user bob = booking.requester self.create_pre_authenticated_session(bob) # Bob opens the event page self.browser.get(self.live_server_url + booking.game.event.get_absolute_url()) # Bob switches to the list view game_view_page = GameViewPage(self) game_view_page.switch_to_list_view() # Bob finds the preloaded booking list_view_page = ListViewPage(self) booking_id = list_view_page.get_newest_booking_id() self.assertEqual(booking_id, booking.pk) # Bob clicks on the material name in the booking game_view_page.open_catalog_modal(booking_id) # Bob sees the details for the booked material catalog_view_page = CatalogViewPage(self) catalog_view_page.verify_material_attributes( list_view_page.get_catalog_modal(), booking.material ) # Bob clicks the category in the modal... list_view_page.get_catalog_modal().find_elements( By.CSS_SELECTOR, ".category-list a" )[0].click() # ... to show the material in the catalog self.wait_for( lambda: self.assertCSSElementExists( ".catalog-masonry", msg="no masonry with materials was found on the page", ) )
def test_delete_user_with_bookings_keeps_bookings(self): # User 1 and 2 are from the same group # User 3 is from a different group user = UserFactory() booking = BookingFactory(requester=user, game__creator=user) group = user.group user2 = UserFactory(group=group) booking2 = BookingFactory(requester=user2, game__creator=user2) user3 = UserFactory() booking3 = BookingFactory(requester=user3, game__creator=user3) self.assertEqual( group, booking.requester.group, "booking1 not associated with requester group", ) self.assertEqual( group, booking2.requester.group, "booking2 not associated with requester group", ) self.assertNotEqual( booking.requester, booking2.requester, "bookings are associated with same requester", ) user.delete() booking.refresh_from_db() user2.delete() booking2.refresh_from_db() user3.delete() booking3.refresh_from_db() # User 1 and 2 should be replaced by the same sentinel user # User 3 should be replaced by another sentinel user # User count should be 2 now as user 1 and 2 are 'merged'. self.assertEqual( get_user_model().objects.count(), 2, "users were not replaced", ) self.assertEqual(Booking.objects.count(), 3, "booking was deleted") self.assertEqual(group, booking.requester.group, "booking1 changed group") self.assertEqual(group, booking2.requester.group, "booking2 changed group") self.assertEqual( booking.requester.email, f"deleted_user@{group}", "requester was not replaced with sentinel user", ) self.assertEqual( booking2.requester.email, f"deleted_user@{group}", "requester was not replaced with sentinel user", ) self.assertEqual( booking.requester, booking2.requester, "different sentinel users were used for the same group", ) self.assertNotEqual( booking.requester, booking3.requester, "same sentinel user was used for different groups", )