Debian Codegraph @

tests/contrib/test_gitlab.py

raw
1 import pytest
2 import responses
3 from flask import Flask
4 from urlobject import URLObject
5
6 from flask_dance.consumer import OAuth2ConsumerBlueprint
7 from flask_dance.consumer.storage import MemoryStorage
8 from flask_dance.contrib.gitlab import gitlab, make_gitlab_blueprint
9
10
11 @pytest.fixture
12 def make_app():
13 "A callable to create a Flask app with the GitLab provider"
14
15 def _make_app(*args, **kwargs):
16 app = Flask(__name__)
17 app.config["SERVER_NAME"] = "a.b.c"
18 app.secret_key = "whatever"
19 blueprint = make_gitlab_blueprint(*args, **kwargs)
20 app.register_blueprint(blueprint)
21 return app
22
23 return _make_app
24
25
26 def test_blueprint_factory_default():
27 # Test with gitlab.com
28 glbp = make_gitlab_blueprint(
29 client_id="foo", client_secret="bar", scope="read_user", redirect_to="index"
30 )
31 assert isinstance(glbp, OAuth2ConsumerBlueprint)
32 assert glbp.session.scope == "read_user"
33 assert glbp.session.base_url == "https://gitlab.com/api/v4/"
34 assert glbp.session.client_id == "foo"
35 assert glbp.client_secret == "bar"
36 assert glbp.authorization_url == "https://gitlab.com/oauth/authorize"
37 assert glbp.token_url == "https://gitlab.com/oauth/token"
38
39
40 def test_blueprint_factory_custom():
41 glbp = make_gitlab_blueprint(
42 client_id="foo",
43 client_secret="bar",
44 scope="read_user",
45 redirect_to="index",
46 hostname="git.example.com",
47 )
48 assert isinstance(glbp, OAuth2ConsumerBlueprint)
49 assert glbp.session.scope == "read_user"
50 assert glbp.session.base_url == "https://git.example.com/api/v4/"
51 assert glbp.session.client_id == "foo"
52 assert glbp.client_secret == "bar"
53 assert glbp.authorization_url == "https://git.example.com/oauth/authorize"
54 assert glbp.token_url == "https://git.example.com/oauth/token"
55
56
57 def test_load_from_config(make_app):
58 app = make_app()
59 app.config["GITLAB_OAUTH_CLIENT_ID"] = "foo"
60 app.config["GITLAB_OAUTH_CLIENT_SECRET"] = "bar"
61
62 resp = app.test_client().get("/gitlab")
63 url = resp.headers["Location"]
64 client_id = URLObject(url).query.dict.get("client_id")
65 assert client_id == "foo"
66
67
68 @responses.activate
69 def test_context_local(make_app):
70 responses.add(responses.GET, "https://google.com")
71
72 # set up two apps with two different set of auth tokens
73 app1 = make_app(
74 "foo1",
75 "bar1",
76 redirect_to="url1",
77 storage=MemoryStorage({"access_token": "app1"}),
78 )
79 app2 = make_app(
80 "foo2",
81 "bar2",
82 redirect_to="url2",
83 storage=MemoryStorage({"access_token": "app2"}),
84 )
85
86 # outside of a request context, referencing functions on the `gitlab` object
87 # will raise an exception
88 with pytest.raises(RuntimeError):
89 gitlab.get("https://google.com")
90
91 # inside of a request context, `gitlab` should be a proxy to the correct
92 # blueprint session
93 with app1.test_request_context("/"):
94 app1.preprocess_request()
95 gitlab.get("https://google.com")
96 request = responses.calls[0].request
97 assert request.headers["Authorization"] == "Bearer app1"
98
99 with app2.test_request_context("/"):
100 app2.preprocess_request()
101 gitlab.get("https://google.com")
102 request = responses.calls[1].request
103 assert request.headers["Authorization"] == "Bearer app2"
104
105
106 def test_no_verify_token(make_app, mocker):
107 app = make_app(
108 "foo",
109 "bar",
110 hostname="my-insecure-gitlab.com",
111 storage=MemoryStorage(),
112 verify_tls_certificates=False,
113 )
114
115 with responses.RequestsMock() as rsps:
116 mock_on_request = mocker.patch.object(
117 rsps, "_on_request", wraps=rsps._on_request
118 )
119 rsps.add(
120 responses.POST,
121 "https://my-insecure-gitlab.com/oauth/token",
122 body='{"access_token":"foobar","token_type":"bearer","scope":"admin"}',
123 )
124 with app.test_client() as client:
125 # reset the session before the request
126 with client.session_transaction() as sess:
127 sess["gitlab_oauth_state"] = "random-string"
128 # make the request
129 resp = client.get(
130 "/gitlab/authorized?code=secret-code&state=random-string",
131 base_url="https://a.b.c",
132 )
133 # check that `verify=False` in the token request
134 mock_on_request.assert_called()
135 call_verify = mock_on_request.call_args[1].get("verify", True)
136 assert call_verify is False
137
138
139 def test_no_verify_api_call(make_app, mocker):
140 app = make_app(
141 "foo",
142 "bar",
143 hostname="my-insecure-gitlab.com",
144 storage=MemoryStorage({"access_token": "fake-token"}),
145 verify_tls_certificates=False,
146 )
147
148 with responses.RequestsMock() as rsps:
149 mock_on_request = mocker.patch.object(
150 rsps, "_on_request", wraps=rsps._on_request
151 )
152 rsps.add(
153 method=responses.GET,
154 url="https://my-insecure-gitlab.com",
155 body="insecure but OK",
156 )
157 with app.test_request_context("/"):
158 app.preprocess_request()
159 gitlab.get("https://my-insecure-gitlab.com")
160
161 # check that `verify=False` in the API call
162 mock_on_request.assert_called()
163 call_verify = mock_on_request.call_args[1].get("verify", True)
164 assert call_verify is False