1package cloudflare
2
3import (
4	"fmt"
5	"net/http"
6	"testing"
7	"time"
8
9	"github.com/stretchr/testify/assert"
10)
11
12func TestAccessApplications(t *testing.T) {
13	setup()
14	defer teardown()
15
16	handler := func(w http.ResponseWriter, r *http.Request) {
17		assert.Equal(t, r.Method, "GET", "Expected method 'GET', got %s", r.Method)
18		w.Header().Set("content-type", "application/json")
19		fmt.Fprintf(w, `{
20			"success": true,
21			"errors": [],
22			"messages": [],
23			"result": [
24				{
25					"id": "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
26					"created_at": "2014-01-01T05:20:00.12345Z",
27					"updated_at": "2014-01-01T05:20:00.12345Z",
28					"aud": "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
29					"name": "Admin Site",
30					"domain": "test.example.com/admin",
31					"session_duration": "24h"
32				}
33			],
34			"result_info": {
35				"page": 1,
36				"per_page": 20,
37				"count": 1,
38				"total_count": 2000
39			}
40		}
41		`)
42	}
43
44	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/access/apps", handler)
45	createdAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
46	updatedAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
47
48	want := []AccessApplication{AccessApplication{
49		ID:              "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
50		CreatedAt:       &createdAt,
51		UpdatedAt:       &updatedAt,
52		AUD:             "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
53		Name:            "Admin Site",
54		Domain:          "test.example.com/admin",
55		SessionDuration: "24h",
56	}}
57
58	actual, _, err := client.AccessApplications("01a7362d577a6c3019a474fd6f485823", PaginationOptions{})
59
60	if assert.NoError(t, err) {
61		assert.Equal(t, want, actual)
62	}
63}
64
65func TestAccessApplication(t *testing.T) {
66	setup()
67	defer teardown()
68
69	handler := func(w http.ResponseWriter, r *http.Request) {
70		assert.Equal(t, r.Method, "GET", "Expected method 'GET', got %s", r.Method)
71		w.Header().Set("content-type", "application/json")
72		fmt.Fprintf(w, `{
73			"success": true,
74			"errors": [],
75			"messages": [],
76			"result": {
77				"id": "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
78				"created_at": "2014-01-01T05:20:00.12345Z",
79				"updated_at": "2014-01-01T05:20:00.12345Z",
80				"aud": "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
81				"name": "Admin Site",
82				"domain": "test.example.com/admin",
83				"session_duration": "24h"
84			}
85		}
86		`)
87	}
88
89	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/access/apps/480f4f69-1a28-4fdd-9240-1ed29f0ac1db", handler)
90
91	createdAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
92	updatedAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
93
94	want := AccessApplication{
95		ID:              "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
96		CreatedAt:       &createdAt,
97		UpdatedAt:       &updatedAt,
98		AUD:             "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
99		Name:            "Admin Site",
100		Domain:          "test.example.com/admin",
101		SessionDuration: "24h",
102	}
103
104	actual, err := client.AccessApplication("01a7362d577a6c3019a474fd6f485823", "480f4f69-1a28-4fdd-9240-1ed29f0ac1db")
105
106	if assert.NoError(t, err) {
107		assert.Equal(t, want, actual)
108	}
109}
110
111func TestCreateAccessApplications(t *testing.T) {
112	setup()
113	defer teardown()
114
115	handler := func(w http.ResponseWriter, r *http.Request) {
116		assert.Equal(t, r.Method, "POST", "Expected method 'POST', got %s", r.Method)
117		w.Header().Set("content-type", "application/json")
118		fmt.Fprintf(w, `{
119			"success": true,
120			"errors": [],
121			"messages": [],
122			"result": {
123				"id": "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
124				"created_at": "2014-01-01T05:20:00.12345Z",
125				"updated_at": "2014-01-01T05:20:00.12345Z",
126				"aud": "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
127				"name": "Admin Site",
128				"domain": "test.example.com/admin",
129				"session_duration": "24h"
130			}
131		}
132		`)
133	}
134
135	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/access/apps", handler)
136
137	actual, err := client.CreateAccessApplication(
138		"01a7362d577a6c3019a474fd6f485823",
139		AccessApplication{
140			Name:            "Admin Site",
141			Domain:          "test.example.com/admin",
142			SessionDuration: "24h",
143		},
144	)
145
146	createdAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
147	updatedAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
148	fullAccessApplication := AccessApplication{
149		ID:              "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
150		Name:            "Admin Site",
151		Domain:          "test.example.com/admin",
152		SessionDuration: "24h",
153		AUD:             "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
154		CreatedAt:       &createdAt,
155		UpdatedAt:       &updatedAt,
156	}
157
158	if assert.NoError(t, err) {
159		assert.Equal(t, fullAccessApplication, actual)
160	}
161}
162
163func TestUpdateAccessApplication(t *testing.T) {
164	setup()
165	defer teardown()
166
167	handler := func(w http.ResponseWriter, r *http.Request) {
168		assert.Equal(t, r.Method, "PUT", "Expected method 'PUT', got %s", r.Method)
169		w.Header().Set("content-type", "application/json")
170		fmt.Fprintf(w, `{
171			"success": true,
172			"errors": [],
173			"messages": [],
174			"result": {
175				"id": "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
176				"created_at": "2014-01-01T05:20:00.12345Z",
177				"updated_at": "2014-01-01T05:20:00.12345Z",
178				"aud": "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
179				"name": "Admin Site",
180				"domain": "test.example.com/admin",
181				"session_duration": "24h"
182			}
183		}
184		`)
185	}
186
187	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/access/apps/480f4f69-1a28-4fdd-9240-1ed29f0ac1db", handler)
188
189	createdAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
190	updatedAt, _ := time.Parse(time.RFC3339, "2014-01-01T05:20:00.12345Z")
191	fullAccessApplication := AccessApplication{
192		ID:              "480f4f69-1a28-4fdd-9240-1ed29f0ac1db",
193		Name:            "Admin Site",
194		Domain:          "test.example.com/admin",
195		SessionDuration: "24h",
196		AUD:             "737646a56ab1df6ec9bddc7e5ca84eaf3b0768850f3ffb5d74f1534911fe3893",
197		CreatedAt:       &createdAt,
198		UpdatedAt:       &updatedAt,
199	}
200
201	actual, err := client.UpdateAccessApplication(
202		"01a7362d577a6c3019a474fd6f485823",
203		fullAccessApplication,
204	)
205
206	if assert.NoError(t, err) {
207		assert.Equal(t, fullAccessApplication, actual)
208	}
209}
210
211func TestUpdateAccessApplicationWithMissingID(t *testing.T) {
212	setup()
213	defer teardown()
214
215	_, err := client.UpdateAccessApplication("d56084adb405e0b7e32c52321bf07be6", AccessApplication{})
216	assert.EqualError(t, err, "access application ID cannot be empty")
217}
218
219func TestDeleteAccessApplication(t *testing.T) {
220	setup()
221	defer teardown()
222
223	handler := func(w http.ResponseWriter, r *http.Request) {
224		assert.Equal(t, r.Method, "DELETE", "Expected method 'DELETE', got %s", r.Method)
225		w.Header().Set("content-type", "application/json")
226		fmt.Fprintf(w, `{
227			"success": true,
228			"errors": [],
229			"messages": [],
230			"result": {
231				"id": "699d98642c564d2e855e9661899b7252"
232			}
233		}
234		`)
235	}
236
237	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/access/apps/480f4f69-1a28-4fdd-9240-1ed29f0ac1db", handler)
238	err := client.DeleteAccessApplication("01a7362d577a6c3019a474fd6f485823", "480f4f69-1a28-4fdd-9240-1ed29f0ac1db")
239
240	assert.NoError(t, err)
241}
242
243func TestRevokeAccessApplicationTokens(t *testing.T) {
244	setup()
245	defer teardown()
246
247	handler := func(w http.ResponseWriter, r *http.Request) {
248		assert.Equal(t, r.Method, "POST", "Expected method 'POST', got %s", r.Method)
249		w.Header().Set("content-type", "application/json")
250		fmt.Fprintf(w, `{
251			"success": true,
252			"errors": [],
253			"messages": []
254		}
255		`)
256	}
257
258	mux.HandleFunc("/zones/01a7362d577a6c3019a474fd6f485823/access/apps/480f4f69-1a28-4fdd-9240-1ed29f0ac1db/revoke-tokens", handler)
259	err := client.RevokeAccessApplicationTokens("01a7362d577a6c3019a474fd6f485823", "480f4f69-1a28-4fdd-9240-1ed29f0ac1db")
260
261	assert.NoError(t, err)
262}
263