Skip to content
This repository has been archived by the owner on Dec 29, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from irgalamarr/lab_3-2_security
Browse files Browse the repository at this point in the history
Add Security to RESTful Service
  • Loading branch information
irgalamarr committed Dec 17, 2023
2 parents f201386 + d9b1905 commit d87f183
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ coverage==6.3.2

# Utilities
httpie==3.2.1
flask-talisman
Flask-Cors
5 changes: 5 additions & 0 deletions service/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
"""
import sys
from flask import Flask
from flask_talisman import Talisman
from flask_cors import CORS
from service import config
from service.common import log_handlers

# Create Flask application
app = Flask(__name__)
app.config.from_object(config)

talisman = Talisman(app)
CORS(app)

# Import the routes After the Flask app is created
# pylint: disable=wrong-import-position, cyclic-import, wrong-import-order
from service import routes, models # noqa: F401 E402
Expand Down
23 changes: 23 additions & 0 deletions tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
from service.common import status # HTTP Status Codes
from service.models import db, Account, init_db
from service.routes import app
from service import talisman

DATABASE_URI = os.getenv(
"DATABASE_URI", "postgresql://postgres:postgres@localhost:5432/postgres"
)

BASE_URL = "/accounts"
HTTPS_ENVIRON = {'wsgi.url_scheme': 'https'}


######################################################################
Expand All @@ -34,6 +36,7 @@ def setUpClass(cls):
app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI
app.logger.setLevel(logging.CRITICAL)
init_db(app)
talisman.force_https = False

@classmethod
def tearDownClass(cls):
Expand Down Expand Up @@ -171,3 +174,23 @@ def test_get_account_list(self):
self.assertEqual(resp.status_code, status.HTTP_200_OK)
data = resp.get_json()
self.assertEqual(len(data), 5)

def test_security_headers(self):
"""It should return security headers"""
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
self.assertEqual(response.status_code, status.HTTP_200_OK)
headers = {
'X-Frame-Options': 'SAMEORIGIN',
'X-Content-Type-Options': 'nosniff',
'Content-Security-Policy': 'default-src \'self\'; object-src \'none\'',
'Referrer-Policy': 'strict-origin-when-cross-origin'
}
for key, value in headers.items():
self.assertEqual(response.headers.get(key), value)

def test_cors_security(self):
"""It should return a CORS header"""
response = self.client.get('/', environ_overrides=HTTPS_ENVIRON)
self.assertEqual(response.status_code, status.HTTP_200_OK)
# Check for the CORS header
self.assertEqual(response.headers.get('Access-Control-Allow-Origin'), '*')

0 comments on commit d87f183

Please sign in to comment.