Exemplo n.º 1
0
def post_antiwish(request, my_id, api_key):
    me = auth(my_id, api_key)
    server_id = 0
    if request.method == 'POST':
        antiwish = loads(request.raw_post_data)
        update = {'user': me}
        if 'content' in antiwish:
            update['content'] = antiwish['content']
        if 'client_id' in antiwish:
            update['client_id'] = antiwish['client_id']
        if 'created' in antiwish:
            update['created'] = antiwish['created']
        if 'id' in antiwish:
            server_id = antiwish['id']
            AntiWish.objects.filter(id=antiwish['id'], user=me).update(**update)
            UserActivity(
                user=me,
                target=None,
                message=antiwish['content'],
                activity_type='updated_item',
            ).save()
        else:
            try:
                antiwish = AntiWish(**update)
                antiwish.save()
                server_id = antiwish.id
                UserActivity(
                    user=me,
                    target=None,
                    message=antiwish.content,
                    activity_type='added_new_item',
                ).save()
            except IntegrityError:
                return render_error(_(u'Something went wrong!'))
    return render_json({'id': server_id})
Exemplo n.º 2
0
    def test_delete_antiwish(self):
        item = AntiWish(user=self.user, content='asd', created=now())
        item.save()

        data = self.json(self.client.get(self.url('my-info')))
        self.assertEqual(len(data['antiwishes']), 1)
        data = self.json(self.client.post(self.url('delete-antiwish/' + str(item.id))))
        self.assertEqual(len(data['antiwishes']), 0)
Exemplo n.º 3
0
 def create_antiwish(self, user):
     self.antiwish = AntiWish(
         created=now(),
         content='asd',
         client_id=1,
         user=user,
     )
     self.antiwish.save()
Exemplo n.º 4
0
 def test_bad_delete_antiwish(self):
     item = AntiWish(user=self.second_user, content='asd', created=now())
     item.save()
     self.go = lambda x: self.client.post(self.url('delete-antiwish/' + str(item.id)))
     self.assert_error({}, 'Nothing to delete')
