def setUp(self): disconnect() # Creating connection to mock database using mongoengine connect('mongoenginetest', host='mongomock://localhost') # Creating mock data. User(name='user1', email='*****@*****.**', password=password_encrypt.hash_password('user1pass')).save() User(name='user2', email='*****@*****.**', password=password_encrypt.hash_password('user2pass')).save() User(name='user3', email='*****@*****.**', password=password_encrypt.hash_password('user3pass')).save() User(name='user4', email='*****@*****.**', password=password_encrypt.hash_password('user4pass')).save() self.userAll = app.routes.UserAll() self.userByEmail = app.routes.UserByEmail()
def post(self): """ Function to add new candidates on the database :return: Response based on success of add. """ try: # Assigning API payload to variable data = api.payload # Encrypting plain password from request. data['password'] = password_encrypt.hash_password(data['password']) access_token = create_access_token( expires_delta=timedelta(days=60), identity=data['email']) # Adding new register to database Candidate(name=data['name'], email=data['email'], role=data['role'], branch=data['branch'], personal_token=access_token, video_interview_path='', cv_path='', password=data['password']).save() return make_response(jsonify(message='Successfully Registered'), 201) except: return make_response( jsonify(message='Sorry, an error has occurred'), 406)
def test_routes_user_all_post_success(self): """ Testing if register of new user is successful through the API, new registered user is then checked in the database to see if the names match. """ with app.app.test_client() as client: # Send data as POST form to endpoint user = { 'name': 'testing', 'email': '*****@*****.**', # Converting byte hash to str for formatting as json 'password': str(password_encrypt.hash_password('testuserpassword')) } result = client.post( '/user', data=json.dumps(user), content_type='application/json' ) # Creating expected response object. expected = ExpectedResponse(201, 'Successfully Registered') # Assigning API response relevant fields to an instance of the object response = ApiResponseMock(result) self.assertEqual(expected.data, response.message) self.assertEqual(expected.status_code, response.status_code)
def test_compare_passwords_fail(self): """ Testing if the raw password matches the hashed password. """ raw_password = '******' hashed = password_encrypt.hash_password(raw_password) result = password_encrypt.compare_passwords('&anotherPassword@2021', hashed) self.assertFalse(result)
def test_compare_passwords_success(self): """ Testing if the raw password matches the hashed password. """ raw_password = '******' hashed = password_encrypt.hash_password(raw_password) result = password_encrypt.compare_passwords(raw_password, hashed) self.assertTrue(result)
def test_routes_user_all_get_fail(self): """ Testing the get method from API creating new register after getting the response from the API to make sure it isn't the same. """ with app.app.app_context(): # Getting response from the API api_response = app.routes.UserAll.get(self.userAll) # Assigning API response relevant fields to an instance of the object response = ApiResponseMock(api_response) User(name='user5', email='*****@*****.**', password=password_encrypt.hash_password('user5pass')).save() # Creating expected response object. expected = ExpectedResponse(200, jsonify(User.objects.all()).data.decode()) self.assertNotEqual(expected.data, response.data) self.assertEqual(expected.status_code, response.status_code)
def post(self): """ Function to add new staffs on the :return: Response based on success of add. """ # Assigning API payload to variable data = api.payload # Encrypting plain password from request. data['password'] = password_encrypt.hash_password(data['password']) # Adding new register to database Staff(name=data['name'], email=data['email'], password=data['password'], isStaff=True).save() return make_response(jsonify(message='Successfully Registered'), 201)
def test_hash_password_number(self): """ Testing if the password as int is encrypted """ result = password_encrypt.hash_password(1234567890) self.assertEqual('Please insert a string to continue', result)
def test_hash_password_byte(self): """ Testing if the password as byte is encrypted """ result = password_encrypt.hash_password(b'&testingPassword@2021') self.assertNotEqual(b'&testingPassword@2021', result)
def test_hash_password_string(self): """ Testing if the password as string is encrypted """ result = password_encrypt.hash_password('&testingPassword@2021') self.assertNotEqual('&testingPassword@2021', result)