def test_user_cannot_lock_room_without_joining(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) data = {"user": self.normal_1.pk} response = self.client.post(self.url_1, data) self.room_1.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_user_cannot_leave_nonexisting_room(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) url = reverse("rooms_api_leave", kwargs={"version": "v1", "pk": 0}) data = {"user": self.normal_1.pk} response = self.client.post(url, data) self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_staff_can_add_user_to_hidden_room(self): self.client.force_authenticate(user=self.staff_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) data = {"user": self.normal_1.pk} response = self.client.post(self.url_3, data) self.room_3.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertJoined(self.normal_1, self.room_3)
def test_user_cannot_join_before_rooming_starts(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.zosia.rooming_start = timedelta_since_now(days=7) self.zosia.save() data = {"user": self.normal_1.pk} response = self.client.post(self.url_1, data) self.room_1.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_staff_can_add_user_to_room_after_rooming_ends(self): self.client.force_authenticate(user=self.staff_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.zosia.rooming_end = timedelta_since_now(days=-7) self.zosia.save() data = {"user": self.normal_1.pk} response = self.client.post(self.url_1, data) self.room_1.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertJoined(self.normal_1, self.room_1)
def test_user_can_join_room_when_available_place(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.room_2.join(self.normal_2) data = {"user": self.normal_1.pk} response = self.client.post(self.url_2, data) self.room_2.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertJoined(self.normal_2, self.room_2) room_assertions.assertJoined(self.normal_1, self.room_2)
def test_user_cannot_remove_other_user_from_room(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_2, self.zosia, payment_accepted=True) self.room_2.join(self.normal_1) self.room_2.join(self.normal_2) data = {"user": self.normal_2.pk} response = self.client.post(self.url_2, data) self.room_2.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)
def test_user_can_leave_joined_room(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.room_1.join(self.normal_1) data = {"user": self.normal_1.pk} response = self.client.post(self.url_1, data) self.room_1.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertEmpty(self.room_1)
def test_staff_can_unlock_room(self): self.client.force_authenticate(user=self.staff_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.room_1.join(self.normal_1) self.room_1.set_lock(self.normal_1) response = self.client.post(self.url_1, {}) self.room_1.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertFalse(self.room_1.is_locked)
def test_user_cannot_unlock_not_owned_room(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.room_2.join(self.normal_2) self.room_2.set_lock(self.normal_2) self.room_2.join(self.normal_1, password=self.room_2.lock.password) response = self.client.post(self.url_2, {}) self.room_2.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) room_assertions.assertLocked(self.room_2, self.normal_2)
def test_owner_can_leave_locked_room_then_unlocks(self): self.client.force_authenticate(user=self.normal_2) create_user_preferences(self.normal_2, self.zosia, payment_accepted=True) self.room_2.join(self.normal_2) self.room_2.set_lock(self.normal_2) data = {"user": self.normal_2.pk} response = self.client.post(self.url_2, data) self.room_2.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertEmpty(self.room_2) room_assertions.assertUnlocked(self.room_2)
def setUp(self): super().setUp() self.user_prefs = create_user_preferences(self.normal, self.zosia, contact='foo') self.url = reverse('user_preferences_edit', kwargs={'pk': self.user_prefs.pk})
def test_user_can_lock_room_after_joining(self): self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.room_1.join(self.normal_1) data = {"user": self.normal_1.pk} response = self.client.post(self.url_1, data) self.room_1.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertLocked(self.room_1, self.normal_1) self.assertEqual(response.data["lock"]["user"]["last_name"], "lennon") self.assertIsNotNone(response.data["lock"]["password"])
def test_staff_can_lock_room_with_expiration_date(self): self.client.force_authenticate(user=self.staff_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) self.room_1.join(self.normal_1) expiration_date = timedelta_since_now(days=1) data = {"user": self.normal_1.pk, "expiration_date": expiration_date} response = self.client.post(self.url_1, data) self.room_1.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertLocked(self.room_1, self.normal_1) self.assertEqual(self.room_1.lock.expiration_date, expiration_date)
def test_post_user_already_registered(self): create_user_preferences(self.normal, self.zosia) self.assertEqual( UserPreferences.objects.filter(user=self.normal).count(), 1) self.client.login(email="*****@*****.**", password="******") response = self.client.post(self.url, data={ 'contact': 'fb: me', 'shirt_size': 'S', 'shirt_type': 'f', 'terms_accepted': True }, follow=True) self.assertEqual(response.status_code, 200) self.assertEqual( UserPreferences.objects.filter(user=self.normal).count(), 1)
def test_user_can_unlock_after_rooming_ends(self): self.client.force_authenticate(user=self.normal_2) create_user_preferences(self.normal_2, self.zosia, payment_accepted=True) self.room_2.join(self.normal_2) self.room_2.set_lock(self.normal_2) self.zosia.rooming_end = timedelta_since_now(days=-7) self.zosia.save() response = self.client.post(self.url_2, {}) self.room_2.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) room_assertions.assertLocked(self.room_2, self.normal_2)
def test_user_can_join_locked_room_with_password(self): self.room_2.join(self.normal_2) self.room_2.set_lock(self.normal_2) self.client.force_authenticate(user=self.normal_1) create_user_preferences(self.normal_1, self.zosia, payment_accepted=True) data = { "user": self.normal_1.pk, "password": self.room_2.lock.password } response = self.client.post(self.url_2, data) self.room_2.refresh_from_db() self.assertEqual(response.status_code, status.HTTP_200_OK) room_assertions.assertJoined(self.normal_1, self.room_2) room_assertions.assertJoined(self.normal_2, self.room_2)
def test_can_user_choose_room_when_at_user_start_time(self): self.active.rooming_start = now() self.active.save() user_prefs = create_user_preferences(self.normal, self.active, payment_accepted=True, bonus_minutes=0) result = self.active.can_user_choose_room(user_prefs) self.assertTrue(result)
def test_can_user_choose_room_when_after_user_start_time(self): self.active.rooming_start = time_point(2016, 12, 23, 0, 0) self.active.save() user_prefs = create_user_preferences(self.normal, self.active, payment_accepted=True, bonus_minutes=3) result = self.active.can_user_choose_room(user_prefs, time=time_point( 2016, 12, 22, 23, 58)) self.assertTrue(result)
def test_get_regular_user_already_registered(self): self.client.login(email="*****@*****.**", password="******") org = create_organization(name='ksi', accepted=True) user_prefs = create_user_preferences(self.normal, self.zosia, organization=org) response = self.client.get(self.url, follow=True) self.assertEqual(response.status_code, 200) context = response.context[-1] self.assertEqual(context['form'].__class__, UserPreferencesForm) self.assertEqual(context['object'], user_prefs)
def test_user_cannot_change_accommodation_after_paid(self): create_user_preferences(self.normal, self.zosia, accommodation_day_1=False, shirt_size='M', payment_accepted=True) self.assertEqual( UserPreferences.objects.filter(user=self.normal).count(), 1) self.client.login(email="*****@*****.**", password="******") response = self.client.post(self.url, data={ 'accommodation_day_1': True, 'shirt_size': 'M', 'shirt_type': 'f', 'contact': 'fb: me', 'terms_accepted': True }, follow=True) self.assertEqual(response.status_code, 200) prefs = UserPreferences.objects.filter(user=self.normal).first() self.assertFalse(prefs.accommodation_day_1) # Sanity check ;) self.assertEqual(prefs.shirt_size, 'M')
def test_index_get_staff_user_multiple_zosias(self): another_zosia = create_zosia() create_user_preferences(self.normal, another_zosia) create_user_preferences(self.normal, self.zosia) create_user_preferences(self.staff, self.zosia) self.client.login(email="*****@*****.**", password='******') response = self.client.get(self.url, follow=True) self.assertEqual(response.status_code, 200) context = response.context[-1] self.assertEqual( list(context['objects']), list(UserPreferences.objects.filter(zosia=self.zosia).all()))
def test_find_available_when_passenger_and_bus_chosen_and_not_full(self): user_prefs = create_user_preferences(self.normal, self.zosia, bus=self.bus3) buses = Bus.objects.find_available(self.zosia, passenger=user_prefs) self.assertEqual(buses.count(), 2)
def test_find_buses_with_free_places_when_bus_chosen_and_not_full(self): create_user_preferences(self.normal, self.zosia, bus=self.bus3) buses = Bus.objects.find_with_free_places(self.zosia) self.assertEqual(buses.count(), 2)
def test_find_available_when_bus_chosen_and_full(self): create_user_preferences(self.normal, self.zosia, bus=self.bus2) buses = Bus.objects.find_available(self.zosia) self.assertEqual(buses.count(), 1)
def setUp(self): super().setUp() self.user_prefs = create_user_preferences(self.normal, self.zosia) self.url = reverse('user_preferences_admin_edit')