-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathuser_data_test.go
123 lines (102 loc) · 4.04 KB
/
user_data_test.go
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package provider
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
func testUserDataSourceConfig_basic() string {
return fmt.Sprintln(`
data "firehydrant_user" "test_user" {
email = "[email protected]"
}`)
}
func TestUserDataSource_OneMatch(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/ping" && r.URL.Path != "/users" {
t.Errorf("Expected to request '/ping' or '/users', got: %s", r.URL.Path)
}
if r.URL.Path == "/users" && r.URL.Query().Get("query") != "[email protected]" {
t.Errorf("Expected query param 'query' to be '[email protected]', got: %s", r.URL.Query().Get("query"))
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data":[{"id": "123", "name": "Test Testerson", "email":"[email protected]"}]}`))
}))
defer server.Close()
orig := os.Getenv("FIREHYDRANT_BASE_URL")
os.Setenv("FIREHYDRANT_BASE_URL", server.URL)
t.Cleanup(func() { os.Setenv("FIREHYDRANT_BASE_URL", orig) })
resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
Steps: []resource.TestStep{
{
Config: testUserDataSourceConfig_basic(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrSet("data.firehydrant_user.test_user", "id"),
resource.TestCheckResourceAttr(
"data.firehydrant_user.test_user", "email", "[email protected]"),
resource.TestCheckResourceAttr(
"data.firehydrant_user.test_user", "id", "123"),
resource.TestCheckResourceAttr(
"data.firehydrant_user.test_user", "name", "Test Testerson"),
),
},
},
})
}
func TestUserDataSource_MultipleMatches(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/ping" && r.URL.Path != "/users" {
t.Errorf("Expected to request '/ping' or '/users', got: %s", r.URL.Path)
}
if r.URL.Path == "/users" && r.URL.Query().Get("query") != "[email protected]" {
t.Errorf("Expected query param 'query' to be '[email protected]', got: %s", r.URL.Query().Get("query"))
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data":[{"id": "123", "email":"[email protected]", "name": "Test Testerson"},{"id": "456", "email":"[email protected]", "name": "Bob Testerson"}]}`))
}))
defer server.Close()
orig := os.Getenv("FIREHYDRANT_BASE_URL")
os.Setenv("FIREHYDRANT_BASE_URL", server.URL)
t.Cleanup(func() { os.Setenv("FIREHYDRANT_BASE_URL", orig) })
resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
Steps: []resource.TestStep{
{
Config: testUserDataSourceConfig_basic(),
ExpectError: regexp.MustCompile(`Found multiple matching users for '[email protected]'`),
},
},
})
}
func TestUserDataSource_NoMatches(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/ping" && r.URL.Path != "/users" {
t.Errorf("Expected to request '/ping' or '/users', got: %s", r.URL.Path)
}
if r.URL.Path == "/users" && r.URL.Query().Get("query") != "[email protected]" {
t.Errorf("Expected query param 'query' to be '[email protected]', got: %s", r.URL.Query().Get("query"))
}
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"data":[]}`))
}))
defer server.Close()
orig := os.Getenv("FIREHYDRANT_BASE_URL")
os.Setenv("FIREHYDRANT_BASE_URL", server.URL)
t.Cleanup(func() { os.Setenv("FIREHYDRANT_BASE_URL", orig) })
resource.Test(t, resource.TestCase{
PreCheck: func() { testFireHydrantIsSetup(t) },
ProviderFactories: defaultProviderFactories(),
Steps: []resource.TestStep{
{
Config: testUserDataSourceConfig_basic(),
ExpectError: regexp.MustCompile(`Did not find user matching '[email protected]'`),
},
},
})
}