def sign_up_user(): """ [서비스] 유저 회원가입 Author: Mark Hasung Kim Returns: { "custom_message": "SERVICE_USER_CREATED, "result": "POST } """ connection = None try: data = request.json if 'username' not in data: raise ApiException(400, INVALID_INPUT) if 'email' not in data: raise ApiException(400, INVALID_INPUT) if 'password' not in data: raise ApiException(400, INVALID_INPUT) if 'userTypeId' not in data: raise ApiException(400, INVALID_INPUT) if not validate_password(data['password']): raise ApiException(400, INVALID_PASSWORD) if not validate_email(data['email']): raise ApiException(400, INVALID_EMAIL) user_info = { 'username': data['username'], 'email': data['email'], 'password': data['password'], 'user_type_id': int(data['userTypeId']), 'phone_number': '', } connection = connect_db() user_service = UserService() user_service.create_user(user_info, connection) connection.commit() return {"custom_message": "SERVICE_USER_CREATED", "result": "POST"} except ApiException as e: if connection: connection.rollback() raise e finally: if connection: connection.close()
class TestUserService(unittest.TestCase): def __init__(self, *args, **kwargs): unittest.TestCase.__init__(self, *args, **kwargs) self.user_service = UserService() def test_create_user(self): self.user_id = self.user_service.create_user(email='default', password='******', name='default') def test_create_session(self): self.session_id = self.user_service.create_session( user_id=self.user_id, password='******') def test_find_one_user_by_id(self): self.user_service.find_one_user_by_id(session_id=self.session_id) def test_find_one_session_by_id(self): self.user_service.find_one_session_by_id(session_id=self.session_id) def runTest(self): self.test_create_user() self.test_create_session() self.test_find_one_user_by_id() self.test_find_one_session_by_id()
def create_user(): """ Create a user. Arguments: user_name (string): Name of user. Returns: Success boolean and API formatted user object. """ req_data = request.get_json() if 'user_name' not in req_data: res = { 'success': 0, 'error': '"user_name" param required' } return make_response( jsonify(res), 400 ) user_name = req_data['user_name'] user = UserService.create_user(user_name) if user is not None: user = UserService.get_user_api_formatted_data(user) res = { 'success': 1 if user is not None else 0, 'user': user } return jsonify(res)
def users_post(): """ register one or many user info :return: a status message """ user_service = UserService() content = request.forms user_id = user_service.create_user(email=content['email'], password=content['password'], name=content['name']) print('created user ', user_id) return str(user_id)
from service.user_service import UserService from service.task_service import TaskService from service.vote_service import VoteService from service.vote_variant_service import VoteVariantService from utils.print import ppp # mock data user_names = ['jim', 'bob', 'sara', 'penny'] task_titles = ['do the dishes', 'buy a car', 'build an app'] # populate mock users users = [] for user_name in user_names: users.append(UserService.create_user(user_name)) # populate mock tasks tasks = [] for title in task_titles: tasks.append(TaskService.create_task(title)) # load vote variants for reference vote_variants = VoteVariantService.load_all_vote_variants() # populate mock votes for user in users: for task in tasks: VoteService.create_vote( user.uuid4, task.uuid4,
class OneAuthTests(unittest.TestCase): def setUp(self): os.environ['ONE_AUTH_ENV'] = 'test' APP.config['TESTING'] = True self.app = APP.test_client() self.mail = Mail(APP) self.db_connection = get_db_session(APP.db_engine) self.user_service = UserService(self.db_connection) self.validation_code_service = ValidationCodeService(self.db_connection) def test_should_return_415_while_not_passing_json(self): data = '{"email": "*****@*****.**"}' response = self.app.post('/one_auth/api/user', data=data, content_type='application/xml') self.assertEquals(415, response.status_code) def test_should_return_400_if_the_any_mandatory_field_is_not_exist_in_request_when_update_the_user_password(self): self.user_service.delete_user('*****@*****.**') with patch.object(one_auth.ValidationCodeService, 'generate_validation_code', lambda: '123456'): self.app.post('/one_auth/api/user', data='{"email": "*****@*****.**"}', content_type='application/json') update_data = '{"email": "*****@*****.**", "validation_code": "123456"}' response = self.app.put('/one_auth/api/user', data=update_data, content_type='application/json') self.assertEquals(400, response.status_code) update_data = '{"email": "*****@*****.**", "password": "******"}' response = self.app.put('/one_auth/api/user', data=update_data, content_type='application/json') self.assertEquals(400, response.status_code) def test_update_user_password(self): self.user_service.delete_user('*****@*****.**') with patch.object(one_auth.ValidationCodeService, 'generate_validation_code', lambda: '123456'): self.app.post('/one_auth/api/user', data='{"email": "*****@*****.**"}', content_type='application/json') update_data = '{"email": "*****@*****.**", "validation_code": "123456", "password": "******"}' response = self.app.put('/one_auth/api/user', data=update_data, content_type='application/json') self.db_connection.commit() created_user = self.user_service.get_user('*****@*****.**') response_json = json.loads(response.data.decode('utf8')) self.assertEquals(200, response.status_code) self.assertEquals('active', created_user.status) self.assertIsNotNone(response_json['access_token']) self.assertIsNotNone(created_user.password) def test_update_user_password_when_password_in_wrong_format(self): self.user_service.delete_user('*****@*****.**') with patch.object(one_auth.ValidationCodeService, 'generate_validation_code', lambda: '123456'): self.app.post('/one_auth/api/user', data='{"email": "*****@*****.**"}', content_type='application/json') update_data = '{"email": "*****@*****.**", "validation_code": "123456", "password": "******"}' response = self.app.put('/one_auth/api/user', data=update_data, content_type='application/json') # self.assertEquals(400, response.status_code) def test_create_and_get_user(self): self.user_service.delete_user('*****@*****.**') user = self.user_service.create_user('*****@*****.**') assert '*****@*****.**' == user.email self.user_service.delete_user('*****@*****.**') def test_should_return_access_token_when_login_with_correct_email_and_password(self): some_email = '*****@*****.**' self.user_service.delete_user(some_email) with patch.object(one_auth.ValidationCodeService, 'generate_validation_code', lambda : '123456'): self.app.post('/one_auth/api/user', data='{"email": "*****@*****.**"}', content_type='application/json') update_data = '{"email": "*****@*****.**", "validation_code": "123456", "password": "******"}' response = self.app.put('/one_auth/api/user', data=update_data, content_type='application/json') response = self.app.get('/one_auth/api/access_tokens', content_type='application/json', headers={'Authorization': 'Basic eHh4QHRlc3QuY29tOnBhc3N3b3JkMQ=='}) result_data = json.loads(response.data.decode('utf8')) self.assertEquals(200, response.status_code) self.assertIsNotNone(result_data['access_token']) self.user_service.delete_user(some_email) def test_should_return_401_when_login_with_incorrect_email_and_password(self): some_email = '*****@*****.**' self.user_service.delete_user(some_email) with patch.object(one_auth.ValidationCodeService, 'generate_validation_code', lambda : '123456'): self.app.post('/one_auth/api/user', data='{"email": "*****@*****.**"}', content_type='application/json') update_data = '{"email": "*****@*****.**", "validation_code": "123456", "password": "******"}' self.app.put('/one_auth/api/user', data=update_data, content_type='application/json') response = self.app.get('/one_auth/api/access_tokens', content_type='application/json', headers={'Authorization': 'Basic OmFhYWE='}) self.assertEquals(401, response.status_code) self.user_service.delete_user(some_email) def test_should_return_200_when_access_token_validation_success(self): access_token = create_user_and_get_token('*****@*****.**') authorization = basic_auth('*****@*****.**', access_token) response = self.app.get('/one_auth/api/validations', headers={ 'Authorization': authorization }) self.assertEquals(200, response.status_code) result_data = json.loads(response.data.decode('utf8')) self.assertEquals('*****@*****.**', result_data['email']) self.assertEquals('', result_data['first_name']) self.assertEquals('', result_data['last_name']) self.assertEquals('', result_data['country']) self.assertEquals('', result_data['department']) self.assertEquals('', result_data['avatar']) @patch('one_auth.UserService') def test_should_return_401_when_access_token_validation_fail(self, mock_user_service): mock_user_service.encode_access_token_for = MagicMock(return_value='invalid_token') response = self.app.get('/one_auth/api/validations', headers={ 'Authorization': 'Basic eHh4QHRlc3QuY29tOnRlc3RfdG9rZW4=' }) self.assertEquals(401, response.status_code)