def test_api_delete_user(context): ("DELETE on /api/user/<string:email> should remove the user persisted in database" ) # Given that I have already an user in the database first_name = 'Guido' email = '*****@*****.**' password = '******' user_created = User(first_name, email, password) user_created.persist() # Given that I perform a DELETE to /api/user/email response = context.http.delete(f'/api/user/{email}') # When I check the response response.headers.should.have.key("Content-Type").being.equal( "application/json") # And check if the status was success response.status_code.should.equal(enums.HTTP_SUCCESS) # And when I deserialize the JSON data = json.loads(response.data) # Then the data should have a specific key and value as below data.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name) data.should.have.key(enums.EMAIL_KEY).being.equal(email) data.should.have.key(enums.UUID_KEY).being.equal(user_created.uuid) # And the user should not be persisted anymore user_list = User.find_instance_by_email(email) user_list.should.be.empty
def test_api_get_user(context): ("GET on /api/user/<string:uuid> should get the user persisted in database" ) # Given that I have already an user in the database first_name = 'Guido' email = '*****@*****.**' password = '******' user_created = User(first_name, email, password) user_created.persist() context.objects.append(user_created) # Given that I perform a POST to /api/user/uuid with a valid JSON data user_uuid = user_created.uuid response = context.http.get(f'/api/user/{user_uuid}') # When I check the response response.headers.should.have.key("Content-Type").being.equal( "application/json") # And check if the status was success response.status_code.should.equal(enums.HTTP_SUCCESS) # And when I deserialize the JSON data = json.loads(response.data) # Then the data should have a specific key and value as below data.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name) data.should.have.key(enums.EMAIL_KEY).being.equal(email) data.should.have.key(enums.UUID_KEY).being.equal(user_uuid)
def test_find_instance_by_uuid(context): ("user.find_instance_by_uuid should return a list of users with no element or a single one" ) # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user = User(first_name, email, password) user.persist() context.objects.append(user) # And that I call the find_instance_by_email with the user email result = User.find_instance_by_uuid(user.uuid) # Then the result should be a list result.should.be.a(list) # And the list should have an unique element len(result).should.equal(1) # And the user should be in the list user.should.be.within(result) # However if I call the find_instance_by_email with an invalid email result = User.find_instance_by_uuid("herecomesaninvalidUUID") # Then the result should be a list result.should.be.a(list) # And the list should be empty result.should.be.empty
def test_get_all_instances(context): ("user.get_all_instances should return a list of users") # Given that I have already users in the database first_name_1 = 'Guido' email_1 = '*****@*****.**' password_1 = 'py123' user_created_1 = User(first_name_1, email_1, password_1) user_created_1.persist() context.objects.append(user_created_1) first_name_2 = 'Raymond' email_2 = '*****@*****.**' password_2 = 'py567' user_created_2 = User(first_name_2, email_2, password_2) user_created_2.persist() context.objects.append(user_created_2) # And that I call get_all_instances result = User.get_all_instances() # Then the result should be a list result.should.be.a(list) # And the users payload should be in the list user_created_1.should.be.within(result) user_created_2.should.be.within(result)
def test_delete_database_user(context): ("user_interface.delete_database_user should return an user payload (dict)" ) # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user_created = User(first_name, email, password) user_created.persist() user_payload = user_created.payload() # And that I call delete_database_user with its email data_json = {enums.EMAIL_KEY: email} result = user_interface.delete_database_user(**data_json) # Then it should be a dictionary result.should.be.a(dict) # And it should be the user payload result.should.equal(user_payload) # However if I try to remove an unknown email and call delete_database_user it should throw an Exception json_data_invalid = {enums.EMAIL_KEY: '*****@*****.**'} user_interface.delete_database_user.when.called_with( **json_data_invalid).should.have.raised(Exception)
def test_get_database_users(context): ("user_interface.get_database_users should return a list of users payload (dict)" ) # Given I have existing users first_name_1 = 'Guido' email_1 = '*****@*****.**' password_1 = 'py123' user_created_1 = User(first_name_1, email_1, password_1) user_created_1.persist() context.objects.append(user_created_1) first_name_2 = 'Raymond' email_2 = '*****@*****.**' password_2 = 'py567' user_created_2 = User(first_name_2, email_2, password_2) user_created_2.persist() context.objects.append(user_created_2) # And that I call get_database_users result = user_interface.get_database_users(**{}) # Then it should be a list result.should.be.a(list) # And it should contains the users payload result.should.contain(user_created_1.payload()) result.should.contain(user_created_2.payload())
def test_get_database_user(context): ("user_interface.get_database_user should return an user payload (dict)") # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user = User(first_name, email, password) user.persist() context.objects.append(user) # And a valid json_data json_data = {enums.UUID_KEY: user.uuid} # And that I call get_database_user with its UUID result = user_interface.get_database_user(**json_data) # Then it should be a dictionary result.should.be.a(dict) # And it should be the user payload result.should.equal(user.payload()) # However if I try to get an unknown UUID call edit_database_user it should throw an Exception json_data_invalid = {enums.UUID_KEY: 'someOddUUID'} user_interface.create_database_user.when.called_with( **json_data_invalid).should.have.raised(Exception)
def test_edit_database_user(context): ("user_interface.edit_database_user should return an user payload (dict)") # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user = User(first_name, email, password) user.persist() context.objects.append(user) # And a valid json_data email_new = '*****@*****.**' password_new = 'ch4ngeMeL4terPleas3' json_data = { enums.UUID_KEY: user.uuid, enums.EMAIL_KEY: email_new, enums.PASSWORD_KEY: password_new } # And that I call edit_database_user result = user_interface.edit_database_user(**json_data) # When I check the result # Then it should be a dictionary result.should.be.a(dict) # And it should be the user payload result.should.equal(user.payload()) # However if I try to edit the user email to another that exists in the database and call edit_database_user it should throw an Exception first_name_2 = 'Guido2' email_2 = '*****@*****.**' password_2 = 'py123' user_2 = User(first_name_2, email_2, password_2) user_2.persist() context.objects.append(user_2) json_data_invalid = { enums.UUID_KEY: user.uuid, enums.EMAIL_KEY: password_2 } user_interface.create_database_user.when.called_with( **json_data_invalid).should.have.raised(Exception) # And if I dont a json with missing information it should also throw an Exception json_data_invalid_info = {enums.UUID_KEY: user.uuid} user_interface.create_database_user.when.called_with( **json_data_invalid_info).should.have.raised(Exception) # Or if I pass an inexisting UUID it should also throw an Exception json_data_invalid_uuid = {enums.UUID_KEY: 'someOddUUID'} user_interface.create_database_user.when.called_with( **json_data_invalid_uuid).should.have.raised(Exception)
def test__edit_user_handler(context): ("controller._edit_user_handler should return a tuple with a dictionary and a HTTP code status" ) # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user = User(first_name, email, password) user.persist() context.objects.append(user) # And a valid json_data email_new = '*****@*****.**' password_new = 'ch4ngeMeL4terPleas3' json_data = { enums.UUID_KEY: user.uuid, enums.EMAIL_KEY: email_new, enums.PASSWORD_KEY: password_new } # And that I call _edit_user_handler result = controller._edit_user_handler(json_data) # When I check the result # Then it should be a tuple containing a dict and an int result.should.be.a(tuple) payload_dict, http_status_code = result # And it should have a dict and a 200 status code payload_dict.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name) payload_dict.should.have.key(enums.EMAIL_KEY).being.equal(email_new) payload_dict.should.have.key(enums.UUID_KEY).being.equal(user.uuid) http_status_code.should.equal(enums.HTTP_SUCCESS) # However if I call the _edit_user_handler and pass no changes json_data_invalid = {enums.UUID_KEY: user.uuid} result = controller._edit_user_handler(json_data_invalid) # When I check the result # Then it should be a tuple containing a dict and an int result.should.be.a(tuple) payload_dict, http_status_code = result # And it should have a dict with an error and a 500 status code payload_dict.should.have.key('error').should.contain( 'No new user information') http_status_code.should.equal(enums.HTTP_ERR_INT)
def test__delete_user_handler(context): ("controller._delete_user_handler should return a tuple with a dictionary and a HTTP code status" ) # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user_created = User(first_name, email, password) user_created.persist() # And that I call _delete_user_handler data_json = {enums.EMAIL_KEY: email} result = controller._delete_user_handler(data_json) # When I check the result # Then it should be a tuple containing a dict and an int result.should.be.a(tuple) payload_dict, http_status_code = result # And it should have a list of dicts and a 200 status code payload_dict.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name) payload_dict.should.have.key(enums.EMAIL_KEY).being.equal(email) payload_dict.should.have.key(enums.UUID_KEY).being.equal(user_created.uuid) http_status_code.should.equal(enums.HTTP_SUCCESS) # And the user should not be persisted anymore user_list = User.find_instance_by_email(email) user_list.should.be.empty # However if I try to remove an inexisting email invalid_email = '*****@*****.**' # And that I call _delete_user_handler json_data_invalid = {enums.EMAIL_KEY: invalid_email} result = controller._delete_user_handler(json_data_invalid) # When I check the result # Then it should be a tuple containing a dict and an int result.should.be.a(tuple) payload_dict, http_status_code = result # And it should have a dict with an error and a 500 status code payload_dict.should.have.key('error').should.contain(invalid_email) http_status_code.should.equal(enums.HTTP_ERR_INT)
def create_database_user(**kwargs): ''' Interface to create user database entry Should receive first_name, email and password Will return first_name, email and uuid upon success ''' email = kwargs.get(enums.EMAIL_KEY) if User.find_instance_by_email(email): raise Exception(f'Could not create user: email {email} already in use') first_name = kwargs.get(enums.FIRST_NAME_KEY) password = kwargs.get(enums.PASSWORD_KEY) try: new_user = User(first_name, email, password) new_user.persist() return new_user.payload() except Exception as err: raise Exception(f'Could not create user: {err}')
def test_api_get_users(context): ("GET on /api/users should get all the users persisted in database") # Given that I have already users in the database first_name_1 = 'Guido' email_1 = '*****@*****.**' password_1 = 'py123' user_created_1 = User(first_name_1, email_1, password_1) user_created_1.persist() context.objects.append(user_created_1) first_name_2 = 'Raymond' email_2 = '*****@*****.**' password_2 = 'py567' user_created_2 = User(first_name_2, email_2, password_2) user_created_2.persist() context.objects.append(user_created_2) # And I perform a GET to /api/users response = context.http.get('/api/users') # When I check the response response.headers.should.have.key("Content-Type").being.equal( "application/json") # And check if the status was success response.status_code.should.equal(enums.HTTP_SUCCESS) # And when I deserialize the JSON data = json.loads(response.data) # Then the data should be a list and have a specific key and value as below { enums.FIRST_NAME_KEY: first_name_1, enums.EMAIL_KEY: email_1, enums.UUID_KEY: user_created_1.uuid }.should.be.within(data) { enums.FIRST_NAME_KEY: first_name_2, enums.EMAIL_KEY: email_2, enums.UUID_KEY: user_created_2.uuid }.should.be.within(data)
def test__get_user_handler(context): ("controller._get_user_handler should return a tuple with a dictionary and a HTTP code status" ) # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user = User(first_name, email, password) user.persist() context.objects.append(user) # And that I call _get_user_handler with an valid uuid uuid = user.uuid json_data = {enums.UUID_KEY: uuid} result = controller._get_user_handler(json_data) # When I check the result # Then it should be a tuple containing a dict and an int result.should.be.a(tuple) payload_dict, http_status_code = result # And it should have a dict and a 200 status code payload_dict.should.have.key(enums.FIRST_NAME_KEY).being.equal(first_name) payload_dict.should.have.key(enums.EMAIL_KEY).being.equal(email) payload_dict.should.have.key(enums.UUID_KEY).being.equal(uuid) http_status_code.should.equal(enums.HTTP_SUCCESS) # However if I call with an invalid uuid invalid_uuid = 'justsomedummystuff' json_data_invalid = {enums.UUID_KEY: invalid_uuid} result = controller._get_user_handler(json_data_invalid) # When I check the result # Then it should be a tuple containing a dict and an int result.should.be.a(tuple) payload_dict, http_status_code = result # And it should have a dict with an error and a 500 status code payload_dict.should.have.key('error').should.contain(invalid_uuid) http_status_code.should.equal(enums.HTTP_ERR_INT)
def test__get_users_handler(context): ("controller._get_users_handler should return a tuple with a list of dictionaries and a HTTP code status" ) # Given I have existing users first_name_1 = 'Guido' email_1 = '*****@*****.**' password_1 = 'py123' user_created_1 = User(first_name_1, email_1, password_1) user_created_1.persist() context.objects.append(user_created_1) first_name_2 = 'Raymond' email_2 = '*****@*****.**' password_2 = 'py567' user_created_2 = User(first_name_2, email_2, password_2) user_created_2.persist() context.objects.append(user_created_2) # And that I call _get_users_handler result = controller._get_users_handler({}) # When I check the result # Then it should be a tuple containing a dict and an int result.should.be.a(tuple) payload_list, http_status_code = result # And it should have a list of dicts and a 200 status code { enums.FIRST_NAME_KEY: first_name_1, enums.EMAIL_KEY: email_1, enums.UUID_KEY: user_created_1.uuid }.should.be.within(payload_list) { enums.FIRST_NAME_KEY: first_name_2, enums.EMAIL_KEY: email_2, enums.UUID_KEY: user_created_2.uuid }.should.be.within(payload_list) http_status_code.should.equal(enums.HTTP_SUCCESS)