(t *testing.T)
| 1543 | } |
| 1544 | |
| 1545 | func TestOAuth2WithFile(t *testing.T) { |
| 1546 | var expectedAuth *string |
| 1547 | var previousAuth string |
| 1548 | tokenTS := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1549 | auth := r.Header.Get("Authorization") |
| 1550 | if auth != *expectedAuth { |
| 1551 | t.Fatalf("bad auth, expected %s, got %s", *expectedAuth, auth) |
| 1552 | } |
| 1553 | if auth == previousAuth { |
| 1554 | t.Fatal("token endpoint called twice") |
| 1555 | } |
| 1556 | previousAuth = auth |
| 1557 | res, _ := json.Marshal(oauth2TestServerResponse{ |
| 1558 | AccessToken: "12345", |
| 1559 | TokenType: "Bearer", |
| 1560 | }) |
| 1561 | w.Header().Add("Content-Type", "application/json") |
| 1562 | _, _ = w.Write(res) |
| 1563 | })) |
| 1564 | defer tokenTS.Close() |
| 1565 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 1566 | auth := r.Header.Get("Authorization") |
| 1567 | if auth != "Bearer 12345" { |
| 1568 | t.Fatalf("bad auth, expected %s, got %s", "Bearer 12345", auth) |
| 1569 | } |
| 1570 | fmt.Fprintln(w, "Hello, client") |
| 1571 | })) |
| 1572 | defer ts.Close() |
| 1573 | |
| 1574 | secretFile, err := os.CreateTemp("", "oauth2_secret") |
| 1575 | if err != nil { |
| 1576 | t.Fatal(err) |
| 1577 | } |
| 1578 | defer os.Remove(secretFile.Name()) |
| 1579 | |
| 1580 | yamlConfig := fmt.Sprintf(` |
| 1581 | client_id: 1 |
| 1582 | client_secret_file: %s |
| 1583 | scopes: |
| 1584 | - A |
| 1585 | - B |
| 1586 | token_url: %s |
| 1587 | endpoint_params: |
| 1588 | hi: hello |
| 1589 | `, secretFile.Name(), tokenTS.URL) |
| 1590 | expectedConfig := OAuth2{ |
| 1591 | ClientID: "1", |
| 1592 | ClientSecretFile: secretFile.Name(), |
| 1593 | Scopes: []string{"A", "B"}, |
| 1594 | EndpointParams: map[string]string{"hi": "hello"}, |
| 1595 | TokenURL: tokenTS.URL, |
| 1596 | } |
| 1597 | |
| 1598 | var unmarshalledConfig OAuth2 |
| 1599 | err = yaml.Unmarshal([]byte(yamlConfig), &unmarshalledConfig) |
| 1600 | if err != nil { |
| 1601 | t.Fatalf("Expected no error unmarshalling yaml, got %v", err) |
| 1602 | } |
nothing calls this directly
no test coverage detected