class GCMNotificationTests(TestCase): 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 tearDown(self): Users.objects.all().delete() Chats.objects.all().delete() def test_send_chat_scheduled_notification(self): gcm = GCMNotificaiton() gcm.send_chat_scheduled_notificaiton(self.chat.id) self.chat.refresh_from_db() self.assertEqual(self.chat.chat_notified_times, 1) def test_send_notification_for_rating_feedbacks(self): gcm = GCMNotificaiton() self.chat.rating_by_userA = { "share_profile": True, "share_message": "This text" } self.chat.rating_by_userB = { "share_profile": True, "share_message": "This text" } self.chat.save() gcm.send_ratings_feedback_notification(self.chat.id) self.chat.refresh_from_db() self.assertEqual(self.chat.rating_notified_times, 1)
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"})