@@ -64,12 +64,39 @@ def test_connection_fail_due_to_api_error(errror_code, expected_status, config,
64
64
assert msg .startswith ("Unable to connect to Gitlab API with the provided Private Access Token" )
65
65
66
66
67
+ def test_connection_fail_due_to_api_error_oauth (oauth_config , mocker , requests_mock ):
68
+ mocker .patch ("time.sleep" )
69
+ test_response = {
70
+ "access_token" : "new_access_token" ,
71
+ "expires_in" : 7200 ,
72
+ "created_at" : 1735689600 ,
73
+ # (7200 + 1735689600).timestamp().to_rfc3339_string() = "2025-01-01T02:00:00+00:00"
74
+ "refresh_token" : "new_refresh_token" ,
75
+ }
76
+ requests_mock .post ("https://gitlab.com/oauth/token" , status_code = 200 , json = test_response )
77
+ requests_mock .get ("/api/v4/groups" , status_code = 500 )
78
+ source = SourceGitlab ()
79
+ status , msg = source .check_connection (logging .getLogger (), oauth_config )
80
+ assert status is False
81
+ assert msg .startswith ("Unable to connect to Gitlab API with the provided credentials" )
82
+
83
+
67
84
def test_connection_fail_due_to_expired_access_token_error (oauth_config , requests_mock ):
68
- expected = "Unable to refresh the `access_token`, please re-auth in Source > Settings."
85
+ expected = "Unable to refresh the `access_token`, please re-authenticate in Sources > Settings."
69
86
requests_mock .post ("https://gitlab.com/oauth/token" , status_code = 401 )
70
87
source = SourceGitlab ()
71
88
status , msg = source .check_connection (logging .getLogger ("airbyte" ), oauth_config )
72
- assert status is False , expected in msg
89
+ assert status is False
90
+ assert expected in msg
91
+
92
+
93
+ def test_connection_refresh_access_token (oauth_config , requests_mock ):
94
+ expected = "Unknown error occurred while checking the connection"
95
+ requests_mock .post ("https://gitlab.com/oauth/token" , status_code = 200 , json = {"access_token" : "new access token" })
96
+ source = SourceGitlab ()
97
+ status , msg = source .check_connection (logging .getLogger ("airbyte" ), oauth_config )
98
+ assert status is False
99
+ assert expected in msg
73
100
74
101
75
102
def test_refresh_expired_access_token_on_error (oauth_config , requests_mock ):
@@ -108,3 +135,27 @@ def test_connection_fail_due_to_config_error(mocker, api_url, deployment_env, ex
108
135
}
109
136
status , msg = source .check_connection (logging .getLogger (), config )
110
137
assert (status , msg ) == (False , expected_message )
138
+
139
+
140
+ def test_try_refresh_access_token (oauth_config , requests_mock ):
141
+ test_response = {
142
+ "access_token" : "new_access_token" ,
143
+ "expires_in" : 7200 ,
144
+ "created_at" : 1735689600 ,
145
+ # (7200 + 1735689600).timestamp().to_rfc3339_string() = "2025-01-01T02:00:00+00:00"
146
+ "refresh_token" : "new_refresh_token" ,
147
+ }
148
+ requests_mock .post ("https://gitlab.com/oauth/token" , status_code = 200 , json = test_response )
149
+
150
+ expected = {"api_url" : "gitlab.com" ,
151
+ "credentials" : {"access_token" : "new_access_token" ,
152
+ "auth_type" : "oauth2.0" ,
153
+ "client_id" : "client_id" ,
154
+ "client_secret" : "client_secret" ,
155
+ "refresh_token" : "new_refresh_token" ,
156
+ "token_expiry_date" : "2025-01-01T02:00:00+00:00" },
157
+ "start_date" : "2021-01-01T00:00:00Z" }
158
+
159
+ source = SourceGitlab ()
160
+ source ._auth_params (oauth_config )
161
+ assert source ._try_refresh_access_token (logger = logging .getLogger (), config = oauth_config ) == expected
0 commit comments