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 auth(): last_login = datetime.now() account_uri = session['ms_user']['uri'] user = User.query(User).filter(User.account_uri == account_uri) user_dict = {'last_login': last_login, 'account_uri': account_uri} if user.count() == 0: user_dict['service_name'] = session['ms_service'] user_dict['display_name'] = session['ms_user']['display_name'] user_dict['account_id'] = session['ms_user']['id'] res = User.commit_data(user_dict) if not res['success']: return res user = User.query(User).filter(User.account_uri == account_uri) user = user.first() user.session.close() if not user.radios: session['configured'] = False return redirect(url_for('user_settings.show_settings')) session['configured'] = True User.update_row(user_dict) return redirect(url_for('radios.radios_list'))
def signup(): """ User sign-up page. GET requests serve sign-up page. POST requests validate form & user creation. """ form = SignupForm() if form.validate_on_submit(): existing_user = User.query(User).filter_by( email=form.email.data).first() if existing_user is None: user = User(name=form.name.data, email=form.email.data, website=form.website.data) user.set_password(form.password.data) user.session.add(user) user.session.commit() # Create new user login_user(user) # Log in as newly created user return redirect(url_for('main_bp.dashboard')) flash('A user already exists with that email address.') return render_template('signup.html', title='Create an Account.', form=form, template='signup-page', body="Sign up for a user account.")
def edit_database_user(**kwargs): ''' Interface to edit user database entry Should receive UUID and first_name and/or email and/or password Will return first_name, email and uuid upon success ''' user_list = User.find_instance_by_uuid(kwargs.get(enums.UUID_KEY)) if user_list: user = user_list[0] first_name = kwargs.get(enums.FIRST_NAME_KEY) email = kwargs.get(enums.EMAIL_KEY) if email and User.find_instance_by_email(email): raise Exception(f'Could not edit user: {email} already in use') password = kwargs.get(enums.PASSWORD_KEY) if any([first_name, email, password]): try: user.edit_user(first_name, email, password) user.persist() return user.payload() except Exception as err: raise Exception(f'Could not edit user: {err}') else: raise Exception('No new user information was passed in') else: raise Exception('No user was found for the given UUID')
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_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_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__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_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_create_database_user(context): ("user_interface.create_database_user should return an user payload (dict)" ) # Given I have a valid data first_name = 'Guido' email = '*****@*****.**' password = '******' json_data = { enums.FIRST_NAME_KEY: first_name, enums.EMAIL_KEY: email, enums.PASSWORD_KEY: password } # And that I call create_database_user result = user_interface.create_database_user(**json_data) user = User.find_instance_by_email(email)[0] context.objects.append(user) # 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 add the same json_data when I call create_database_user it should throw an Exception user_interface.create_database_user.when.called_with( **json_data).should.have.raised(Exception) # And if I try to add a json with missing information it should also throw an Exception json_data_invalid = {} user_interface.create_database_user.when.called_with( **json_data_invalid).should.have.raised(Exception)
def test_api_create_user(context): ("POST on /api/user should return a json containing the information persisted in database" ) # Given that I perform a POST to /api/user with a valid JSON data first_name = 'Guido' email = '*****@*****.**' password = '******' json_data = { enums.FIRST_NAME_KEY: first_name, enums.EMAIL_KEY: email, enums.PASSWORD_KEY: password } response = context.http.post('/api/user', json=json_data) context.objects.append(User.find_instance_by_email(email)[0]) # 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)
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 get_database_users(**kwargs): ''' Interface to get user database entries ''' user_list = User.get_all_instances() if user_list: return [user.payload() for user in user_list] else: raise Exception('No user found in the database')
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 get_database_user(**kwargs): ''' Interface to get user database entry Should receive UUID ''' uuid = kwargs.get(enums.UUID_KEY) user_list = User.find_instance_by_uuid(uuid) if user_list: user = user_list[0] return user.payload() else: raise Exception(f'No user was found for the passed in UUID {uuid}')
def test_user_payload(): ("user.payload should return a dict with user details") # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user = User(first_name, email, password) # And that I call the payload on the object result = user.payload() # Then the result should be a dict result.should.be.a(dict) # And the dict should have an expected structure result.should.equal({ enums.FIRST_NAME_KEY: user.first_name, enums.UUID_KEY: user.uuid, enums.EMAIL_KEY: user.email })
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 delete_database_user(**kwargs): ''' Interface to remove user database entry Should receive email ''' email = kwargs.get(enums.EMAIL_KEY) user_list = User.find_instance_by_email(email) if user_list: user = user_list[0] payload = user.payload() user.remove() return payload else: raise Exception(f'No user was found for the given email {email}')
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_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)
def test__create_user_handler(context): ("controller._create_user_handler should return a tuple with a dictionary and a HTTP code status" ) # Given I have a valid json_data first_name = 'Guido' email = '*****@*****.**' password = '******' json_data = { enums.FIRST_NAME_KEY: first_name, enums.EMAIL_KEY: email, enums.PASSWORD_KEY: password } # And that I call _create_user_handler result = controller._create_user_handler(json_data) user = User.find_instance_by_email(email)[0] context.objects.append(user) # 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(user.uuid) http_status_code.should.equal(enums.HTTP_SUCCESS) # However if I try to add the same json_data when I call _create_user_handler result = controller._create_user_handler(json_data) # When I check the result # Then it should be a tuple 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(email) http_status_code.should.equal(500)
def login(): if current_user.is_authenticated: return redirect(url_for('welcome_page.get_started')) form = LoginForm() if form.validate_on_submit(): user = User.query(User).filter_by(email=form.email.data).first() if user and user.check_password(password=form.password.data): login_user(user) next_page = request.args.get('next') return redirect(next_page or url_for('welcome_page.get_started')) flash('Invalid username/password') return redirect(url_for('auth_page.login')) return render_template('login.html', form=form)
def test_create_user(): ("user.__init__ should create a brand new user with custom UUID and password hash" ) # Given I have a valid data first_name = 'Guido' email = '*****@*****.**' password = '******' # And that I call the User.__init__ user = User(first_name, email, password) # Then it should be a User instance user.should.be.a(User) # And its UUID should be a string user.uuid.should.be.a(str) # And its password should be stored in bytes user.password.should.be.a(bytes)
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_edit_user(): ("user.edit_user should change the user attributes if modifications are passed in" ) # Given I have an existing user first_name = 'Guido' email = '*****@*****.**' password = '******' user = User(first_name, email, password) uuid_before = user.uuid password_before = user.password # And that I call the edit_user with some valid modifications first_name_new = "Raymond" email_new = '*****@*****.**' password_new = 'jupyter567' user.edit_user(first_name_new, email_new, password_new) password_after = user.password # Then its UUID should be the same as before user.uuid.should.be(uuid_before) # And its name should be the new name user.first_name.should.be(first_name_new) # And its email should be the new email user.email.should.be(email_new) # And its password shouldnt be the same as stored user.password.shouldnt.be(password_before) # However if I pass no changes to edit_user user.edit_user('', '', '') # Then its UUID should be the same as before user.uuid.should.be(uuid_before) # And its name should remain the new name user.first_name.should.be(first_name_new) # And its email should remain the new email user.email.should.be(email_new) # And its password remain as stored user.password.should.be(password_after)
def radios_list(): radios_data = User.get_user_radio_page_data(session['ms_user']['id']) return render_template('radios_list.html', radios=radios_data)