forked from databrickslabs/brickster
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-request-helpers.R
More file actions
232 lines (208 loc) · 5.73 KB
/
Copy pathtest-request-helpers.R
File metadata and controls
232 lines (208 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
local_clear_auth_env <- function() {
withr::local_envvar(
c(
DATABRICKS_CONFIG_FILE = NA_character_,
DATABRICKS_CONFIG_PROFILE = NA_character_,
ARM_CLIENT_ID = NA_character_,
ARM_CLIENT_SECRET = NA_character_,
ARM_TENANT_ID = NA_character_
),
.local_envir = parent.frame()
)
withr::local_options(
use_databrickscfg = FALSE,
db_profile = NULL,
.local_envir = parent.frame()
)
}
local_error_response <- function(
body,
content_type = "application/json",
status = 400
) {
httr2::response(
status_code = status,
headers = list("content-type" = content_type),
body = charToRaw(body)
)
}
test_that("request helpers - building requests", {
local_clear_auth_env()
host <- "some_url"
token <- "some_token"
endpoint <- "clusters/create"
endpoint_version <- "2.0"
method <- "POST"
body <- list(a = 1, b = 2)
expect_no_condition({
req <- db_request(
endpoint = endpoint,
method = method,
version = endpoint_version,
body = body,
host = host,
token = token
)
})
expect_s3_class(req, "httr2_request")
expect_identical(
req$url,
paste("https://", host, "/api/", endpoint_version, "/", endpoint, sep = "")
)
expect_identical(req$method, method)
expect_identical(req$body$data, body)
expect_no_error(req$options$useragent)
expect_equal(req$policies$retry_max_tries, 3)
expect_true(req$policies$retry_on_failure)
req_json <- db_request_json(req)
expect_equal(unclass(req_json), "{\"a\":1,\"b\":2}")
expect_s3_class(req_json, "json")
expect_null(db_request_json(NULL))
})
test_that("request error body handles standard Databricks JSON errors", {
resp <- local_error_response(
paste0(
'{"error_code":"PERMISSION_DENIED",',
'"message":"You do not have permission to use the SQL Warehouse."}'
),
status = 403
)
expect_identical(
db_req_error_body(resp),
"PERMISSION_DENIED: You do not have permission to use the SQL Warehouse."
)
})
test_that("request error body handles non-JSON Databricks errors", {
text_html_resp <- local_error_response(
"Invalid Token",
content_type = "text/html; charset=utf-8"
)
string_resp <- local_error_response(
"DEADLINE_EXCEEDED: Deadline exceeded when awaiting statement ID",
content_type = "text/plain"
)
empty_resp <- local_error_response(
"",
content_type = "text/html; charset=utf-8",
status = 504
)
expect_identical(db_req_error_body(text_html_resp), "Invalid Token")
expect_identical(
db_req_error_body(string_resp),
"DEADLINE_EXCEEDED: Deadline exceeded when awaiting statement ID"
)
expect_identical(db_req_error_body(empty_resp), "Gateway Timeout")
})
test_that("request helpers - m2m auth flow", {
local_clear_auth_env()
host <- "some_url"
endpoint <- "clusters/create"
endpoint_version <- "2.0"
method <- "POST"
body <- list(a = 1, b = 2)
withr::local_envvar(
DATABRICKS_CLIENT_ID = "client-id",
DATABRICKS_CLIENT_SECRET = "client-secret"
)
req <- db_request(
endpoint = endpoint,
method = method,
version = endpoint_version,
body = body,
host = host,
token = NULL
)
expect_identical(
req$policies$auth_sign$params$flow,
"oauth_flow_client_credentials"
)
expect_identical(
req$policies$auth_sign$params$flow_params$scope,
"all-apis"
)
expect_identical(
req$policies$auth_sign$params$flow_params$client$id,
"client-id"
)
expect_identical(req$policies$auth_sign$params$expiry_margin, 40)
})
test_that("request helpers isolate OAuth clients and tokens by workspace", {
local_clear_auth_env()
withr::local_envvar(
DATABRICKS_CLIENT_ID = "client-id",
DATABRICKS_CLIENT_SECRET = "client-secret"
)
req_a <- db_request(
endpoint = "clusters/list",
method = "GET",
version = "2.0",
host = "workspace-a.cloud.databricks.com",
token = NULL
)
req_b <- db_request(
endpoint = "clusters/list",
method = "GET",
version = "2.0",
host = "workspace-b.cloud.databricks.com",
token = NULL
)
client_a <- req_a$policies$auth_sign$params$flow_params$client
client_b <- req_b$policies$auth_sign$params$flow_params$client
expect_identical(
client_a$token_url,
"https://workspace-a.cloud.databricks.com/oidc/v1/token"
)
expect_identical(
client_b$token_url,
"https://workspace-b.cloud.databricks.com/oidc/v1/token"
)
expect_false(identical(client_a$name, client_b$name))
cache_a <- req_a$policies$auth_sign$cache
cache_b <- req_b$policies$auth_sign$cache
cache_a$clear()
cache_b$clear()
withr::defer(cache_a$clear())
withr::defer(cache_b$clear())
cache_a$set(httr2::oauth_token(
access_token = "workspace-a-token",
expires_in = 3600
))
expect_null(cache_b$get())
})
test_that("request helpers - azure m2m auth flow", {
local_clear_auth_env()
host <- "some_url"
endpoint <- "clusters/create"
endpoint_version <- "2.0"
method <- "POST"
body <- list(a = 1, b = 2)
withr::local_envvar(
ARM_CLIENT_ID = "azure-client-id",
ARM_CLIENT_SECRET = "azure-client-secret",
ARM_TENANT_ID = "azure-tenant-id"
)
req <- db_request(
endpoint = endpoint,
method = method,
version = endpoint_version,
body = body,
host = host,
token = NULL
)
expect_identical(
req$policies$auth_sign$params$flow,
"oauth_flow_client_credentials"
)
expect_identical(
req$policies$auth_sign$params$flow_params$scope,
"2ff814a6-3304-4ab8-85cb-cd0e6f879c1d/.default"
)
expect_identical(
req$policies$auth_sign$params$flow_params$token_params,
list()
)
expect_identical(
req$policies$auth_sign$params$flow_params$client$token_url,
"https://login.microsoftonline.com/azure-tenant-id/oauth2/v2.0/token"
)
})