def test_reset_password(self, password1=None, password2=None, original_password=None): if password1 is None or password2 is None or original_password is None: original_password = '******' password1 = '12' password2 = '12' with app.app_context(): # first login res1 = self.client.post('/users/login', data=json.dumps( dict(email="hzj.com", password="******")), content_type='application/json', follow_redirects=True) dat1 = res1.get_json() log1 = dat1['login'] self.assertEqual(log1, True) response = self.client.post( '/users/reset-password', data=json.dumps( dict(password1=password1, original_password=original_password, password2=password2)), content_type='application/json', follow_redirects=True) data = response.get_json() reset = data['reset_password'] self.assertEqual(reset, True)
def test_fail_login(self, email="wrong-email", password="******"): with app.app_context(): response = self.client.post('/users/login', data=json.dumps( dict(email=email, password=password)), content_type='application/json', follow_redirects=True) data = response.get_json() print(data) # self.assertIn("login", data) login = data['login'] self.assertEqual(login, False)
def test_success_login(self, email=None, password=None): if email is None or password is None: email = 'hzj.com' password = '******' with app.app_context(): response = self.client.post('/users/login', data=json.dumps( dict(email=email, password=password)), content_type='application/json') data = response.get_json() # self.assertIn("login", data) login = data['login'] self.assertEqual(login, True)
def test_login_logout_user(self): with app.app_context(): res1 = self.client.post('/users/login', data=json.dumps( dict(email="hzj.com", password="******")), content_type='application/json', follow_redirects=True) dat1 = res1.get_json() log1 = dat1['login'] self.assertEqual(log1, True) response = self.client.get('/users/logout', follow_redirects=True) data = response.get_json() print(data) logout = data['logout'] self.assertEqual(logout, True)
def test_register(self, email=None, password=None, username=None): if email is None or password is None or username is None: email = 'gjf.com' password = '******' username = "******" with app.app_context(): response = self.client.post('/users/register', data=json.dumps( dict(username=username, email=email, password=password)), content_type='application/json', follow_redirects=True) data = response.get_json() # self.assertIn("login", data) register = data['register'] self.assertEqual(register, True)
def createresponse(request_data): from interface import app method = request_data['method'] api_name = request_data['api_name'] redirect_path = request_data['redirect_path'] data = request_data['data'] response = None try: with app.app_context(): if method == 'GET': request_data = requests.get(redirect_path) elif method == 'POST': request_data = requests.post(redirect_path, headers=headers, data=json.dumps(data)) elif method == 'PUT': request_data = requests.put(redirect_path, headers=headers, data=json.dumps(data)) elif method == 'DELETE': request_data = requests.delete(redirect_path, headers=headers) response = make_response(request_data.content) status_code = request_data.status_code if request_data.content.decode() != 'No data to return.': response.headers['Content-Type'] = 'application/json' except Exception as e: print(e) response = { 'msg': 'Error occurred while contacting service: ' + api_name } status_code = 500 return response, status_code
def test_reset_username(self, new_username=None): if new_username is None: new_username = "******" with app.app_context(): # First Without Login response = self.client.post('/users/reset-username', data=json.dumps( dict(new_username=new_username)), content_type='application/json', follow_redirects=True) data = response.get_json() self.assertIsNone(data) # Login First res1 = self.client.post('/users/login', data=json.dumps( dict(email="hzj.com", password="******")), content_type='application/json', follow_redirects=True) dat1 = res1.get_json() log1 = dat1['login'] self.assertEqual(log1, True) # Get Method Test res0 = self.client.get('/users/reset-username', follow_redirects=True) data0 = res0.get_data(as_text=True) self.assertIn('reset username', data0) # Post Method Test response = self.client.post('/users/reset-username', data=json.dumps( dict(new_username=new_username)), content_type='application/json', follow_redirects=True) data = response.get_json() self.assertIsNotNone(data) reset = data['reset_username'] self.assertEqual(reset, True)
def test_return_user_info(self, email="hzj.com", password="******"): with app.app_context(): # login first res1 = self.client.post('/users/login', data=json.dumps( dict(email=email, password=password)), content_type='application/json', follow_redirects=True) dat1 = res1.get_json() log1 = dat1['login'] self.assertEqual(log1, True) response = self.client.get('/users/info', follow_redirects=True) data = response.get_json() username = data['username'] return_email = data['email'] self.assertEqual(username, "hzj") self.assertEqual(return_email, "hzj.com")
def test_reset_email(self, new_email=None, password="******"): if new_email is None: new_email = "ripley.com" with app.app_context(): # First Without Login response = self.client.post('/users/reset-email', data=json.dumps( dict(new_email=new_email, password=password)), content_type='application/json', follow_redirects=True) data = response.get_json() self.assertIsNone(data) # Login First res1 = self.client.post('/users/login', data=json.dumps( dict(email="hzj.com", password=password)), content_type='application/json', follow_redirects=True) dat1 = res1.get_json() log1 = dat1['login'] self.assertEqual(log1, True) # Get Method Test res0 = self.client.get('/users/reset-email', follow_redirects=True) data0 = res0.get_data(as_text=True) self.assertIn('Welcome to reset email', data0) # Post Method Test # Password fail response = self.client.post('/users/reset-email', data=json.dumps( dict(new_email=new_email, password="******")), content_type='application/json', follow_redirects=True) data = response.get_json() reset = data['reset_email'] self.assertEqual(reset, False) # Email does not change response1 = self.client.post('/users/reset-email', data=json.dumps( dict(new_email="hzj.com", password="******")), content_type='application/json', follow_redirects=True) data1 = response1.get_json() reset1 = data1['reset_email'] self.assertEqual(reset1, False) # Success response2 = self.client.post('/users/reset-email', data=json.dumps( dict(new_email=new_email, password="******")), content_type='application/json', follow_redirects=True) data2 = response2.get_json() reset2 = data2['reset_email'] self.assertEqual(reset2, True) def test_return_user_info(self, email="hzj.com", password="******"): with app.app_context(): # login first res1 = self.client.post('/users/login', data=json.dumps( dict(email=email, password=password)), content_type='application/json', follow_redirects=True) dat1 = res1.get_json() log1 = dat1['login'] self.assertEqual(log1, True) response = self.client.get('/users/info', follow_redirects=True) data = response.get_json() username = data['username'] return_email = data['email'] self.assertEqual(username, "hzj") self.assertEqual(return_email, "hzj.com")
def send_async_email(msg): """ sends the email, however beacuase of the @async function, sends it withing a separate thread. """ with app.app_context(): mail.send(msg)