Skip to content

Commit

Permalink
Refactor WireMock mappings/files, reset before tests
Browse files Browse the repository at this point in the history
  • Loading branch information
impl committed Apr 11, 2022
1 parent c3898c9 commit fab416d
Show file tree
Hide file tree
Showing 16 changed files with 173 additions and 62 deletions.
90 changes: 90 additions & 0 deletions auth0/internal/wiremock/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package wiremock

import (
"context"
"fmt"
"net/http"
"net/url"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-retryablehttp"
)

// Client provides communciation with the admin API of a WireMock server
// instance.
type Client struct {
host string
cl *retryablehttp.Client
}

// NewManagementAPIClient creates a Go Auth0 management API client configured to
// talk to this WireMock server instance.
func (c *Client) NewManagementAPIClient() (*management.Management, error) {
return management.New(c.host, management.WithInsecure())
}

// ResetAllMappings causes the WireMock server to remove transient mappings and
// reload static mappings from disk.
func (c *Client) ResetAllMappings(ctx context.Context) (err error) {
resp, err := c.doRequest(ctx, http.MethodPost, "__admin/mappings/reset")
if err != nil {
return fmt.Errorf("failed to reset WireMock mappings: %w", err)
}
defer func() {
err = multierror.Append(err, resp.Body.Close()).ErrorOrNil()
}()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("the WireMock server returned %s when trying to reset mappings", resp.Status)
}
return nil
}

// ResetAllScenarios causes the WireMock server to put all currently defined
// mappings' scenarios back into their initial state.
func (c *Client) ResetAllScenarios(ctx context.Context) (err error) {
resp, err := c.doRequest(ctx, http.MethodPost, "__admin/scenarios/reset")
if err != nil {
return fmt.Errorf("failed to reset WireMock scenarios: %w", err)
}
defer func() {
err = multierror.Append(err, resp.Body.Close()).ErrorOrNil()
}()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("the WireMock server returned %s when trying to reset scenarios", resp.Status)
}
return nil
}

func (c *Client) doRequest(ctx context.Context, method, path string) (*http.Response, error) {
u := &url.URL{
Scheme: "http",
Host: c.host,
Path: path,
}

req, err := retryablehttp.NewRequest(method, u.String(), nil)
if err != nil {
return nil, fmt.Errorf("failed to create HTTP request: %w", err)
}

resp, err := c.cl.Do(req.WithContext(ctx))
if err != nil {
return nil, fmt.Errorf("failed to send HTTP request to WireMock: %w", err)
}
return resp, nil
}

// NewClient constructs a WireMock client that communicates with the WireMock
// server on the given host and port.
func NewClient(host string) *Client {
cl := retryablehttp.NewClient()
cl.CheckRetry = retryablehttp.ErrorPropagatedRetryPolicy

return &Client{
host: host,
cl: cl,
}
}
26 changes: 22 additions & 4 deletions auth0/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (
"os"
"sort"
"strings"
"sync"
"testing"

"github.com/auth0/go-auth0/management"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/auth0/terraform-provider-auth0/auth0/internal/wiremock"
)

var testProviderFactories = map[string]func() (*schema.Provider, error){
Expand All @@ -21,12 +25,26 @@ var testProviderFactories = map[string]func() (*schema.Provider, error){
},
}

