forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_app_token_test.go
More file actions
75 lines (62 loc) · 1.86 KB
/
data_source_github_app_token_test.go
File metadata and controls
75 lines (62 loc) · 1.86 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
package github
import (
"fmt"
"net/http"
"net/url"
"os"
"testing"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func TestAccGithubAppTokenDataSource(t *testing.T) {
t.Run("creates a application token without error", func(t *testing.T) {
expectedAccessToken := "W+2e/zjiMTweDAr2b35toCF+h29l7NW92rJIPvFrCJQK"
owner := "test-owner"
pemData, err := os.ReadFile(testGitHubAppPrivateKeyFile)
if err != nil {
t.Logf("Unexpected error: %s", err)
t.Fail()
}
ts := githubApiMock([]*mockResponse{
{
ExpectedUri: fmt.Sprintf("/app/installations/%s/access_tokens", testGitHubAppInstallationID),
ExpectedHeaders: map[string]string{
"Accept": "application/vnd.github.v3+json",
},
ResponseBody: fmt.Sprintf(`{"token": "%s"}`, expectedAccessToken),
StatusCode: 201,
},
})
defer ts.Close()
httpCl := http.DefaultClient
httpCl.Transport = http.DefaultTransport
client := github.NewClient(httpCl)
u, _ := url.Parse(ts.URL + "/")
client.BaseURL = u
meta := &Owner{
name: owner,
v3client: client,
}
testSchema := map[string]*schema.Schema{
"app_id": {Type: schema.TypeString},
"installation_id": {Type: schema.TypeString},
"pem_file": {Type: schema.TypeString},
"token": {Type: schema.TypeString},
}
schema := schema.TestResourceDataRaw(t, testSchema, map[string]any{
"app_id": testGitHubAppID,
"installation_id": testGitHubAppInstallationID,
"pem_file": string(pemData),
"token": "",
})
diags := dataSourceGithubAppTokenRead(t.Context(), schema, meta)
if diags.HasError() {
t.Logf("Unexpected error: %v", diags)
t.Fail()
}
if schema.Get("token") != expectedAccessToken {
t.Logf("Expected %s, got %s", expectedAccessToken, schema.Get("token"))
t.Fail()
}
})
}