Example #1
0
    def test_get_handler_one(self):
        client, table = init()

        item = {'item': 'I need to finish this test!', 'completed': True}
        created = create(client, '1', item, table.table_name,
                         ['completed', 'item'])

        event = {
            'queryStringParameters': {
                'id': created['todoId']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

        results = handler(event,
                          {})  # not using the context, so no need to mock it.
        # Verify the results are not null
        assert results
        # Verify the status code is '200'
        assert 'statusCode' in results and results['statusCode'] == '200'
        # Verify the contents of the body
        assert 'body' in results
        body = json.loads(results['body'])
        # Verify that the UUIDs were set
        assert body['userId'] == '1'
        assert body['todoId'] and len(body['todoId']) == 36
        # Verify the items that a user can set were set correctly.
        assert body['item'] == created['item']
        assert body['completed']
    def test_delete_function(self):
        client, table = init()
        item = {'item': 'I need to finish this test!', 'completed': True}

        todo = create(client, '1', item, 'todo_test', ['completed', 'item'])
        delete(client, '1', todo['todoId'], table.table_name)
        todo_from_get = get_one(client, '1', todo['todoId'], table.table_name)

        # Verify it's an empty dict
        assert not todo_from_get
    def test_update_function(self):
        client, table = init()
        item = {'item': 'I need to finish this test!', 'completed': True}

        todo = create(client, '1', item, 'todo_test', ['completed', 'item'])
        todo['completed'] = False
        todo['item'] = 'Make all the tests!'

        update(client, '1', todo, table.table_name)
        todo_from_get = get_one(client, '1', todo['todoId'], table.table_name)

        # Verify it's not complete
        assert not todo_from_get['completed']
        assert todo_from_get['item'] == 'Make all the tests!'
Example #4
0
    def test_get_one_function(self):
        client, table = init()
        item = {'item': 'I need to finish this test!', 'completed': True}

        todo = create(client, '1', item, table.table_name,
                      ['completed', 'item'])
        todo_from_get = get_one(client, '1', todo['todoId'], table.table_name)

        # Verify not null
        assert todo_from_get

        # Verify the record is correct
        assert todo_from_get['todoId'] == todo['todoId']
        assert todo_from_get['item'] == todo['item']
        assert todo_from_get['completed'] == todo['completed']
        assert todo_from_get['userId'] == todo['userId']
    def test_create_function(self):
        client, table = init()
        item = {
            'item': 'I need to finish this test!',
            'completed': True,
            'fake': 8675309
        }

        results = create(client, '1', item, table.table_name,
                         ['completed', 'item'])

        # Verify the results are not null
        assert results
        # Verify that the UUIDs were set
        assert results['userId'] == '1'
        assert results['todoId'] and len(results['todoId']) == 36
        # Verify the items that a user can set were set correctly.
        assert results['item'] == item['item']
        assert results['completed']
        # Verify that the fake attribute is removed by the whitelist
        assert 'fake' not in results
    def test_update_handler(self):
        client, table = init()

        item = {'item': 'I need to finish this test!', 'completed': True}

        event = {
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

        todo = create(client, '1', item, 'todo_test', ['completed', 'item'])

        # make a change to the item
        todo['item'] = todo['item'] + '!!'
        # Set the body to the todo item.
        event['body'] = json.dumps(todo)

        results = handler(event, {})

        # Verify the results are not null
        assert results
        # Verify the status code is '200'
        assert 'statusCode' in results and results['statusCode'] == '200'
        # Verify the contents of the body
        assert 'body' in results
        body = json.loads(results['body'])
        # Verify that the UUIDs were set
        assert body['userId'] == '1'
        assert body['todoId'] and len(body['todoId']) == 36
        # Verify the items that a user can set were set correctly.
        assert body['item'] == json.loads(event['body'])['item']
        assert body['completed']
    def test_delete_handler(self):
        client, table = init()
        item = {'item': 'I need to finish this test!', 'completed': True}

        todo = create(client, '1', item, 'todo_test', ['completed', 'item'])

        event = {
            'queryStringParameters': {
                'id': todo['todoId']
            },
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

        handler(event, {})
        todo_from_get = get_one(client, '1', todo['todoId'], table.table_name)

        # Verify it's an empty dict
        assert not todo_from_get
Example #8
0
    def test_get_handler_all(self):
        client, table = init()

        items = [{
            'item': 'A',
            'completed': True
        }, {
            'item': 'B',
            'completed': False
        }, {
            'item': 'C',
            'completed': True
        }]

        # The first two will be created for user '1' the third for '2'
        create(client, '1', items[0], table.table_name, ['completed', 'item'])
        create(client, '1', items[1], table.table_name, ['completed', 'item'])
        create(client, '2', items[2], table.table_name, ['completed', 'item'])

        event = {
            'requestContext': {
                'authorizer': {
                    'claims': {
                        'cognito:username': '******'
                    }
                }
            }
        }

        results = handler(event,
                          {})  # not using the context, so no need to mock it.
        # Verify the results are not null
        assert results
        # Verify the status code is '200'
        assert 'statusCode' in results and results['statusCode'] == '200'
        # Verify the contents of the body
        assert 'body' in results
        body = json.loads(results['body'])
        assert len(body) == 2
        # Verify that the correct records are returned
        assert all([i['userId'] == '1' for i in body])
        assert all([len(i['todoId']) == 36 for i in body])
Example #9
0
    def test_get_all_function(self):
        client, table = init()
        items = [{
            'item': 'A',
            'completed': True
        }, {
            'item': 'B',
            'completed': False
        }, {
            'item': 'C',
            'completed': True
        }]
        # The first two will be created for user '1' the third for '2'
        create(client, '1', items[0], table.table_name, ['completed', 'item'])
        create(client, '1', items[1], table.table_name, ['completed', 'item'])
        create(client, '2', items[2], table.table_name, ['completed', 'item'])

        todo_items = get_all(client, '1', table.table_name)

        assert len(todo_items) == 2
        # Verify items with content A or B are returned.
        assert all([i['item'] in ['A', 'B'] for i in todo_items])
 def test_create_function_error(self):
     client, table = init()
     # Verify todo items with no item raise an error
     with self.assertRaises(ValueError):
         create(client, '1', {}, table.table_name, ['completed', 'item'])