func providerWithMockedAPI() *schema.Provider {
const wiremockHost = "localhost:8080"
var (
wireMockClient = wiremock.NewClient("localhost:8080")
wireMockReset sync.Once
)

func providerWithMockedAPI() *schema.Provider {
provider := Provider()
provider.ConfigureContextFunc = func(_ context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) {
apiClient, err := management.New(wiremockHost, management.WithInsecure())
provider.ConfigureContextFunc = func(ctx context.Context, data *schema.ResourceData) (interface{}, diag.Diagnostics) {
var err error
wireMockReset.Do(func() {
err = multierror.Append(
wireMockClient.ResetAllMappings(ctx),
wireMockClient.ResetAllScenarios(ctx),
).ErrorOrNil()
})
if err != nil {
return nil, diag.FromErr(err)
}

apiClient, err := wireMockClient.NewManagementAPIClient()
if err != nil {
return nil, diag.FromErr(err)
}
Expand Down
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ version: "3.8"

services:
auth0:
build:
context: .
dockerfile: dockerfiles/wiremock/Dockerfile
image: wiremock/wiremock:2.30.0
volumes:
- ./dockerfiles/wiremock/__files:/home/wiremock/__files
- ./dockerfiles/wiremock/mappings:/home/wiremock/mappings
command: --port 8080 --verbose
expose:
- "8080"
Expand Down
5 changes: 0 additions & 5 deletions dockerfiles/wiremock/Dockerfile

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"custom_domain_id": "cd_fakecustomdomain",
"custom_domain_id": "cd_auth0managed",
"domain": "terraform-provider.auth0.com",
"primary": true,
"status": "pending_verification",
Expand All @@ -8,7 +8,7 @@
"methods": [
{
"name": "cname",
"record": "terraform-provider-cd-fakecustomdomain.edge.tenants.eu.auth0.com"
"record": "terraform-provider-cd-auth0managed.edge.tenants.eu.auth0.com"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"custom_domain_id": "cd_fakecustomdomain",
"custom_domain_id": "cd_auth0managed",
"domain": "terraform-provider.auth0.com",
"primary": true,
"status": "ready",
"type": "auth0_managed_certs",
"origin_domain_name": "terraform-provider-cd-auth0managed.edge.tenants.eu.auth0.com",
"verification": {
"methods": [
{
"name": "cname",
"record": "terraform-provider-cd-fakecustomdomain.edge.tenants.eu.auth0.com"
"record": "terraform-provider-cd-auth0managed.edge.tenants.eu.auth0.com"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"request": {
"method": "DELETE",
"url": "/api/v2/custom-domains/cd_fakecustomdomain"
"url": "/api/v2/custom-domains/cd_auth0managed"
},
"response": {
"status": 204
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"scenarioName": "Custom domain verification with Auth0-managed certificates",
"requiredScenarioState": "Started",
"request": {
"method": "GET",
"url": "/api/v2/custom-domains/cd_auth0managed"
},
"response": {
"status": 200,
"bodyFileName": "custom_domain_verification/with_auth0_managed_certs/custom_domain_pending_verification.json",
"headers": {
"Content-Type": "application/json"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"scenarioName": "Custom domain verification with Auth0-managed certificates",
"requiredScenarioState": "Verified",
"request": {
"method": "GET",
"url": "/api/v2/custom-domains/cd_auth0managed"
},
"response": {
"status": 200,
"bodyFileName": "custom_domain_verification/with_auth0_managed_certs/custom_domain_ready.json",
"headers": {
"Content-Type": "application/json"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
"response": {
"status": 201,
"bodyFileName": "custom_domain_with_pending_verification.json",
"bodyFileName": "custom_domain_verification/with_auth0_managed_certs/custom_domain_pending_verification.json",
"headers": {
"Content-Type": "application/json"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"scenarioName": "Custom domain verification with Auth0-managed certificates",
"requiredScenarioState": "Started",
"newScenarioState": "Verified",
"request": {
"method": "POST",
"url": "/api/v2/custom-domains/cd_auth0managed/verify"
},
"response": {
"status": 201,
"bodyFileName": "custom_domain_verification/with_auth0_managed_certs/custom_domain_ready.json",
"headers": {
"Content-Type": "application/json"
}
}
}
16 changes: 0 additions & 16 deletions dockerfiles/wiremock/mappings/get_custom_domain_pending_200.json

This file was deleted.

15 changes: 0 additions & 15 deletions dockerfiles/wiremock/mappings/get_custom_domain_ready_200.json

This file was deleted.

13 changes: 0 additions & 13 deletions dockerfiles/wiremock/mappings/post_custom_domain_verify_200.json

This file was deleted.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/auth0/go-auth0 v0.6.2
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.13.0
)

Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,16 @@ github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9n
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 h1:1/D3zfFHttUKaCaGKZ/dR2roBXv0vKbSCnssIldfQdI=
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBMaudVLy8fmjf9Npq1dq9RalhveqZG5w/yz3mHWs=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v1.2.0 h1:La19f8d7WIlm4ogzNHB0JGqs5AUDAZ2UfCY4sJXcJdM=
github.com/hashicorp/go-hclog v1.2.0/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/hashicorp/go-plugin v1.4.3 h1:DXmvivbWD5qdiBts9TpBC7BYL1Aia5sxbRgQB+v6UZM=
github.com/hashicorp/go-plugin v1.4.3/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ=
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down

0 comments on commit fab416d

Please sign in to comment.