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 ):
@@ -172,4 +175,24 @@ def test_delete_account(self):
172175 def test_method_not_allowed (self ):
173176 """It should not allow an illegal method call"""
174177 resp = self .client .delete (BASE_URL )
175- self .assertEqual (resp .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
178+ self .assertEqual (resp .status_code , status .HTTP_405_METHOD_NOT_ALLOWED )
179+
180+ def test_security_headers (self ):
181+ """It should return security headers"""
182+ response = self .client .get ('/' , environ_overrides = HTTPS_ENVIRON )
183+ self .assertEqual (response .status_code , status .HTTP_200_OK )
184+ headers = {
185+ 'X-Frame-Options' : 'SAMEORIGIN' ,
186+ 'X-Content-Type-Options' : 'nosniff' ,
187+ 'Content-Security-Policy' : 'default-src \' self\' ' ,
188+ 'Referrer-Policy' : 'strict-origin-when-cross-origin'
189+ }
190+ for key , value in headers .items ():
191+ self .assertEqual (response .headers .get (key ), value )
192+
193+ def test_cors_security (self ):
194+ """It should return a CORS header"""
195+ response = self .client .get ('/' , environ_overrides = HTTPS_ENVIRON )
196+ self .assertEqual (response .status_code , status .HTTP_200_OK )
197+ # Check for the CORS header
198+ self .assertEqual (response .headers .get ('Access-Control-Allow-Origin' ), '*' )
0 commit comments