|
4 | 4 | package provider
|
5 | 5 |
|
6 | 6 | import (
|
7 |
| - "fmt" |
8 |
| - "regexp" |
| 7 | + "net/http" |
9 | 8 | "testing"
|
10 | 9 |
|
11 | 10 | "github.com/hashicorp/terraform-plugin-testing/helper/resource"
|
| 11 | + "github.com/supabase/cli/pkg/api" |
| 12 | + "gopkg.in/h2non/gock.v1" |
12 | 13 | )
|
13 | 14 |
|
14 | 15 | func TestAccProjectAPIKeysDataSource(t *testing.T) {
|
| 16 | + // Setup mock api |
| 17 | + defer gock.OffAll() |
| 18 | + gock.New("https://api.supabase.com"). |
| 19 | + Get("/v1/projects/mayuaycdtijbctgqbycg/api-keys"). |
| 20 | + Times(3). |
| 21 | + Reply(http.StatusOK). |
| 22 | + JSON([]api.ApiKeyResponse{ |
| 23 | + { |
| 24 | + Name: "anon", |
| 25 | + ApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.anon", |
| 26 | + }, |
| 27 | + { |
| 28 | + Name: "service_role", |
| 29 | + ApiKey: "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.service_role", |
| 30 | + }, |
| 31 | + }) |
| 32 | + |
15 | 33 | resource.Test(t, resource.TestCase{
|
16 | 34 | PreCheck: func() { testAccPreCheck(t) },
|
17 | 35 | ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
|
18 | 36 | Steps: []resource.TestStep{
|
19 | 37 | // Read testing
|
20 | 38 | {
|
21 |
| - Config: testAccProjectAPIKeysDataSourceConfig("example-project-id"), |
| 39 | + Config: testAccProjectAPIKeysDataSourceConfig, |
22 | 40 | Check: resource.ComposeAggregateTestCheckFunc(
|
23 |
| - resource.TestCheckResourceAttr("data.supabase_project_apikeys.test", "project_id", "example-project-id"), |
24 |
| - resource.TestCheckResourceAttrSet("data.supabase_project_apikeys.test", "anon_key"), |
25 |
| - resource.TestCheckResourceAttrSet("data.supabase_project_apikeys.test", "service_role_key"), |
| 41 | + resource.TestCheckResourceAttr("data.supabase_project_apikeys.production", "anon_key", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.anon"), |
| 42 | + resource.TestCheckResourceAttr("data.supabase_project_apikeys.production", "service_role_key", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.service_role"), |
26 | 43 | ),
|
27 | 44 | },
|
28 | 45 | },
|
29 | 46 | })
|
30 | 47 | }
|
31 | 48 |
|
32 |
| -func testAccProjectAPIKeysDataSourceConfig(projectID string) string { |
33 |
| - return fmt.Sprintf(` |
34 |
| -data "supabase_project_apikeys" "test" { |
35 |
| - project_id = %[1]q |
36 |
| -} |
37 |
| -`, projectID) |
38 |
| -} |
39 |
| - |
40 |
| -func TestAccProjectAPIKeysDataSource_NotFound(t *testing.T) { |
41 |
| - resource.Test(t, resource.TestCase{ |
42 |
| - PreCheck: func() { testAccPreCheck(t) }, |
43 |
| - ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, |
44 |
| - Steps: []resource.TestStep{ |
45 |
| - { |
46 |
| - Config: testAccProjectAPIKeysDataSourceConfig("non-existent-project"), |
47 |
| - ExpectError: regexp.MustCompile("Unable to read project API keys"), |
48 |
| - }, |
49 |
| - }, |
50 |
| - }) |
| 49 | +const testAccProjectAPIKeysDataSourceConfig = ` |
| 50 | +data "supabase_project_apikeys" "production" { |
| 51 | + project_id = "mayuaycdtijbctgqbycg" |
51 | 52 | }
|
| 53 | +` |
0 commit comments