def setUp(self): app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join( basedir, 'test.db') self.app = app.test_client() db.create_all()
def test_get_one_entry_invalid_user(self): """Tests whether a user can get an entry without logging in to the application.""" with app.test_client() as myapp: response = myapp.get('/api/v2/entries/19') self.assertEqual(response.status_code, 401, msg='The status code should be 401') self.assertTrue(b'Invalid token please' in response.data, msg="'Invalid token please' should be in the response")
def test_login_page(self): with app.test_client() as myapp: response = myapp.post('/api/v2/auth/login', json=dict( email='*****@*****.**', password='******')) self.assertEqual(response.status_code, 200, msg='The status code should be 200') self.assertTrue(b'user_token' in response.data, msg="'Invalid credentials' should be in the response")
def test_login_with_wrong_email(self): with app.test_client() as myapp: response = myapp.post('/api/v2/auth/login', json=dict( email='*****@*****.**', password='******')) self.assertEqual(response.status_code, 401, msg='The status code should be 401') self.assertTrue(b'Invalid credentials' in response.data, msg="'Invalid credentials' should be in the response")
def test_main_page(self): """sends a get request to the home page, extracts the response from the request and confirms that the status code from the response is 200""" with app.test_client() as myapp: response = myapp.get('/api/v2/auth') self.assertEqual(response.status_code, 200) self.assertTrue(b'Welcome to my home page' in response.data, msg="""'Welcome to my home page' should be in the response""")
def test_get_one_entry(self): """Log in a user then tests whether the user can get one valid entry from the diary.""" with app.test_client() as myapp: response = myapp.get('/api/v2/entries/1?user_token=' + self.help_login_user) self.assertEqual(response.status_code, 200, msg='The status code should be 200') self.assertTrue(b'message' in response.data, msg="'message' should be in the response")
def help_login_user(self): """Helper method used to login a user. sends a post request with valid credentials for a registered user, decodes the response, and converts the response into a dictionary. It then extracts a value from the dictionary with the key user_token and returns the token.""" with app.test_client() as myapp: login_user = myapp.post('/api/v2/auth/login', json=dict( email='*****@*****.**', password='******')) login_user = json.loads(login_user.data.decode("utf-8")) token = str(login_user.get("user_token", None)) return token
def test_get_one_invalid_entry(self): """log in a user then tests whether the application can get an invalid entry.""" with app.test_client() as myapp: response = myapp.get('/api/v2/entries/0?user_token=' + self.help_login_user) self.assertEqual(response.status_code, 403, msg='The status code should be 403') self.assertTrue(b'The entry does not exist' in response.data, msg="'The entry does not exist' should be in\ the response")
def setUp(self): self.message = "" self.app = app.test_client() self.app.testing = True with open('endpoint.json') as endpoint_data: endpoint = jsonpickle.decode(endpoint_data.read())['endpoint'] # if endpoint == 'seng3011laser.com' or endpoint == 'localhost:5000': self.endpoint = 'http://' + endpoint self.getResponse = self.urlGet
def setUp(self): app.testing = True self.app = app.test_client() with app.test_request_context(): db.create_all() user = {"tipo": 'admin', "email": "*****@*****.**", "password": '******'} headers = {"Content-Type": 'application/json'} login = requests.post( 'https://choutuve-app-server.herokuapp.com/login', headers=headers, data=json.dumps(user)) self.token = login.text
def test_register_used_email(self): """Tests that registration with an already registered email is not allowed""" with app.test_client() as myapp: response = myapp.post('/api/v2/auth/signup', json=dict(first_name='firstname', last_name='lastname', email='*****@*****.**', user_name='cycks1', password='******', password2='password')) self.assertEqual(response.status_code, 403, msg='''The status code should be 403''')
def test_add_entry_by_invalid_user(self): """Tests whether a user can add an entry without logging in to the application.""" with app.test_client() as myapp: response = myapp.post('/api/v2/entries', json={"title": "Check delete", "contents": "Stand up with LFA", "date_of_event": "1532521128", "reminder_time": "1532521128"}) self.assertEqual(response.status_code, 403, msg='The status code should be 403') self.assertTrue(b'Invalid token please' in response.data, msg="'Invalid token please' should be in the response")
def test_modify_entry(self): """Log in a user then tests whether the user can get all entries from the diary.""" with app.test_client() as myapp: response = myapp.put('/api/v2/entries/1?user_token=' + self.help_login_user, json={"title": "Check", "contents": "Stand up with LFA", "date_of_event": "1532521128", "reminder_time": "1532521128"}) self.assertEqual(response.status_code, 200, msg='The status code should be 200') self.assertTrue(b'message' in response.data, msg="'message' should be in the response")
def test_fetch_authenticated_user_details(self): """log in a user then tests whether the application can fetch appropriate user details.""" with app.test_client() as myapp: login_user = myapp.post('/api/v2/auth/login', json=dict( email='*****@*****.**', password='******')) login_user = json.loads(login_user.data.decode("utf-8")) token = str(login_user.get("user_token", None)) response = myapp.get('/api/v2/auth/profile?user_token='+token) self.assertEqual(response.status_code, 200, msg='The status code should be 200') self.assertTrue(b'message' in response.data, msg="'message' should be in the response")
def test_add_entries(self): """log in a user then tests whether the application can add an entry.""" with app.test_client() as myapp: response = myapp.post('/api/v2/entries?user_token=' + self.help_login_user, json={"title": "Check Delete working", "contents": "Stand up with LFA", "date_of_event": "1532521128", "reminder_time": "1532521128"}) self.assertEqual(response.status_code, 201, msg='The status code should be 201') self.assertTrue(b'now in the database.' in response.data, msg="'now in the database.' should be in the response")
def test_add_invalid_entry(self): """log in a user then tests whether the application can add an invalid entry.""" with app.test_client() as myapp: response = myapp.post('/api/v2/entries?user_token=' + self.help_login_user, json={"title": "Che", "contents": "Stand up with LFA", "date_of_event": "1532521128", "reminder_time": "1532521128"}) self.assertEqual(response.status_code, 411, msg='The status code should be 411') self.assertTrue(b' be longer then 5 and less than 60' in response.data, msg="' be longer then 5 and less than 60' should be in\ the response")
def test_main(self): query = {"keyword": "python", "lat": 53.3498, "lng": -6.2603} tester = app.test_client(self) result = tester.post('/api/v1.0/events', data=json.dumps(query), headers={ 'Authorization': _basic_auth_str('xxxxxx', 'xxxxxxxx'), 'Content-Type': 'application/json' }) # check result from server with expected data self.assertEqual( result.status_code, 200, )
def test_register_used_username(self): """Tests that registration with an already registered username is not allowed""" with app.test_client() as myapp: response = myapp.post('/api/v2/auth/signup', json=dict(first_name='firstname', last_name='lastname', email='*****@*****.**', user_name='cycks', password='******', password2='password')) self.assertEqual(response.status_code, 403, msg='''The status code should be 403''') self.assertTrue(b'username or email already taken' in response.data, msg="""'username or email already taken' should be in the response""")
def setUp(self): db_username = app.config['DB_USERNAME'] db_password = app.config['DB_PASSWORD'] db_host = app.config['DB_HOST'] self.db_uri = "mysql+pymysql://%s:%s@%s/" % (db_username, db_password, db_host) app.config['TESTING'] = True app.config['WTF_CSRF_ENABLED'] = False app.config['BLOG_DATABASE_NAME'] = 'test_blog' app.config['SQL_ALCHEMY_DATABASE_URI'] = self.db_uri + app.config[ 'BLOG_DATABASE_NAME'] engine = sqlalchemy.create_engine(self.db_uri) conn = engine.connect() conn.execute("commit") conn.execute("CREATE DATABASE " + app.config['BLOG_DATABASE_NAME']) db.create_all() conn.close() self.app = app.test_client()
def setUp(self) -> None: app.config["TESTING"] = True app.config["DEBUG"] = False self.app_client = app.test_client()
def test_index10(self): tester = app.test_client(self) response = tester.get('/static/templates/Theory.html') self.assertEqual(response.status_code, 200)
def setUp(self): app.config["TESTING"] = True app.config["CSRF_ENABLED"] = False app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join(basedir, "test.db") self.app = app.test_client() db.create_all()
def test_index4(self): tester = app.test_client(self) response = tester.get('/static/templates/Introduction.html') self.assertEqual(response.status_code, 200)
def test_index9(self): tester = app.test_client(self) response = tester.get('/static/templates/results.htm') self.assertEqual(response.status_code, 200)
import os import sys from flask import * from __init__ import app from flask.ext.sqlalchemy import SQLAlchemy # if you add blueprints, import their models as below from apps.example.models import * app.testing = True app.test_client() ctx = app.test_request_context() ctx.push() print "app and db have been imported." print "You have a test client: client,\nand a test request context: ctx"
def setUp(self) -> None: app.testing = True self.app = app.test_client()
def setUp(self): # creates a test client self.app = app.test_client() self.app.testing = True
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import sys from flask import * from __init__ import app from flask.ext.sqlalchemy import SQLAlchemy from apps.glyph.models import * app.testing = True client = app.test_client() ctx = app.test_request_context() ctx.push() print "app and db have been imported.\nYou have a test client: client,\nand a test request context: ctx"
def test_index8(self): tester = app.test_client(self) response = tester.get('/static/templates/References.html') self.assertEqual(response.status_code, 200)
def client(): client = app.test_client() yield client
def setUp(self): self.app = app.test_client()
def test_index(self): tester = app.test_client(self) response = tester.get('/', content_type='html/text') self.assertEqual(response.status_code, 200)
def setUp(self) -> None: # run every time we run a test case app.testing = True self.app = app.test_client() database_creator.recreate_database()