def test_pagination(self): for i in range(25): post("/app/chat/messages", dict(recipient_user_id=self.user_id_2, data=dict(text="test_{}".format(i))), self.token) res = get('/app/chat/messages', dict(thread_id=1), self.token_2) self.assertEqual(res.status_code, 200) data = res.get_json() messages = data.get("messages") cursor = data.get("cursor") self.assertEqual(len(messages), 20) self.assertIsNotNone(cursor) res = get("/app/chat/messages", dict(thread_id=1, cursor=cursor), self.token_2) data = res.get_json() messages_2 = data.get("messages") cursor = data.get("cursor") self.assertEqual(len(messages_2), 5) self.assertIsNotNone(cursor) messages.extend(messages_2) texts = set(x["text"] for x in messages) self.assertSetEqual(texts, set("test_{}".format(i) for i in range(25))) res = get("/app/chat/messages", dict(thread_id=1, cursor=cursor), self.token_2) data = res.get_json() self.assertFalse(data.get("messages")) self.assertEqual(cursor, data.get("cursor"))
def test_rate(self): self.start() # try rating without arriving res = post("/app/meetups/1/partner/rating", dict(rating_id=1), self.token) self.assertEqual(res.status_code, 400) self.arrive(1) res = post("/app/meetups/1/partner/rating", dict(rating_id=1), self.token) self.assertEqual(res.status_code, 200) self.assertTrue(res.get_json()["success"]) res = get("/app/meetups/1", token=self.token) self.assertTrue(res.get_json()["concluded"]) res = get("/app/meetups/1", token=self.token_2) self.assertFalse(res.get_json()["concluded"]) with read() as session: rating = session.query(MeetupUserRating).first() self.assertIsNotNone(rating) self.assertEqual(rating.user_id, self.user_id_2) self.assertEqual(rating.rating_id, 1) self.assertEqual(rating.meetup_id, 1)
def start(self, user_id, activity_id): return post("/app/activity", data=dict( activity_id=activity_id, location=LOCATION if user_id == 1 else LOCATION2 ), token=self.token if user_id == 1 else self.token_2)
def test_send_text(self): res = post("/app/chat/messages", dict(recipient_user_id=self.user_id_2, data=dict(text="test")), self.token) self.assertEqual(res.status_code, 200) data = res.get_json() self.assertDictEqual(data, dict( thread_id=1, text="test", id=1, location=None, place_id=None, sender_user_id=1)) # check created in db with read() as session: thread = session.query(Thread).all() self.assertEqual(len(thread), 1) thread = thread[0] self.assertEqual(thread.creator_user_id, self.user_id) self.assertEqual(thread.recipient_user_id, self.user_id_2) messages = session.query(Message).all() self.assertEqual(len(messages), 1) message = messages[0] self.assertEqual(message.text, "test") self.assertEqual(message.sender_user_id, self.user_id) self.assertIsNone(message.coordinates) self.assertIsNone(message.place_id) # check other user's notifications notifs = get_notifications(self.user_id_2) self.assertEqual(len(notifs), 1) notif_data = notifs[0]["data"]["payload"] self.assertDictEqual(data, notif_data) res = get('/app/chat/messages', dict(thread_id=1), self.token_2) self.assertEqual(res.get_json().get("messages")[0], data)
def test_block(self): res = post("/app/profiles/2/block", token=self.token) self.assertEqual(res.status_code, 200) with session_scope() as session: x = session.query(UserBlock).first() self.assertIsNotNone(x) self.assertEqual(x.blocker_user_id, 1) self.assertEqual(x.user_id, 2)
def start(self, from_place=False): with write() as session: session.add(PlaceCategory(name="test")) session.flush() session.add( Place(name="test", google_place_id="test", coordinates=make_point(PLACE_LOCATION["lat"], PLACE_LOCATION["lng"]), place_category_id=1)) post("/app/activity", data=dict(activity_id=1, location=LOCATION), token=self.token) post("/app/activity", data=dict(activity_id=1, location=LOCATION2), token=self.token_2) post("/app/chat/messages", dict(recipient_user_id=self.user_id_2, data=dict(text="test")), self.token) res = post("/app/meetups", data=dict(thread_id=1, location=None if from_place else PLACE_LOCATION, place_id=1 if from_place else None), token=self.token_2) self.assertEqual(res.status_code, 200) self.meetup_id = res.get_json()["id"]
def test_skip(self): self.start() # try noshow without arriving res = post("/app/meetups/1/partner/skip", dict(rating_id=1), self.token) self.assertEqual(res.status_code, 400) self.arrive(1) res = post("/app/meetups/1/partner/skip", dict(rating_id=1), self.token) self.assertEqual(res.status_code, 200) self.assertTrue(res.get_json()["success"]) res = get("/app/meetups/1", token=self.token) self.assertTrue(res.get_json()["concluded"]) res = get("/app/meetups/1", token=self.token_2) self.assertFalse(res.get_json()["concluded"])
def test_invite(self): invitation_token = create_user(FIRST_NAME, LAST_NAME, EMAIL, False)["invitation_token"] signup_user(invitation_token, PASSWORD) token = login_user(EMAIL, PASSWORD)["token"] res = post( "/admin/administrators", dict(first_name="Test", last_name="Testy", email="test@[email protected]"), token) self.assertEqual(res.status_code, 200)
def test_send_place(self): with write() as session: session.add(PlaceCategory(name="test")) session.flush() session.add( Place(name="test", google_place_id="test", place_category_id=1)) res = post("/app/chat/messages", dict(recipient_user_id=self.user_id_2, data=dict(place_id=1)), self.token) self.assertEqual(res.status_code, 200) self.assertEqual(res.get_json()["place_id"], 1)
def setUp(self): data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": TEST_INSTANCE_NUMBER } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data))
def setUp(self): # Ensures an already existing valid input means 403 error data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "default_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": 1 } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data))
def test_cancel(self): self.start() res = post("/app/meetups/1/cancel", token=self.token) self.assertEqual(res.status_code, 200) self.assertTrue(res.get_json()["success"]) # check user was notified notifications = get_notifications(2) self.assertEqual(len(notifications), 2) # first is message data = notifications[1]["data"] self.assertEqual(data["notification_type"], "MEETUP_CANCEL") self.assertEqual(data["payload"]["meetup_id"], 1) # check meetup is over res = get("/app/meetups/1", token=self.token) self.assertEqual(res.status_code, 200) data = res.get_json() self.assertTrue(data["cancelled"]) self.assertTrue(data["concluded"])
def test_config_response(self): # Ensures valid input means 201 data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "default_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": TEST_INSTANCE_NUMBER } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data)) res = response.json() self.assertEqual(response.status_code, 201) self.assertEqual(res['message'], 'Create Success')
def test_config_existing_response(self): # Ensures an already existing valid input means 403 error data = { "api_name": "email-microapi", "current_config": { "key_1": "value_1", "key_2": "value_2" }, "default_config": { "key_1": "value_1", "key_2": "value_2" }, "user_id": 1 } response = requests.post(BASE_URL + '/config/new', headers={'Content-Type': 'application/json'}, data=json.dumps(data)) res = response.json() self.assertEqual(response.status_code, 403) self.assertEqual(res['message'], 'Config already exists! Please update instead')
def test_place_interests(self): with write() as session: session.add(PlaceCategory(name="test")) session.flush() session.add( Place(name="test", google_place_id="test", place_category_id=1)) self.start(1, 1) self.start(2, 1) # notify interest res = post("/app/activity/places/1/interests", dict(), self.token) self.assertEqual(res.status_code, 200) self.assertTrue(res.get_json()["success"]) # self should not return results res = get("/app/activity/places/1/interests", dict(), self.token) self.assertEqual(res.status_code, 200) self.assertEqual(len(res.get_json()["results"]), 0) # for other should return 1 result res = get("/app/activity/places/1/interests", dict(), self.token_2) self.assertEqual(res.status_code, 200) results = res.get_json()["results"] self.assertEqual(len(results), 1) # results should be the user status status = get("/app/activity/users/1", dict(), self.token_2).get_json() self.assertDictEqual(results[0], status) # remove interest res = delete("/app/activity/places/1/interests", dict(), self.token) self.assertEqual(res.status_code, 200) self.assertTrue(res.get_json()["success"]) # should no longer appear for other res = get("/app/activity/places/1/interests", dict(), self.token_2) self.assertEqual(res.status_code, 200) self.assertEqual(len(res.get_json()["results"]), 0)
def start(self, activity_id=1): return post("/app/activity", data=dict(activity_id=activity_id, location=LOCATION), token=self.token)
def test_block_then_query(self): post("/app/profiles/2/block", token=self.token) res = get("/app/profiles/1", token=self.token_2) self.assertEqual(res.status_code, 403) error_code = res.get_json()["error_code"] self.assertEqual(error_code, "BLOCKED")
def test_send_location(self): res = post("/app/chat/messages", dict(recipient_user_id=self.user_id_2, data=dict(location=dict(lng=0, lat=0))), self.token) self.assertEqual(res.status_code, 200) self.assertEqual(res.get_json()["location"], dict(lat=0.0, lng=0.0))
def test_config_with_no_entry_response(self): # Ensures no body input means 400 error response = requests.post(BASE_URL + '/config/new') self.assertEqual(response.status_code, 400)