Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add unit tests for Private-Network #367

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions tests/decorator/test_private_network_headers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# -*- coding: utf-8 -*-

from ..base_test import FlaskCorsTestCase
from flask import Flask

from flask_cors import *
from flask_cors.core import *


class SupportsCredentialsCase(FlaskCorsTestCase):
def setUp(self):
self.app = Flask(__name__)

@self.app.route('/test_allow_private_network_access_supported')
@cross_origin(allow_private_network=True)
def test_private_network_supported():
return 'Private network!'

@self.app.route('/test_allow_private_network_access_unsupported')
@cross_origin(allow_private_network=False)
def test_credentials_unsupported():
return 'Private network!'

@self.app.route('/test_default')
@cross_origin()
def test_default():
return 'Open!'

def test_credentials_supported(self):
""" The specified route should return the
Access-Control-Allow-Credentials header.
"""
resp = self.get('/test_allow_private_network_access_supported', origin='www.example.com', headers={ACL_REQUEST_HEADER_PRIVATE_NETWORK:'true'})
self.assertEqual(resp.headers.get(ACL_RESPONSE_PRIVATE_NETWORK), 'true')

def test_default(self):
""" The default behavior should be to allow private network access.
"""
resp = self.get('/test_default', origin='www.example.com', headers={ACL_REQUEST_HEADER_PRIVATE_NETWORK:'true'})
self.assertTrue(ACL_RESPONSE_PRIVATE_NETWORK in resp.headers)

resp = self.get('/test_default')
self.assertFalse(ACL_RESPONSE_PRIVATE_NETWORK in resp.headers)

def test_credentials_unsupported(self):
""" If private network access is disabled, the header should never be sent."""
resp = self.get('/test_allow_private_network_access_unsupported', origin='www.example.com')
self.assertFalse(ACL_RESPONSE_PRIVATE_NETWORK in resp.headers)

resp = self.get('/test_allow_private_network_access_unsupported', origin='www.example.com', headers={ACL_REQUEST_HEADER_PRIVATE_NETWORK:'true'})
self.assertEqual(resp.headers.get(ACL_RESPONSE_PRIVATE_NETWORK), 'false')


if __name__ == "__main__":
unittest.main()