aboutsummaryrefslogtreecommitdiff
path: root/provider/provider_test.go
blob: ade33a652d32e4020c1ba1c620c0122f51988863 (plain)
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
package provider_test

import (
	"encoding/json"
	"net/http/httptest"
	"testing"

	p "github.com/pulumi/pulumi-go-provider"
	"github.com/pulumi/pulumi/sdk/v3/go/property"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestConfigureRequiresCredentials(t *testing.T) {

	t.Setenv("PORKBUN_API_KEY", "")
	t.Setenv("PORKBUN_SECRET_API_KEY", "")

	server := newIntegrationServer(t)
	err := server.Configure(p.ConfigureRequest{Args: property.Map{}})
	require.Error(t, err)
	assert.Contains(t, err.Error(), "PORKBUN_API_KEY")
}

func TestConfigureReadsCredentialsFromEnvironment(t *testing.T) {
	fake := newFakePorkbun(t)
	httpSrv := httptest.NewServer(fake)
	t.Cleanup(httpSrv.Close)

	t.Setenv("PORKBUN_API_KEY", testAPIKey)
	t.Setenv("PORKBUN_SECRET_API_KEY", testSecretKey)

	server := newIntegrationServer(t)
	checked, err := server.CheckConfig(p.CheckRequest{
		Inputs: property.NewMap(map[string]property.Value{
			"baseUrl": property.New(httpSrv.URL),
		}),
	})
	require.NoError(t, err)
	require.NoError(t, server.Configure(p.ConfigureRequest{Args: checked.Inputs}))

	resp, err := server.Create(p.CreateRequest{
		Urn:        testURN,
		Properties: checkedInputs(nil),
	})
	require.NoError(t, err)
	assert.NotNil(t, fake.record("example.com", resp.ID))
}

func TestSchema(t *testing.T) {
	server := newIntegrationServer(t)
	resp, err := server.GetSchema(p.GetSchemaRequest{})
	require.NoError(t, err)

	var schema struct {
		Name   string `json:"name"`
		Config struct {
			Variables map[string]struct {
				Secret bool `json:"secret"`
			} `json:"variables"`
		} `json:"config"`
		Resources map[string]struct {
			RequiredInputs []string `json:"requiredInputs"`
		} `json:"resources"`
	}
	require.NoError(t, json.Unmarshal([]byte(resp.Schema), &schema))

	assert.Equal(t, "porkbun", schema.Name)
	require.Contains(t, schema.Resources, dnsRecordToken)
	assert.ElementsMatch(t, []string{"domain", "type", "content"},
		schema.Resources[dnsRecordToken].RequiredInputs)

	require.Contains(t, schema.Config.Variables, "apiKey")
	require.Contains(t, schema.Config.Variables, "secretApiKey")
	assert.True(t, schema.Config.Variables["apiKey"].Secret, "apiKey must be marked secret")
	assert.True(t, schema.Config.Variables["secretApiKey"].Secret, "secretApiKey must be marked secret")
}