File: test-oauth-flow-jwt.R

package info (click to toggle)
r-cran-httr2 1.1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,604 kB
  • sloc: sh: 21; makefile: 2
file content (41 lines) | stat: -rw-r--r-- 1,354 bytes parent folder | download | duplicates (2)
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
# 1. Create service account
# 2. Add key and download json
# 3. json <- jsonlite::read_json(path)
# 4. secret_write_rds(json, "tests/testthat/test-oauth-flow-jwt-google.rds", "HTTR2_KEY")

test_that("can generate token and use it automatically", {
  secrets <- secret_read_rds(test_path("test-oauth-flow-jwt-google.rds"), "HTTR2_KEY")

  client <- oauth_client(
    id = secrets$client_id,
    key = secrets$private_key,
    token_url = secrets$token_uri,
    auth = "body"
  )
  claim <- list(
    iss = secrets$client_email,
    scope = "https://www.googleapis.com/auth/userinfo.email",
    aud = "https://oauth2.googleapis.com/token"
  )

  # Can generate token
  token <- oauth_flow_bearer_jwt(client, claim)
  expect_s3_class(token, "httr2_token")

  # Can use it in request
  resp <- request("https://openidconnect.googleapis.com/v1/userinfo") %>%
    req_oauth_bearer_jwt(client, claim) %>%
    req_perform() %>%
    resp_body_json()

  expect_type(resp, "list")
  expect_equal(resp$email_verified, TRUE)
})

test_that("validates inputs", {
  client1 <- oauth_client("test", "http://example.com")
  expect_snapshot(oauth_flow_bearer_jwt(client1), error = TRUE)

  client2 <- oauth_client("test", "http://example.com", key = "abc", auth_params = list(claim = "123"))
  expect_snapshot(oauth_flow_bearer_jwt(client2, claim = NULL), error = TRUE)
})