1212from service .common import status # HTTP Status Codes
1313from service .models import db , Account , init_db
1414from service .routes import app
15+ from service import talisman
1516
1617DATABASE_URI = os .getenv (
1718 "DATABASE_URI" , "postgresql://postgres:postgres@localhost:5432/postgres"
1819)
1920
2021BASE_URL = "/accounts"
22+ HTTPS_ENVIRON = {'wsgi.url_scheme' : 'https' }
2123
2224
2325######################################################################
@@ -34,6 +36,7 @@ def setUpClass(cls):
3436 app .config ["SQLALCHEMY_DATABASE_URI" ] = DATABASE_URI
3537 app .logger .setLevel (logging .CRITICAL )
3638 init_db (app )
39+ talisman .force_https = False
3740
3841 @classmethod
3942 def tearDownClass (cls ):
@@ -169,4 +172,24 @@ def test_get_account_list(self):
169172 def test_method_not_allowed (self ):
170173 """It should not allow an illegal method call"""
171174 resp = self .client .delete (BASE_URL )
172- self .assertEqual (resp .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
175+ self .assertEqual (resp .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
176+
177+ def test_security_headers (self ):
178+ """It should return security headers"""
179+ response = self .client .get ('/' , environ_overrides = HTTPS_ENVIRON )
180+ self .assertEqual (response .status_code , status .HTTP_200_OK )
181+ headers = {
182+ 'X-Frame-Options' : 'SAMEORIGIN' ,
183+ 'X-Content-Type-Options' : 'nosniff' ,
184+ 'Content-Security-Policy' : 'default-src \' self\' ; object-src \' none\' ' ,
185+ 'Referrer-Policy' : 'strict-origin-when-cross-origin'
186+ }
187+ for key , value in headers .items ():
188+ self .assertEqual (response .headers .get (key ), value )
189+
190+ def test_cors_security (self ):
191+ """It should return a CORS header"""
192+ response = self .client .get ('/' , environ_overrides = HTTPS_ENVIRON )
193+ self .assertEqual (response .status_code , status .HTTP_200_OK )
194+ # Check for the CORS header
195+ self .assertEqual (response .headers .get ('Access-Control-Allow-Origin' ), '*' )
0 commit comments