Exemplo n.º 5
0
class UserTestCase(MyTestCase):
    def setUp(self):
        super(UserTestCase, self).setUp()
        self.user = User.objects.create_user('me', '', 'psw')
        self.second_user = User.objects.create_user('she', '', '1')

    def make_friends(self):
        Friendship(
            initiator=self.user,
            target=self.second_user,
            accepted=True,
        ).save()

    def create_antiwish(self, user):
        self.antiwish = AntiWish(
            created=now(),
            content='asd',
            client_id=1,
            user=user,
        )
        self.antiwish.save()

    def assert_antiwish(self, data, antiwish):
        self.assertEqual(data['content'], antiwish.content)
        self.assertEqual(data['id'], antiwish.id)
        self.assertEqual(data['client_id'], antiwish.client_id)
        self.assertEqual(parser.parse(data['created']), antiwish.created)

    def assert_friend(self, data, friend, status):
        self.assertEqual(data['username'], friend.username)
        self.assertEqual(data['id'], friend.id)
        self.assertEqual(data['status'], status)

    def test_update_profile(self):
        post = lambda items: self.json(self.client.post(self.url('update-profile'), dumps(items), content_type='application/json'))
        avatar = open(os.path.join(settings.STATIC_ROOT, 'images', 'test.jpg'), 'rb').read().encode('base64')
        data = post({
            'first_name': 'White',
            'last_name': 'Man',
            'city': 'Budapest',
            'country': 'Hungary',
            'avatar': avatar,
        })
        self.assertEqual(data['first_name'], 'White')
        self.assertEqual(data['last_name'], 'Man')
        self.assertEqual(data['city'], 'Budapest')
        self.assertEqual(data['country'], 'Hungary')
        self.assertTrue(len(data['avatar_url']))

    def test_my_info(self):
        self.make_friends()
        self.create_antiwish(self.user)

        data = self.json(self.client.get(self.url('my-info')))
        self.assertEqual(data['username'], 'me')
        self.assertEqual(data['id'], 1)
        self.assert_friend(data['friends'][0], self.second_user, 2)
        self.assert_antiwish(data['antiwishes'][0], self.antiwish)

    def test_user_info(self):
        self.make_friends()
        self.create_antiwish(self.second_user)
        third_user = User.objects.create_user('test', '', '2')
        Friendship(
            initiator=third_user,
            target=self.second_user,
            accepted=False,
        ).save()

        data = self.json(self.client.get(self.url('user-info/' + str(self.second_user.id))))
        self.assertEqual(data['username'], 'she')
        self.assertEqual(data['id'], 2)
        self.assertEqual(len(data['friends']), 1)
        self.assert_friend(data['friends'][0], self.user, 2)
        self.assert_antiwish(data['antiwishes'][0], self.antiwish)
        self.assertEqual(data['status'], 2)

    def test_post_antiwish(self):
        post = lambda items: self.json(self.client.post(self.url('post-antiwish'), dumps(items), content_type='application/json'))

        data = post({
            'client_id': 1,
            'content': 'first',
            'created': str(now()),
        })
        self.assertEqual(data['id'], 1)
        self.assertEqual(AntiWish.objects.get(id=1).content, 'first')

        data = post({
            'content': 'updated',
            'id': 1,
        })
        self.assertEqual(len(AntiWish.objects.all()), 1)
        self.assertEqual(AntiWish.objects.get(id=1).content, 'updated')

        data = post({
            'content': 'second',
            'client_id': 2,
            'created': str(now()),
        })
        self.assertEqual(len(AntiWish.objects.all()), 2)
        self.assertEqual(AntiWish.objects.get(id=2).content, 'second')

    def test_delete_antiwish(self):
        item = AntiWish(user=self.user, content='asd', created=now())
        item.save()

        data = self.json(self.client.get(self.url('my-info')))
        self.assertEqual(len(data['antiwishes']), 1)
        data = self.json(self.client.post(self.url('delete-antiwish/' + str(item.id))))
        self.assertEqual(len(data['antiwishes']), 0)

    def test_bad_delete_antiwish(self):
        item = AntiWish(user=self.second_user, content='asd', created=now())
        item.save()
        self.go = lambda x: self.client.post(self.url('delete-antiwish/' + str(item.id)))
        self.assert_error({}, 'Nothing to delete')

    def test_request_friendship(self):
        data = self.json(self.client.post(self.url('request-friendship-with/' + str(self.second_user.id))))
        self.assert_friend(data['friends'][0], self.second_user, 1)
        data = self.json(self.client.get('/api/{0}/{1}/my-info/'.format(self.second_user.id, self.second_user.api_key.key)))
        self.assert_friend(data['friends'][0], self.user, 0)

    def test_bad_request_friendship(self):
        data = self.json(self.client.post(self.url('request-friendship-with/1000')))
        self.assertEqual(data['friends'], [])

    def test_accept_friendship(self):
        self.client.post(self.url('request-friendship-with/' + str(self.second_user.id)))
        data = self.json(self.client.get('/api/{0}/{1}/accept-friendship/{2}/'.format(self.second_user.id, self.second_user.api_key.key, self.user.id)))
        self.assert_friend(data['friends'][0], self.user, 2)
        data = self.json(self.client.get(self.url('my-info')))
        self.assert_friend(data['friends'][0], self.second_user, 2)

    def test_bad_accept_friendship(self):
        self.client.post(self.url('request-friendship-with/' + str(self.second_user.id)))
        data = self.json(self.client.post(self.url('accept-friendship/' + str(self.second_user.id))))
        self.assert_friend(data['friends'][0], self.second_user, 1)
        data = self.json(self.client.post(self.url('accept-friendship/10000')))
        self.assertEqual(len(data['friends']), 1)

    def test_ignore_friendship(self):
        self.client.post(self.url('request-friendship-with/' + str(self.second_user.id)))
        data = self.json(self.client.get('/api/{0}/{1}/ignore-friendship/{2}/'.format(self.second_user.id, self.second_user.api_key.key, self.user.id)))
        self.assertEqual(len(data['friends']), 0)
        data = self.json(self.client.get(self.url('my-info')))
        self.assertEqual(len(data['friends']), 0)

    def test_bad_ignore_friendship(self):
        data = self.json(self.client.post(self.url('ignore-friendship/1000')))
        self.assertEqual(data['friends'], [])

    def test_remove_friend(self):
        Friendship(initiator=self.user, target=self.second_user, accepted=True).save()
        data = self.json(self.client.post(self.url('remove-friend/' + str(self.second_user.id))))
        self.assertEqual(data['friends'], [])
        data = self.json(self.client.post(self.url('remove-friend/' + str(self.second_user.id))))
        self.assertEqual(data['friends'], [])

    def test_bad_remove_friend(self):
        data = self.json(self.client.post(self.url('ignore-friendship/1000')))
        self.assertEqual(data['friends'], [])