def setUp(self): self.usera, _ = Users.objects.get_or_create(fcm_token="token1", fb_link="link1") self.userb, _ = Users.objects.get_or_create(fcm_token="token2", fb_link="link2") self.chat = Chats(userA=self.usera, userB=self.userb, chat_time=now()) self.chat.save()
def update_chats(request): data = json.loads(request.body) username, password = retrieve_username_password_from_authorization(request) if username != Opentok.get_api_key(): return error_response('Unauthorized Access', 401) chat_id = data.get('chat_id', None) if chat_id: Chats.objects.filter(id=chat_id).delete() return JsonResponse({"status": "deleted"}) if data['usera_uuid'] == data['userb_uuid']: return error_response("Cannot schedule call between same users") userA = Users.objects.get(user_uuid=data['usera_uuid']) userB = Users.objects.get(user_uuid=data['userb_uuid']) next_seconds = int(data['next_seconds']) chat_time = now() + timedelta(0, next_seconds) call_possible = check_call_schedule_compatiblity(userA, userB, chat_time) if call_possible is False: return error_response("Chat schedule not possible, check times") q1 = Q(userA=userA) & Q(userB=userB) q2 = Q(userA=userB) & Q(userB=userA) from_time = now() - timedelta(1, 0) q3 = Q(chat_time__gte=from_time) q = Q(Q(q1 | q2) & q3) if Chats.objects.filter(q).exists(): return error_response( "You cannot schedule a call, u already have a chat scheduled since past day" ) chat = Chats(userB=userB, userA=userA, chat_time=chat_time) chat.save() GCMNotificaiton().send_chat_scheduled_notificaiton(chat.id) return JsonResponse({"status": "created"})
def test_generation_of_new_opentok_session_id(self, x1, x2, x3): user = Users() user.save() data = {"user_uuid": user.user_uuid} Chats(userA=user, userB=user, chat_time=now()).save() response = self.client.post('/fiveapp/get_session', content_type="application/json", **self.auth_headers(user.user_uuid, '')) assert response.status_code == 200 res_data = json.loads(response.content) self.assertEqual(res_data['session']['sessionId'], 'session1') self.assertEqual(res_data['session']['token'], 'token1')
def test_chat_time_for_user_with_expired_time(self): user = Users() user.save() data = {"user_uuid": user.user_uuid} chat_time = now() - timedelta(0, 300) Chats(userA=user, userB=user, chat_time=chat_time).save() response = self.client.post('/fiveapp/next_chat', content_type="application/json", **self.auth_headers(user.user_uuid, '')) assert response.status_code == 200 res_data = json.loads(response.content) self.assertIsNone(res_data['chat'])
def test_chat_time_for_user_with_future_time(self): user = Users() user.save() data = {"user_uuid": user.user_uuid} chat_time = now() + timedelta(0, 600) Chats(userA=user, userB=user, chat_time=chat_time).save() response = self.client.post('/fiveapp/next_chat', content_type="application/json", **self.auth_headers(user.user_uuid, '')) assert response.status_code == 200 res_data = json.loads(response.content) self.assertIn('seconds_left_for_chat_start', res_data['chat']) self.assertNotIn('session_data', res_data['chat'])
def test_chat_time_for_user_with_scheduled_chat(self, x, y): user = Users(gender='male') other_user = Users(gender='female') other_user.save() user.save() data = {"user_uuid": user.user_uuid} Chats(userA=user, userB=other_user, chat_time=now()).save() response = self.client.post('/fiveapp/next_chat', content_type="application/json", **self.auth_headers(user.user_uuid, '')) assert response.status_code == 200 res_data = json.loads(response.content) self.assertEqual(res_data['chat']['user']['gender'], 'female') self.assertIn('seconds_left_for_chat_start', res_data['chat']) self.assertIn('token', res_data['chat']['session_data'])