def setup(self): self.event1 = Event2019Factory.create(event_name="All set") self.event2 = Event2019Factory.create(event_name="Need Chiefs") self.event2.datetime_setup_complete = timezone.now( ) + timezone.timedelta(days=1) self.event2.datetime_start = timezone.now() + timezone.timedelta( days=2) self.event2.datetime_end = timezone.now() + timezone.timedelta(days=3) self.event2.approved = True self.event2.save() self.lighting = Category.objects.create(name="Lighting") self.sound = Category.objects.create(name="Sound") self.l1 = Service.objects.create(shortname="L1", longname="Basic Lighting", base_cost=10.00, addtl_cost=5.00, category=self.lighting) self.s1 = Service.objects.create(shortname="S1", longname="Basic Sound", base_cost=10.00, addtl_cost=5.00, category=self.sound) self.service1 = ServiceInstance.objects.create(service=self.l1, event=self.event1) self.service2 = ServiceInstance.objects.create(service=self.s1, event=self.event2)
def test_user_detail_view(self): self.setup() # Should be allowed to view own profile self.assertOk(self.client.get(reverse("accounts:detail", args=[1]))) # Should not be allowed to view user profile without view_user permission self.assertOk(self.client.get(reverse("accounts:detail", args=[self.user2.pk])), 403) permission = Permission.objects.get(codename="view_user") self.user.user_permissions.add(permission) self.assertOk(self.client.get(reverse("accounts:detail", args=[self.user2.pk]))) # Same permissions currently apply for accessing member profiles # (To require view_member permissions for this as well, you would need to update the Mixin) self.associate.user_set.add(self.user2) # Make sure user has missing CC report event = Event2019Factory.create(event_name="Test Event") event.datetime_end = timezone.now() + timezone.timedelta(days=-1) category = Category.objects.create(name="Lighting") service = Service.objects.create(shortname="L1", longname="Lights", base_cost=10.00, addtl_cost=5.00, category=category) building = Building.objects.create(name="Some Building", shortname="Building") location = Location.objects.create(name="Some Location", building=building) EventCCInstance.objects.create(event=event, crew_chief=self.user2, category=category, service=service, setup_location=location) self.assertOk(self.client.get(reverse("accounts:detail", args=[self.user2.pk])))
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 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 test_events_endpoint(self): models.Endpoint.objects.create(name="Events", url="events", description="Get your event info here", example="name=Test Event", response="[]") # Test that we get 204 response if no events can be found matching query self.assertOk(self.client.get("/api/v1/events"), 204) building = Building.objects.create(name="Campus Center", shortname="CC") location1 = Location.objects.create(name="CC Office", building=building) location2 = Location.objects.create(name="Dunkin Donuts", building=building) now = timezone.now().replace(microsecond=0) time_past = timezone.now() + timezone.timedelta(hours=-1) time_past = time_past.replace(microsecond=0) time_future = timezone.now() + timezone.timedelta(hours=1) time_future = time_future.replace(microsecond=0) Event2019Factory.create(event_name="Test Event", location=location1, datetime_start=now, datetime_end=time_future, description="An event", approved=True) Event2019Factory.create(event_name="Another Event", location=location2, datetime_start=time_past, datetime_end=time_future, approved=True) Event2019Factory.create(event_name="Past Event", location=location1, datetime_start=time_past, datetime_end=time_past, approved=True) now_str = timezone.localtime(now).strftime("%Y-%m-%dT%H:%M:%S%z") time_past_str = timezone.localtime(time_past).strftime("%Y-%m-%dT%H:%M:%S%z") time_future_str = timezone.localtime(time_future).strftime("%Y-%m-%dT%H:%M:%S%z") # Default response should be just upcoming events that are not sensitive, cancelled, or designated as a test default_response = '[{"id":1,"event_name":"Test Event","description":"An event","location":"CC Office",' \ '"datetime_start":"' + now_str + '","datetime_end":"' + time_future_str + '"},' \ '{"id":2,"event_name":"Another Event","description":null,"location":"Dunkin Donuts",' \ '"datetime_start":"' + time_past_str + '","datetime_end":"' + time_future_str + '"}]' self.assertEqual(self.client.get("/api/v1/events").content.decode('utf-8'), default_response) # Test filter by name name_response = '[{"id":1,"event_name":"Test Event","description":"An event","location":"CC Office",' \ '"datetime_start":"' + now_str + '","datetime_end":"' + time_future_str + '"}]' self.assertEqual(self.client.get("/api/v1/events", {"name": "test"}).content.decode('utf-8'), name_response) # Test filter by location location_response = '[{"id":2,"event_name":"Another Event","description":null,"location":"Dunkin Donuts",' \ '"datetime_start":"' + time_past_str + '","datetime_end":"' + time_future_str + '"}]' self.assertEqual(self.client.get("/api/v1/events", {"location": "dunkin"}).content.decode('utf-8'), location_response) # Test filter by start or end time (match) start = now.strftime("%Y-%m-%dT%H:%M:%SZ") end = time_future.strftime("%Y-%m-%dT%H:%M:%SZ") self.assertEqual(self.client.get("/api/v1/events", {"start": start}).content.decode('utf-8'), name_response) self.assertEqual(self.client.get("/api/v1/events", {"end": end}).content.decode('utf-8'), default_response) # Test both start and end time time_response = '[{"id":2,"event_name":"Another Event","description":null,"location":"Dunkin Donuts",' \ '"datetime_start":"' + time_past_str + '","datetime_end":"' + time_future_str + '"},' \ '{"id":3,"event_name":"Past Event","description":null,"location":"CC Office",' \ '"datetime_start":"' + time_past_str + '","datetime_end":"' + time_past_str + '"}]' self.assertEqual(self.client.get("/api/v1/events", {"start": str(time_past + timezone.timedelta(hours=-1)), "end": str(now + timezone.timedelta(minutes=-1))} ).content.decode('utf-8'), time_response) self.assertEqual(self.client.get("/api/v1/events", {"start": str(now), "end": str(time_future + timezone.timedelta(hours=1))} ).content.decode('utf-8'), default_response)
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 test_find_previous(self): self.setup() # Test GET request not allowed self.assertOk(self.client.get(reverse("wizard-findprevious")), 405) # Test missing required fields (in this case location is missing) test_data = { "org": self.org.pk, "event_name": "Test Event #2", "start": timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%f%z'), "end": timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%f%z'), "setup_complete": timezone.now().strftime('%Y-%m-%dT%H:%M:%S.%f%z') } self.assertOk( self.client.post(reverse("wizard-findprevious"), json.dumps(test_data), content_type="application/json"), 422) test_data["location"] = self.location.pk # Test no match self.assertOk( self.client.post(reverse("wizard-findprevious"), json.dumps(test_data), content_type="application/json"), 204) # Test match but no permission to access unapproved events event = Event2019Factory.create(event_name="Test Event", location=self.location, datetime_start=timezone.now() - timezone.timedelta(days=1)) event.org.add(self.org) service = ServiceFactory.create(shortname="S4", enabled_event2019=True) ServiceInstance.objects.create(service=service, event=event, detail="Tons of wub") self.assertOk( self.client.post(reverse("wizard-findprevious"), json.dumps(test_data), content_type="application/json"), 204) # Test match with valid permissions self.org.user_in_charge = self.user self.org.save() expected_response = { "event_name": "Test Event", "location": self.location.pk, "start": str(event.datetime_start), "services": [{ "id": service.shortname, "detail": "Tons of wub" }] } resp = self.client.post(reverse("wizard-findprevious"), json.dumps(test_data), content_type="application/json") self.assertOk(resp) self.assertEqual(resp.content, json.dumps(expected_response).encode('utf-8'))