-
Notifications
You must be signed in to change notification settings - Fork 541
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1343 from russellkerns/rkerns/AUTH-5403
AUTH-5403 added access custom pages
- Loading branch information
Showing
6 changed files
with
347 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
```release-note:enhancement | ||
access_application: Add support for custom pages | ||
``` | ||
|
||
```release-note:enhancement | ||
access_custom_page: Add support for custom pages | ||
``` | ||
|
||
```release-note:enhancement | ||
access_organization: add support for custom pages | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
var ErrMissingUID = errors.New("required UID missing") | ||
|
||
type AccessCustomPageType string | ||
|
||
const ( | ||
Forbidden AccessCustomPageType = "forbidden" | ||
IdentityDenied AccessCustomPageType = "identity_denied" | ||
) | ||
|
||
type AccessCustomPage struct { | ||
// The HTML content of the custom page. | ||
CustomHTML string `json:"custom_html,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
AppCount int `json:"app_count,omitempty"` | ||
Type AccessCustomPageType `json:"type,omitempty"` | ||
UID string `json:"uid,omitempty"` | ||
} | ||
|
||
type AccessCustomPageListResponse struct { | ||
Response | ||
Result []AccessCustomPage `json:"result"` | ||
ResultInfo `json:"result_info"` | ||
} | ||
|
||
type AccessCustomPageResponse struct { | ||
Response | ||
Result AccessCustomPage `json:"result"` | ||
} | ||
|
||
type ListAccessCustomPagesParams struct{} | ||
|
||
type CreateAccessCustomPageParams struct { | ||
CustomHTML string `json:"custom_html,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Type AccessCustomPageType `json:"type,omitempty"` | ||
} | ||
|
||
type UpdateAccessCustomPageParams struct { | ||
CustomHTML string `json:"custom_html,omitempty"` | ||
Name string `json:"name,omitempty"` | ||
Type AccessCustomPageType `json:"type,omitempty"` | ||
UID string `json:"uid,omitempty"` | ||
} | ||
|
||
func (api *API) ListAccessCustomPages(ctx context.Context, rc *ResourceContainer, params ListAccessCustomPagesParams) ([]AccessCustomPage, error) { | ||
uri := buildURI(fmt.Sprintf("/%s/%s/access/custom_pages", rc.Level, rc.Identifier), params) | ||
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return []AccessCustomPage{}, err | ||
} | ||
|
||
var customPagesResponse AccessCustomPageListResponse | ||
err = json.Unmarshal(res, &customPagesResponse) | ||
if err != nil { | ||
return []AccessCustomPage{}, err | ||
} | ||
return customPagesResponse.Result, nil | ||
} | ||
|
||
func (api *API) GetAccessCustomPage(ctx context.Context, rc *ResourceContainer, id string) (AccessCustomPage, error) { | ||
uri := fmt.Sprintf("/%s/%s/access/custom_pages/%s", rc.Level, rc.Identifier, id) | ||
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return AccessCustomPage{}, err | ||
} | ||
|
||
var customPageResponse AccessCustomPageResponse | ||
err = json.Unmarshal(res, &customPageResponse) | ||
if err != nil { | ||
return AccessCustomPage{}, err | ||
} | ||
return customPageResponse.Result, nil | ||
} | ||
|
||
func (api *API) CreateAccessCustomPage(ctx context.Context, rc *ResourceContainer, params CreateAccessCustomPageParams) (AccessCustomPage, error) { | ||
uri := fmt.Sprintf("/%s/%s/access/custom_pages", rc.Level, rc.Identifier) | ||
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params) | ||
if err != nil { | ||
return AccessCustomPage{}, err | ||
} | ||
|
||
var customPageResponse AccessCustomPageResponse | ||
err = json.Unmarshal(res, &customPageResponse) | ||
if err != nil { | ||
return AccessCustomPage{}, err | ||
} | ||
return customPageResponse.Result, nil | ||
} | ||
|
||
func (api *API) DeleteAccessCustomPage(ctx context.Context, rc *ResourceContainer, id string) error { | ||
uri := fmt.Sprintf("/%s/%s/access/custom_pages/%s", rc.Level, rc.Identifier, id) | ||
_, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (api *API) UpdateAccessCustomPage(ctx context.Context, rc *ResourceContainer, params UpdateAccessCustomPageParams) (AccessCustomPage, error) { | ||
if params.UID == "" { | ||
return AccessCustomPage{}, ErrMissingUID | ||
} | ||
|
||
uri := fmt.Sprintf("/%s/%s/access/custom_pages/%s", rc.Level, rc.Identifier, params.UID) | ||
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params) | ||
if err != nil { | ||
return AccessCustomPage{}, err | ||
} | ||
|
||
var customPageResponse AccessCustomPageResponse | ||
err = json.Unmarshal(res, &customPageResponse) | ||
if err != nil { | ||
return AccessCustomPage{}, err | ||
} | ||
return customPageResponse.Result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package cloudflare | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAccessCustomPages(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodGet, "HTTP method") | ||
w.Header().Set("content-type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": [ | ||
{ | ||
"name": "Forbidden", | ||
"app_count": 0, | ||
"type": "forbidden", | ||
"uid": "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc" | ||
} | ||
], | ||
"result_info": { | ||
"page": 1, | ||
"per_page": 20, | ||
"count": 1, | ||
"total_count": 1 | ||
} | ||
}`) | ||
} | ||
|
||
want := []AccessCustomPage{ | ||
{ | ||
Name: "Forbidden", | ||
AppCount: 0, | ||
Type: Forbidden, | ||
UID: "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", | ||
}, | ||
} | ||
mux.HandleFunc("/accounts/"+testAccountID+"/access/custom_pages", handler) | ||
actual, err := client.ListAccessCustomPages(context.Background(), AccountIdentifier(testAccountID), ListAccessCustomPagesParams{}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestAccessCustomPage(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodGet, "HTTP method") | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"custom_html": "<html><body><h1>Forbidden</h1></body></html>", | ||
"name": "Forbidden", | ||
"app_count": 0, | ||
"type": "forbidden", | ||
"uid": "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc" | ||
} | ||
}`) | ||
} | ||
|
||
want := AccessCustomPage{ | ||
Name: "Forbidden", | ||
AppCount: 0, | ||
Type: Forbidden, | ||
UID: "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", | ||
CustomHTML: "<html><body><h1>Forbidden</h1></body></html>", | ||
} | ||
mux.HandleFunc("/accounts/"+testAccountID+"/access/custom_pages/480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", handler) | ||
actual, err := client.GetAccessCustomPage(context.Background(), AccountIdentifier(testAccountID), "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc") | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, want, actual) | ||
} | ||
} | ||
|
||
func TestCreateAccessCustomPage(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodPost, "HTTP method") | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"custom_html": "<html><body><h1>Forbidden</h1></body></html>", | ||
"name": "Forbidden", | ||
"app_count": 0, | ||
"type": "forbidden", | ||
"uid": "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc" | ||
} | ||
}`) | ||
} | ||
|
||
customPage := AccessCustomPage{ | ||
Name: "Forbidden", | ||
AppCount: 0, | ||
Type: Forbidden, | ||
UID: "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", | ||
CustomHTML: "<html><body><h1>Forbidden</h1></body></html>", | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/access/custom_pages", handler) | ||
actual, err := client.CreateAccessCustomPage(context.Background(), AccountIdentifier(testAccountID), CreateAccessCustomPageParams{ | ||
Name: "Forbidden", | ||
Type: Forbidden, | ||
CustomHTML: "<html><body><h1>Forbidden</h1></body></html>", | ||
}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, customPage, actual) | ||
} | ||
} | ||
|
||
func TestUpdateAccessCustomPage(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodPut, "HTTP method") | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
"result": { | ||
"custom_html": "<html><body><h1>Forbidden</h1></body></html>", | ||
"name": "Forbidden", | ||
"app_count": 0, | ||
"type": "forbidden", | ||
"uid": "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc" | ||
} | ||
}`) | ||
} | ||
|
||
customPage := AccessCustomPage{ | ||
Name: "Forbidden", | ||
AppCount: 0, | ||
Type: Forbidden, | ||
UID: "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", | ||
CustomHTML: "<html><body><h1>Forbidden</h1></body></html>", | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/access/custom_pages/480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", handler) | ||
actual, err := client.UpdateAccessCustomPage(context.Background(), AccountIdentifier(testAccountID), UpdateAccessCustomPageParams{ | ||
UID: "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", | ||
Name: "Forbidden", | ||
Type: Forbidden, | ||
CustomHTML: "<html><body><h1>Forbidden</h1></body></html>", | ||
}) | ||
|
||
if assert.NoError(t, err) { | ||
assert.Equal(t, customPage, actual) | ||
} | ||
} | ||
|
||
func TestDeleteAccessCustomPage(t *testing.T) { | ||
setup() | ||
defer teardown() | ||
|
||
handler := func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, r.Method, http.MethodDelete, "HTTP method") | ||
w.Header().Set("Content-Type", "application/json") | ||
fmt.Fprintf(w, `{ | ||
"success": true, | ||
"errors": [], | ||
"messages": [], | ||
result: { | ||
"id": "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc" | ||
} | ||
}`) | ||
} | ||
|
||
mux.HandleFunc("/accounts/"+testAccountID+"/access/custom_pages/480f4f69-1a28-4fdd-9240-1ed29f0ac1dc", handler) | ||
err := client.DeleteAccessCustomPage(context.Background(), AccountIdentifier(testAccountID), "480f4f69-1a28-4fdd-9240-1ed29f0ac1dc") | ||
|
||
assert.NoError(t, err) | ||
} |
Oops, something went wrong.