def test_register(): with client() as c: resp = api_post(c, None, '/api/register', None) assert 400 == resp.status_code resp = api_post(c, None, '/api/register', { 'timezone': 'US/Eastern', 'facebook_id': 'xxx123', 'facebook_access_token': '123' }) assert 200 == resp.status_code, 'oops {}'.format(resp.data) resp = ujson.loads(resp.data) assert resp['objects'][0]['id'] > 0 # try again, should fail now with a dupe resp = api_post(c, None, '/api/register', { 'timezone': 'US/Eastern', 'facebook_id': 'xxx123', 'facebook_access_token': '123' }) assert 400 == resp.status_code, 'oops {}'.format(resp.data) # try to login as this user now resp = api_post(c, None, '/api/login', { 'facebook_id': 'xxx123', 'facebook_access_token': '123' }) assert 200 == resp.status_code, 'oops {}'.format(resp.data) resp = ujson.loads(resp.data) assert resp['objects'][0]['facebook_id'] == 'xxx123'
def test_create_event(): from server.models.event import Event from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') create_event(c, user1, 'test event') resp = api_get(c, user1, '/api/events/') assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert data['objects'][0]['name'] == 'test event' assert data['objects'][0]['group'] == {'$ref': 'Group:1'} event_id = data['objects'][0]['id'] event = Event.find(event_id) assert user1.is_attending(event), 'user is attending the new event' assert 1 == Event.select().count() event_id = create_event(c, user1, 'test event 2') user1 = User.find(key='test') assert user1.get_attending_id() == event_id assert 2 == Event.select().count(), '2 events total now' count, page, results = Event.select().group(user1.group).execute() assert 1 == count, 'only 1 in the group though, since the other has no attendees now' assert results[0].name == 'test event 2'
def test_private_event(): from server.models.event import Event, EventAttendee from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') make_friends(c, user1, user2) event_id = create_event(c, user1, 'test event 1', privacy='private') assert 1 == Event.select().count() assert 0 == Event.select().group(user1.group).count() assert 1 == Event.select().user(user1).count() assert 0 == Event.select().user(user2).count() event = Event.find(event_id) resp = api_post(c, user1, '/api/events/{}/invites'.format(event_id), {'invited_id': user2.id}) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 1 == Event.select().user(user2).count() count, page, results = EventAttendee.select().event(event).user( user2).execute() assert count == 1 assert results[0] == user1
def test_update_user(): from server.models.user import User with client() as c: user = User.find(key='test') resp = api_post(c, user, '/api/users/me', { 'key': '123', 'bio': '321' }) assert resp.status_code == 200, 'oops {}'.format(resp.data) user = User.find(key='test') assert user.bio == '321' assert user.key == 'test', 'key should not change, blacklisted'
def test_event_messages(): from server.models.event import Event from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') event_id = create_event(c, user1, 'e1') event = Event.find(event_id) create_event_message(c, user1, event, 'test.jpg') assert 1 == EventMessage.select().event(event).count() assert 1 == EventMessage.select().event(event).user(user1).count() assert 0 == EventMessage.select().event(event).user(user2).count() make_friends(c, user1, user2) assert 1 == EventMessage.select().event(event).user(user2).count()
def test_event_message_votes(): from server.models.event import Event from server.models.user import User from server.db import wigo_db with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') user3 = User.find(key='test3') make_friends(c, user2, user3) event_id = create_event(c, user1, 'e1') event = Event.find(event_id) message_id_1 = create_event_message(c, user1, event, 'test.jpg') message_id_2 = create_event_message(c, user1, event, 'test.jpg') message_1 = EventMessage.find(message_id_1) message_2 = EventMessage.find(message_id_2) resp = create_event_message_vote(c, user1, event, message_1) resp = create_event_message_vote(c, user2, event, message_2) resp = create_event_message_vote(c, user3, event, message_2) assert wigo_db.get_sorted_set_size(skey(message_1, 'votes')) == 1 assert wigo_db.get_sorted_set_size(skey(message_2, 'votes')) == 2 assert EventMessage.select().event(event).by_votes().get() == message_2 resp = api_get(c, user1, '/api/events/{}/messages/meta'.format(event_id)) data = ujson.loads(resp.data) assert 1 == data[str(message_id_1)]['num_votes'] make_friends(c, user1, user2) make_friends(c, user1, user3) resp = create_event_message_vote(c, user2, event, message_1) resp = create_event_message_vote(c, user3, event, message_1) assert EventMessage.select().event(event).by_votes().get() == message_1
def test_update_user_group(): from server.models.group import Group from server.models.user import User with client() as c: user = User.find(key='test') g = user.group user.group_id = Group.find(code='san_diego').id user.save() assert User.find(key='test').group.name == 'San Diego' # posting with geo should change the users group resp = api_post(c, user, '/api/users/me', { 'bio': '321' }, lat=42.3584, lon=-71.0598) assert User.find(key='test').group.name == 'Boston'
def test_user_events(): from server.models.event import Event from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') create_event(c, user1, 'test event 1') create_event(c, user2, 'test event 2') assert 2 == Event.select().count(), '2 events total now' assert 2 == Event.select().group(user1.group).count() count, page, results = Event.select().user(user1).execute() assert 1 == count assert results[0].name == 'test event 1' count, page, results = Event.select().user(user2).execute() assert 1 == count assert results[0].name == 'test event 2'
def test_attending(): from server.models.event import Event, EventAttendee from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') create_event(c, user1, 'test event 1') event = Event.find(name='test event 1', group=user1.group) resp = api_post(c, user2, '/api/events/{}/attendees'.format(event.id), {}) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 2 == EventAttendee.select().event(event).count() assert 1 == EventAttendee.select().event(event).user(user1).count() assert 1 == EventAttendee.select().event(event).user(user2).count() make_friends(c, user1, user2) assert 2 == EventAttendee.select().event(event).user(user1).count()
def test_private_friends(): from server.db import wigo_db from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') user2.privacy = 'private' user2.save() Friend({ 'user_id': user1.id, 'friend_id': user2.id, 'accepted': True }).save() assert wigo_db.set_is_member(skey(user1, 'friends', 'private'), user2.id) user2.privacy = 'public' user2.save() assert not wigo_db.set_is_member(skey(user1, 'friends', 'private'), user2.id)
def test_events_with_friends(): from server.models.event import Event from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') create_event(c, user1, 'test event 1') assert 1 == Event.select().user(user1).count() assert 0 == Event.select().user(user2).count() make_friends(c, user1, user2) assert 1 == Event.select().user(user1).count() assert 1 == Event.select().user(user2).count() # remove friends, event should disappear from user2 resp = api_delete(c, user2, '/api/users/me/friends/{}'.format(user1.id)) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 1 == Event.select().user(user1).count() assert 0 == Event.select().user(user2).count()
def test_friends(): from server.models.group import Group from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') resp = api_post(c, user1, '/api/users/me/friends', { 'friend_id': user2.id }) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 0 == User.select().user(user2).friends().count() assert 1 == User.select().user(user2).friend_requests().count() assert 0 == User.select().user(user1).friends().count() assert 0 == User.select().user(user1).friend_requests().count() resp = api_post(c, user2, '/api/users/me/friends', { 'friend_id': user1.id }) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 1 == User.select().user(user2).friends().count() assert 0 == User.select().user(user2).friend_requests().count() assert 1 == User.select().user(user1).friends().count() assert 0 == User.select().user(user1).friend_requests().count() # remove friendship resp = api_delete(c, user2, '/api/users/me/friends/{}'.format(user1.id)) assert resp.status_code == 200, 'oops {}'.format(resp.data) assert 0 == User.select().user(user1).friends().count() assert 0 == User.select().user(user2).friends().count()
async def test_create_answer(): method = methods.Health() client = tests.client() resp, status = await client.request(method) assert status == 204
async def test_health(): method = methods.Health(api_key=tests.fpredictor_api_key) client = tests.client() resp, status = await client.request(method) assert status == 204
def test_messages(): from server.models.user import User with client() as c: user1 = User.find(key='test') user2 = User.find(key='test2') make_friends(c, user1, user2) resp = api_post(c, user1, '/api/messages', {'to_user_id': user2.id, 'message': 't1'}) assert resp.status_code == 200, 'oops {}'.format(resp.data) resp = api_get(c, user1, '/api/conversations') assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 1 assert data['objects'][0]['message'] == 't1' # post another message resp = api_post(c, user1, '/api/messages', {'to_user_id': user2.id, 'message': 't2'}) assert resp.status_code == 200, 'oops {}'.format(resp.data) resp = api_get(c, user1, '/api/conversations') assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 1 assert data['objects'][0]['message'] == 't2' resp = api_get(c, user1, '/api/conversations/{}'.format(user2.id)) assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 2 assert data['objects'][0]['message'] == 't2' assert data['objects'][1]['message'] == 't1' resp = api_get(c, user2, '/api/conversations') assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 1 assert data['objects'][0]['message'] == 't2' resp = api_get(c, user2, '/api/conversations/{}'.format(user1.id)) assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 2 assert data['objects'][0]['message'] == 't2' assert data['objects'][1]['message'] == 't1' resp = api_delete(c, user1, '/api/messages/{}'.format(data['objects'][0]['id'])) assert resp.status_code == 501, 'oops {}'.format(resp.data) # delete the conversation with user2 resp = api_delete(c, user1, '/api/conversations/{}'.format(user2.id)) # user 1 shouldn't see it anymore resp = api_get(c, user1, '/api/conversations') assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 0 resp = api_get(c, user1, '/api/conversations/{}'.format(user2.id)) assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 0 # but user 2 still should resp = api_get(c, user2, '/api/conversations') assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 1 resp = api_get(c, user2, '/api/conversations/{}'.format(user1.id)) assert resp.status_code == 200, 'oops {}'.format(resp.data) data = ujson.loads(resp.data) assert len(data['objects']) == 2 assert data['objects'][0]['message'] == 't2' assert data['objects'][1]['message'] == 't1'