def test_add_task(self): mock_task = mock.Mock() with mock.patch('pushy.tasks.create_push_notification_groups.delay', new=mock_task) as mocked_task: send_push_notification('some test push notification', self.payload) notification = PushNotification.objects.latest('id') mocked_task.assert_called_once_with(notification_id=notification.id) self.assertEquals(notification.payload, self.payload)
def test_add_task_filter_on_device_type(self): mock_task = mock.Mock() with mock.patch('pushy.tasks.create_push_notification_groups.delay', new=mock_task) as mocked_task: send_push_notification('some other test push notification', self.payload, filter_type=Device.DEVICE_TYPE_IOS) notification = PushNotification.objects.latest('id') mocked_task.assert_called_with(notification_id=notification.id) self.assertEqual(notification.filter_user, 0) self.assertEqual(notification.filter_type, Device.DEVICE_TYPE_IOS)
def test_add_task_filter_device(self): device = Device.objects.create(key='TEST_DEVICE_KEY', type=Device.DEVICE_TYPE_IOS) mock_task = mock.Mock() with mock.patch('pushy.tasks.send_single_push_notification.apply_async', new=mock_task) as mocked_task: send_push_notification('some other test push notification', self.payload, device=device) notification = PushNotification.objects.latest('id') mocked_task.assert_called_with(kwargs={ 'device': device.id, 'payload': notification.payload })
def test_add_task_filter_on_user(self): user = get_user_model().objects.create_user(username='******', email='*****@*****.**', password='******') mock_task = mock.Mock() with mock.patch('pushy.tasks.create_push_notification_groups.delay', new=mock_task) as mocked_task: send_push_notification('some other test push notification', self.payload, filter_user=user) notification = PushNotification.objects.latest('id') mocked_task.assert_called_with(notification_id=notification.id) self.assertEqual(notification.filter_user, user.id) self.assertEqual(notification.filter_type, 0)
def send_ban(request): ban_id = request.data.get('ban_id') ban = Ban.objects.get(id=ban_id) banned_user = ban.user banned_reason = GeneralFunctions.make_reported_string(ban.b_reason) notification = Notification.objects.create(to_user=banned_user, from_user=request.user, time=now(), text="Ban!!!" + "Reason(s)" + banned_reason) notification.save() send_push_notification('YOUR_TITLE', NotificationSerializer(notification).data, device=notification.to_user.user_device, store=False) return Response(status=status.HTTP_200_OK)
def send_to_group(request): try: group_id = request.data.get('group_id') group = Group.objects.get(id=group_id) message = Message.objects.create(text=request.data.get('text'), owner=request.user, time=now()) for member in group.members.all(): message.receiver.add(member) send_push_notification('YOUR_TITLE', MessageSerializer(message).data, device=member.user_device, store=False) message.save() serializer = MessageSerializer(message) return Response(serializer.data, status=status.HTTP_201_CREATED) except: return Response(status=status.HTTP_400_BAD_REQUEST)
def send_report(request): report_id = request.data.get('report_id') report = Report.objects.get(id=report_id) reported_user = report.user reasons = report.r_reason reason_string = GeneralFunctions.make_reported_string(reasons) notification = Notification.objects.create(from_user=request.user, to_user=reported_user, text="Reporting!!!" + " Reason(s):" + reason_string, time=now()) notification.save() send_push_notification('YOUR_TITLE', NotificationSerializer(notification).data, device=notification.to_user.user_device, store=False)
def create_reply(request): try: message_id = request.data.get('message_id') message = Message.objects.get(id=message_id) receiver_id = request.data.get('receiver_id') receiver = User.objects.get(id=receiver_id) reply = Reply.objects.create(text=request.data.get('text'), owner=request.user, time=now(), message=message) reply.receiver.add(receiver) reply.save() send_push_notification('YOUR_TITLE', MessageSerializer(reply).data, device=receiver.user_device, store=False) serializer = ReplySerializer(reply) return Response(serializer.data, status=status.HTTP_201_CREATED) except: return Response(status=status.HTTP_400_BAD_REQUEST)
def objection(request): try: ban_id = request.data.get('ban_id') ban = Ban.objects.get(id=ban_id) admins = ban.organization.admins.all() for admin in admins: notification = Notification.objects.create(to_user=admin, from_user=request.user, text="Ban Objection", time=now()) notification.save() send_push_notification('YOUR_TITLE', NotificationSerializer(notification).data, device=notification.to_user.user_device, store=False) return Response(status=status.HTTP_200_OK) except: return Response(status=status.HTTP_400_BAD_REQUEST)
def send_friendship_request(request): try: id = request.data.get('id') user = User.objects.get(id=id) notification = Notification.objects.create( to_user=User.objects.get(id=request.user.id), text="Friendship", time=now(), from_user=request.user) notification.save() send_push_notification('YOUR_TITLE', NotificationSerializer(notification).data, device=notification.to_user.user_device, store=False) # user.notification_set.add(notification) return Response(status=status.HTTP_200_OK) except: return Response(status=status.HTTP_400_BAD_REQUEST)
def test_add_task_without_storage(self): with mock.patch('pushy.tasks.create_push_notification_groups.delay'): notification = send_push_notification( 'test', {}, filter_type=Device.DEVICE_TYPE_IOS, store=False ) self.assertIsNone(notification.id)
def test_add_task_without_storage(self): with mock.patch('pushy.tasks.create_push_notification_groups.delay'): notification = send_push_notification( 'test', {}, filter_type=Device.DEVICE_TYPE_IOS, store=False) self.assertIsNone(notification.id)