Exemplo n.º 1
0
    def test_get_latest(self):
        list_id = db_follow_list.save('test follow list 1', self.main_user['id'], [2, 3, 4])
        list_1 = db_follow_list.get(list_id)
        list_id = db_follow_list.save('test follow list 2', self.main_user['id'], [3, 4])
        list_2 = db_follow_list.get(list_id)

        latest_list = db_follow_list.get_latest(self.main_user['id'])
        self.assertEqual(latest_list['id'], list_2['id'])

        db_follow_list.update(list_1['id'], 'new name', [4])
        latest_list = db_follow_list.get_latest(self.main_user['id'])
        self.assertEqual(latest_list['id'], list_1['id'])
Exemplo n.º 2
0
    def test_save(self):
        list_id = db_follow_list.save('test follow list', self.main_user['id'], [2, 3, 4])
        created_list = db_follow_list.get(list_id)
        self.assertEqual(created_list['id'], list_id)
        self.assertEqual(created_list['name'], 'test follow list')
        self.assertEqual(created_list['creator'], self.main_user['id'])
        self.assertIn('created', created_list)
        self.assertIn('last_saved', created_list)
        self.assertEqual(len(created_list['members']), 3)
        for index, user_id in enumerate(range(2, 5)):
            self.assertEqual(created_list['members'][index]['id'], user_id)

        # try to save another list with same name for the same user
        with self.assertRaises(DatabaseException):
            db_follow_list.save('test follow list', self.main_user['id'], [2, 3, 4])
Exemplo n.º 3
0
def save_list():
    creator = _validate_auth_header()
    raw_data = request.get_data()
    try:
        data = ujson.loads(raw_data.decode("utf-8"))
    except ValueError as e:
        log_raise_400("Cannot parse JSON document: %s" % str(e), raw_data)

    try:
        list_name = data['name']
        list_id = data['id']
        members = data['users']
    except KeyError as e:
        log_raise_400("JSON missing key: %s" % str(e))

    members = db_user.validate_usernames(members)
    if list_id is None:
        # create a new list
        try:
            list_id = db_follow_list.save(
                name=list_name,
                creator=creator['id'],
                members=[member['id'] for member in members],
            )
        except DatabaseException as e:
            raise APIForbidden("List with same name already exists.")
    else:

        # do some validation
        current_list = db_follow_list.get(list_id)
        if current_list is None:
            raise APINotFound("List not found: %d" % list_id)
        if current_list['creator'] != creator['id']:
            raise APIUnauthorized("You can only edit your own lists.")

        # update the old list
        db_follow_list.update(
            list_id=list_id,
            name=list_name,
            members=[member['id'] for member in members],
        )

    return jsonify({
        "code": 200,
        "message": "it worked!",
        "list_id": list_id,
    })
Exemplo n.º 4
0
    def test_update(self):
        list_id = db_follow_list.save('test follow list', self.main_user['id'], [2, 3, 4])
        created_list = db_follow_list.get(list_id)
        self.assertEqual(created_list['id'], list_id)
        self.assertEqual(created_list['name'], 'test follow list')
        self.assertEqual(created_list['creator'], self.main_user['id'])
        old_created = created_list['created']
        old_saved = created_list['last_saved']
        self.assertEqual(len(created_list['members']), 3)
        for index, user_id in enumerate(range(2, 5)):
            self.assertEqual(created_list['members'][index]['id'], user_id)

        db_follow_list.update(list_id, 'new name', [3, 4])
        updated_list = db_follow_list.get(list_id)
        self.assertEqual(updated_list['id'], list_id)
        self.assertEqual(updated_list['name'], 'new name')
        self.assertEqual(updated_list['created'], old_created)
        self.assertGreater(updated_list['last_saved'], old_saved)
        self.assertEqual(len(updated_list['members']), 2)
        for index, user_id in enumerate(range(3, 5)):
            self.assertEqual(updated_list['members'][index]['id'], user_id)