def test_search(self): self.assertOk(self.client.get(reverse("search"))) UserFactory.create(password="******", first_name="Ellen") self.assertContains(self.client.get(reverse("search"), {'q': 'ell'}), "Ellen")
def test_officer_endpoint(self): models.Endpoint.objects.create(name="Officers", url="officers", description="Officer endpoint", example="title=something", response="[]") # Test that we get 404 when there are no officers self.assertOk(self.client.get("/api/v1/officers"), 404) # Test that we see all officers and not normal user (will require user to have title) self.user.title = "President" self.user.first_name = "Test" self.user.last_name = "User" self.user.save() officer = Group.objects.create(name="Officer") officer.user_set.add(self.user) user2 = UserFactory.create(password="******", first_name="Other", last_name="Officer") UserFactory.create(password="******") officer.user_set.add(user2) self.assertEqual( self.client.get("/api/v1/officers").content.decode('utf-8'), '[{"title":"President","name":"Test User"}]') user2.title = "Vice President" user2.save() self.assertEqual( self.client.get("/api/v1/officers").content.decode('utf-8'), '[{"title":"President","name":"Test User"},{"title":"Vice President","name":"Other Officer"}]' ) # Test that we can get a specific officer's info (based on title) self.assertEqual( self.client.get("/api/v1/officers?title=president").content.decode( 'utf-8'), '[{"title":"President","name":"Test User"}]') # Test that we can get a specific officer's info (based on name) - If both parameters, results must match both self.assertEqual( self.client.get("/api/v1/officers?first_name=test&last_name=user" ).content.decode('utf-8'), '[{"title":"President","name":"Test User"}]') # Test additional custom options self.user.class_year = 2020 self.user.save() self.assertEqual( self.client.get( "/api/v1/officers?title=president&options=class_year").content. decode('utf-8'), '[{"title":"President","name":"Test User","class_year":2020}]')
def test_password_set_view(self): self.setup() # Change own password (superuser) self.assertOk(self.client.get(reverse("accounts:password", args=[1]))) data = { 'new_password1': 'Password.1', 'new_password2': 'Password.1', 'save': 'Set Password' } # Should send to login page on success (rendered as view - status should be 200) self.assertOk(self.client.post(reverse("accounts:password", args=[1]), data)) # Change own password (non-superuser) self.user.is_superuser = False self.assertOk(self.client.get(reverse("accounts:password", args=[1]))) # Set own password (non-superuser) self.user.password = None self.assertOk(self.client.get(reverse("accounts:password", args=[1]))) # Non-superuser should not be allowed to set password for someone else another_user = UserFactory.create(password="******") self.assertOk(self.client.get(reverse("accounts:password", args=[another_user.pk])), 403)
def test_hour_viewset(self): models.Endpoint.objects.create(name="Office Hours", url="office-hours", description="Office Hours Endpoint", example="officer=president", response="[]") self.user.title = "President" self.user.save() user2 = UserFactory.create(password="******", title="Vice President") # Test that no content returns a 204 self.assertOk(self.client.get("/api/v1/office-hours"), 204) # Test that we can get all office hours start_time = timezone.now() end_time = timezone.now() + timezone.timedelta(hours=1) hour1 = OfficeHour.objects.create(officer=self.user, day=1, hour_start=start_time.time(), hour_end=end_time.time()) hour2 = OfficeHour.objects.create(officer=user2, day=3, hour_start=start_time.time(), hour_end=end_time.time()) self.assertEqual(self.client.get("/api/v1/office-hours").content.decode('utf-8'), '[{"officer":"President","day":1,"hour_start":"' + str(start_time.time()) + '","hour_end":"' + str(end_time.time()) + '"},{"officer":"Vice President","day":3,"hour_start":"' + str(start_time.time()) + '","hour_end":"' + str(end_time.time()) + '"}]') # Test that we can get office hours for a specific officer self.assertEqual(self.client.get("/api/v1/office-hours?officer=president").content.decode('utf-8'), '[{"officer":"President","day":1,"hour_start":"' + str(start_time.time()) + '","hour_end":"' + str(end_time.time()) + '"}]') # Test that we can filter office hours by the day self.assertEqual(self.client.get("/api/v1/office-hours?day=3").content.decode('utf-8'), '[{"officer":"Vice President","day":3,"hour_start":"' + str(start_time.time()) + '","hour_end":"' + str(end_time.time()) + '"}]') # Test that we can get office hours that start at a certain time (exact match) start_time = timezone.datetime.strptime('13:30', '%H:%M').replace(tzinfo=pytz.timezone('US/Eastern')) end_time = timezone.datetime.strptime('15:30', '%H:%M').replace(tzinfo=pytz.timezone('US/Eastern')) # Ensure that only 1 result will appear in the tests below hour1.hour_start = start_time + timezone.timedelta(hours=3) hour1.hour_end = end_time + timezone.timedelta(hours=2) hour2.hour_start = start_time + timezone.timedelta(hours=3) hour2.hour_end = end_time + timezone.timedelta(hours=2) hour1.save() hour2.save() OfficeHour.objects.create(officer=self.user, day=4, hour_start=start_time.time(), hour_end=end_time.time()) self.assertEqual(self.client.get("/api/v1/office-hours?start=13:30:00").content.decode('utf-8'), '[{"officer":"President","day":4,"hour_start":"13:30:00","hour_end":"15:30:00"}]') # Test that we can get office hours that end at a certain time (exact match) self.assertEqual(self.client.get("/api/v1/office-hours?end=15:30:00").content.decode('utf-8'), '[{"officer":"President","day":4,"hour_start":"13:30:00","hour_end":"15:30:00"}]') # Test that if both start and end times are provided the result contains entries that overlap with those times self.assertEqual(self.client.get("/api/v1/office-hours?start=13:00:00&end=14:00:00").content.decode('utf-8'), '[{"officer":"President","day":4,"hour_start":"13:30:00","hour_end":"15:30:00"}]')
def test_hourchange_viewset(self): models.Endpoint.objects.create( name="Office Hour Updates", url="hours/updates", description="Office Hour Updates endpoint", example="expires=2020-01-01T00:00:00Z", response="[]") self.user.title = "President" self.user.save() user2 = UserFactory.create(password="******") user2.title = "Treasurer" user2.save() # Test that we get 204 response if no updates exist self.assertOk(self.client.get("/api/v1/hours/updates"), 204) post_date = timezone.datetime.strptime( "2019-12-01T12:00:00Z", "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=pytz.UTC) expires_before = timezone.datetime.strptime("2019-12-31T23:59:00Z", "%Y-%m-%dT%H:%M:%SZ")\ .replace(tzinfo=pytz.UTC) expires_match = timezone.datetime.strptime("2020-01-01T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ")\ .replace(tzinfo=pytz.UTC) expires_after = timezone.datetime.strptime("2020-01-03T00:00:00Z", "%Y-%m-%dT%H:%M:%SZ")\ .replace(tzinfo=pytz.UTC) HourChange.objects.create(officer=self.user, expires=expires_before, message="Test Hour Change", date_posted=post_date) HourChange.objects.create(officer=user2, expires=expires_match, message="Another test hour change", date_posted=post_date) HourChange.objects.create(officer=self.user, expires=expires_after, message="Even more hours will change", date_posted=post_date) # Test that we get all updates self.assertEqual(len(self.client.get("/api/v1/hours/updates").data), 3) # Test that we can get updates by officer resp = self.client.get("/api/v1/hours/updates?officer=treasurer") self.assertEqual(len(resp.data), 1) self.assertEqual(resp.data[0]["message"], "Another test hour change") # Test that we can get updates based on when they expire (on or after specified date) resp = self.client.get( "/api/v1/hours/updates?expires=2020-01-01T00:00:00Z") self.assertEqual(len(resp.data), 2) self.assertEqual(resp.data[0]["message"], "Another test hour change") self.assertEqual(resp.data[1]["message"], "Even more hours will change")
def setUp(self): super(TrainingTests, self).setUp() self.user2 = UserFactory.create(password="******") self.training_type = models.TrainingType.objects.create( name="Truss Training", description="It's in the name") self.training = models.Training.objects.create( training_type=self.training_type, date=date.today(), trainer=self.user, recorded_by=self.user) self.trainee = models.Trainee.objects.create(training=self.training, person=self.user2)
def setUp(self): self.e = EventFactory.create(event_name="Test Event") self.e2 = EventFactory.create(event_name="Other Test Event") self.e3 = Event2019Factory.create(event_name="2019 Test Event") self.e4 = Event2019Factory.create(event_name="Another 2019 Test Event") self.user = UserFactory.create(password='******') self.client.login(username=self.user.username, password='******') self.category = Category.objects.create(name="Lighting") self.category.save() sound_cat = Category.objects.create(name="Sound") sound_cat.save() lighting = Lighting.objects.create(shortname="L1", longname="Basic Lighting", base_cost=10.00, addtl_cost=5.00, category=self.category) lighting.save() self.lighting = ServiceInstance.objects.create(service=lighting, event=self.e3) self.lighting.save() self.extra = Extra.objects.create(name="Test Extra", cost=10.00, desc="A test extra", category=self.category) self.extra.save() self.extra_instance = ExtraInstance.objects.create(event=self.e4, extra=self.extra, quant=1) self.extra_instance.save() self.organization = Organization.objects.create( name="Test org", phone="1234567890", user_in_charge=self.user) self.organization.save() self.e4.org.add(self.organization) self.multibilling = MultiBilling.objects.create( date_billed=timezone.datetime.today(), amount=20.00, org=self.organization) self.multibilling.save() self.multibilling.events.add(self.e2) self.multibilling.events.add(self.e4)
def setup(self): self.user2 = UserFactory.create(password="******") self.associate = Group.objects.create(name="Associate") self.officer = Group.objects.create(name="Officer") self.request_factory = RequestFactory()
def setUp(self): self.user = UserFactory.create(password="******", username="******") self.user2 = UserFactory.create(password="******") self.org = OrgFactory.create(user_in_charge=self.user, name="LNL")
def setUp(self): self.user = UserFactory.create(password="******") self.projectionist = models.Projectionist(user=self.user) self.projectionist.save()
def test_crew_endpoint(self): models.Endpoint.objects.create(name="Crew Checkin", url="crew/checkin", description="Checkin to events", example="user=12345&event=1", response="[]") models.Endpoint.objects.create(name="Crew Checkout", url="crew/checkout", description="Checkout from events", example="user=12345&event=1", response="[]") # Get token for authentication token, created = Token.objects.get_or_create(user=self.user) client = APIClient() client.credentials(HTTP_AUTHORIZATION='Token ' + token.key) # Test that only POST requests are permitted self.assertOk(client.get("/api/v1/crew/checkin"), 405) self.assertOk(client.get("/api/v1/crew/checkout"), 405) # Test that student ID is required (the next several tests will be the same for both actions) self.assertOk(client.post("/api/v1/crew/checkin", {'event': 1}), 400) # Test that failure to provide event id results in 400 self.assertOk(client.post("/api/v1/crew/checkin", {'id': 12345}), 400) # Return 404 if user cannot be found with student ID self.assertOk(client.post("/api/v1/crew/checkin", {'id': 12345, 'event': 1}), 404) self.user.student_id = 12345 self.user.save() # Return error if event cannot be found or is otherwise not eligible for checkin / checkout self.assertOk(client.post("/api/v1/crew/checkin", {'id': 12345, 'event': 1}), 404) event = Event2019Factory.create(event_name="Test Event") event.datetime_end = timezone.now() + timezone.timedelta(hours=1) event.max_crew = 1 event2 = Event2019Factory.create(event_name="Another Event") event2.datetime_end = timezone.now() + timezone.timedelta(hours=1) event2.approved = True event2.max_crew = 1 event.save() event2.save() CCInstanceFactory.create(crew_chief=self.user, event=event, setup_start=timezone.now()) CCInstanceFactory.create(crew_chief=self.user, event=event2, setup_start=timezone.now()) self.assertOk(client.post("/api/v1/crew/checkin", {'id': 12345, 'event': 1}), 403) event.approved = True event.save() second_user = UserFactory.create(password="******") other_checkin = CrewAttendanceRecord.objects.create(event=event, user=second_user, active=True) # Should display an error if we have reached maximum crew self.assertOk(client.post("/api/v1/crew/checkin", {'id': 12345, 'event': 1}), 403) other_checkin.delete() # Check response on successful checkin resp = client.post("/api/v1/crew/checkin", {'id': 12345, 'event': 1}) self.assertOk(resp, 201) self.assertTrue(CrewAttendanceRecord.objects.filter(user=self.user).exists()) # Return 409 if checkin record already exists for the user (even if it's for another event) self.assertOk(client.post("/api/v1/crew/checkin", {'id': 12345, 'event': 2}), 409) # Return 404 if checkin record could not be found during checkout self.assertOk(client.post("/api/v1/crew/checkout", {'id': 12345, 'event': 2}), 404) # Test with checkout time included custom_datetime = timezone.now() + timezone.timedelta(days=-1) self.assertOk(client.post("/api/v1/crew/checkout", {'id': 12345, 'event': 1, 'checkout': custom_datetime}), 201) self.assertEqual(CrewAttendanceRecord.objects.get(user=self.user).checkout, custom_datetime) # Test with Checkin time included self.assertOk(client.post("/api/v1/crew/checkin", {'id': 12345, 'event': 1, 'checkin': custom_datetime}), 201) self.assertEqual(CrewAttendanceRecord.objects.get(user=self.user, active=True).checkin, custom_datetime)
def setUp(self): self.e = EventFactory.create(event_name="Test Event") self.e2 = EventFactory.create(event_name="Other Test Event") self.e3 = Event2019Factory.create(event_name="2019 Test Event") self.user = UserFactory.create(password='******')
def setUp(self): self.e = EventFactory.create(event_name="Test Event") self.e2 = EventFactory.create(event_name="Other Test Event") self.user = UserFactory.create(password='******') self.client.login(username=self.user.username, password='******')
def setUp(self): self.e = EventFactory.create(event_name="Test Event") self.e2 = EventFactory.create(event_name="Other Test Event") self.user = UserFactory.create(password='******')