def test_create_image(self): with app.test_request_context(path='/create_image'): flask.session['num_iters'] = 3 flask.session['style_im'] = 'tests_static_starry_night1.jpg' flask.session['content_im'] = '' rv = app.dispatch_request() assert rv.status_code == 200
def test_200(self, event, fx_dummy_user, fx_dynamodb_table) -> None: # Ensure that the dummy user exists before calling the method assert fx_dynamodb_table.scan()['Items'][0] == fx_dummy_user response = app.dispatch_request(event, {}) assert response['statusCode'] == HTTPStatus.NO_CONTENT assert json.loads(response['body']) == {} assert fx_dynamodb_table.scan()['Items'] == []
def test_delete_image_unauthorized(self): self.delete_image_setup() user2 = User.query.filter_by(social_id="facebook$2").first() image = Image.query.first() with app.test_request_context(path='/delete_image', method='POST', data={'id': image.id}): flask.g.user = user2 rv = app.dispatch_request() assert Image.query.filter_by(id=image.id).count() == 1 assert os.path.exists(os.path.join(app.config['OUT_DIR'], str(image.id) + '.jpg'))
def test_200(self, event, fx_dynamodb_table) -> None: new_name = 'fatsushi2' event['body'] = json.dumps({'name': new_name}) response = app.dispatch_request(event, {}) response_json = json.loads(response['body']) assert response['statusCode'] == HTTPStatus.OK assert fx_dynamodb_table.scan()['Items'][0] == response_json assert response_json['name'] == new_name
def test_save_image(self): # copy final generated image into right directory copyfile('tests/static/4.png', os.path.join(app.config['INTERMEDIATE_IM_DIR'], '4.png')) # create fake user user = User(social_id="facebook$123", nickname="Test User") db.session.add(user) db.session.commit() with app.test_request_context(path='/save_image', method='POST', data={'out_image': '4.png'}): flask.g.user = user flask.session['num_iters'] = 3 flask.session['style_im'] = 'tests_static_starry_night1.jpg' flask.session['content_im'] = '' app.dispatch_request() # assert that image entry has been added to database user = User.query.filter_by(social_id="facebook$123").first() assert Image.query.filter_by(author=user).count() == 1 image_id = Image.query.filter_by(author=user).first().id # assert that file has been saved in filesystem assert os.path.exists(os.path.join(app.config['OUT_DIR'], str(image_id) + '.jpg'))
def test_update_recipe(self): """ Tests if updating works (works), then checking result in show_recipe In the end, it is removing recipe. """ recipe_to_update = {'recipe_name': 'Test to update', 'preparation_steps_list' : 'test test test', 'ingredients_list': 'test1 test1 test1', 'author_name': 'test_user', "servings": "test", "cooking_time": "test", "difficulty": "Medium", "dish_type": "Main", 'cuisine_name': ""} new_data = {'recipe_name': 'Test recipe updated', 'preparation_steps_list' : 'updated test!', 'ingredients_list': 'updated test is updated', 'author_name': 'test_user', "servings": "test", "cooking_time": "test", "difficulty": "Easy", "dish_type": "Main", "cuisine_name": "" } recipe_to_update['file'] = (io.BytesIO(b"abcdef"), 'test.jpg') new_data['file'] = (io.BytesIO(b"abcdef"), 'test.jpg') # inserting recipe with app.test_request_context('insert_recipe', method='POST', data= recipe_to_update, content_type='multipart/form-data'): app.dispatch_request() # checking if worked with app.app_context(): id_part = str(basic.find_recipe_id("recipe_name", "Test to update")) resp = self.app.get('/show_recipe/'+ str(id_part)) self.assertEqual(resp._status_code, 200) self.assertIn('test test test', str(resp.data)) # updating recipe with app.test_request_context('/update_recipe/'+ id_part , method='POST', data=new_data, content_type='multipart/form-data'): app.dispatch_request() with app.app_context(): # checking if worked id_part = str(basic.find_recipe_id("recipe_name", "Test recipe updated")) resp = self.app.get('/show_recipe/' + id_part) self.assertEqual(resp._status_code, 200) self.assertIn('updated test!', str(resp.data)) self.app.get('/remove_recipe/' + str(id_part) + '/True' ) resp = self.app.get('/show_recipe/'+ str(id_part)) self.assertIn('We can\\\'t find the page you are looking for', str(resp.data))
def test_registering_function(self): """ This function is testing creating cookbook from website level. In fact is checking mostly validation. There is additional HTML5 validation for that, however. Because of session problems, crashes when cookbook is in fact created. But tests are going smoothly up to validation point """ invalid_cookbook1 = {'cookbook_name': "new", "password": "******", "author_name": "some_name", "cookbook_desc": ""} invalid_cookbook2 = {'cookbook_name': "new_but_valid", "password": "******", "author_name": "in", "cookbook_desc": ""} valid_cookbook = {'cookbook_name': "valid_title", "password": "******", "author_name": "valid_name", "cookbook_desc": ""} with app.test_request_context('/register', method='POST', data=invalid_cookbook1): app.dispatch_request() # checking if worked with app.test_request_context('/register', method='POST', data=invalid_cookbook2): app.dispatch_request() with app.test_request_context('/register', method='POST', data=valid_cookbook): rv = app.dispatch_request() with app.app_context(): self.assertEqual(views.check_if_exists("cookbook_name", "new"), False) self.assertEqual(views.check_if_exists("cookbook_name", "new_but_valid"), False) # inserted self.assertEqual(views.check_if_exists("cookbook_name", "valid_title"), True) # and cleanup views.mongo.db.cookbooks.delete_one({"author_name": "valid_name"})
def test_inserting_recipe(self): """ Tests if inserting recipe is working (including image file). The only part not passing test is updating provided user's 'recipes_owned' array, as this functionality requires session. It would not work in running app as session would not allow. """ recipe_data = {'recipe_name': 'Inserting?', 'preparation_steps_list' : 'test test test', 'ingredients_list': 'test1 test1 test1', 'author_name': 'test_user', "servings": "test", "cooking_time": "test", "difficulty": "Easy", "dish_type": "Main", 'cuisine_name': "" } recipe_data['file'] = (io.BytesIO(b"abcdef"), 'test.jpg') with app.test_request_context('insert_recipe', method='POST', data=recipe_data, content_type='multipart/form-data'): resp=app.dispatch_request()
def test_upload_images(self): # create FileStorage object from input file style_file = None content_file = None with open('tests/static/starry_night1.jpg', 'rb') as sfp: with open('tests/static/bladerunner.jpg', 'rb') as cfp: style_file = FileStorage(sfp) content_file = FileStorage(cfp) # need test_request_context so we can access session with app.test_request_context(path='/upload_images', method='POST', data=dict(num_iter='3', style_im=style_file, content_im=content_file)): rv = app.dispatch_request() # need this to actually send request # assert session contains the right data assert flask.session['num_iters'] == 3 assert flask.session['style_im'] == 'tests_static_starry_night1.jpg' assert flask.session['content_im'] == 'tests_static_bladerunner.jpg' assert os.path.exists(os.path.join(app.config['UPLOAD_DIR'], 'tests_static_starry_night1.jpg')) assert os.path.exists(os.path.join(app.config['UPLOAD_DIR'], 'tests_static_bladerunner.jpg'))
def test_404(self, event) -> None: event['pathParameters']['user_id'] = 'DUMMYID' event['body'] = json.dumps({'name': 'DUMMYNAME'}) response = app.dispatch_request(event, {}) assert response['statusCode'] == HTTPStatus.NOT_FOUND
def test_404(self, apigw_event) -> None: apigw_event['requestContext']['path'] = '/NOT_IMPLEMENTED_PATH' response = app.dispatch_request(apigw_event, {}) assert response['statusCode'] == HTTPStatus.NOT_FOUND
def test_200(self, event, fx_dynamodb_table) -> None: response = app.dispatch_request(event, {}) response_json = json.loads(response['body']) assert response['statusCode'] == HTTPStatus.CREATED assert fx_dynamodb_table.scan()['Items'][0] == response_json
def test_404(self, event) -> None: event['pathParameters']['user_id'] = 'DUMMYID' response = app.dispatch_request(event, {}) assert response['statusCode'] == HTTPStatus.NOT_FOUND
def test_200(self, event, fx_dummy_user) -> None: response = app.dispatch_request(event, {}) assert response['statusCode'] == HTTPStatus.OK assert json.loads(response['body']) == fx_dummy_user
def test_405(self, apigw_event, not_allowed_method: str) -> None: apigw_event['requestContext']['path'] = '/user' apigw_event['httpMethod'] = not_allowed_method response = app.dispatch_request(apigw_event, {}) assert response['statusCode'] == HTTPStatus.METHOD_NOT_ALLOWED