// Code generated by Fern. DO NOT EDIT.

package user_attribute_profiles_test

import (
	context "context"
	fmt "fmt"
	management "github.com/auth0/go-auth0/v2/management"
	client "github.com/auth0/go-auth0/v2/management/client"
	option "github.com/auth0/go-auth0/v2/management/option"
	require "github.com/stretchr/testify/require"
	gowiremock "github.com/wiremock/go-wiremock"
	wiremocktestcontainersgo "github.com/wiremock/wiremock-testcontainers-go"
	http "net/http"
	os "os"
	testing "testing"
)

// TestMain sets up shared test fixtures for all tests in this package// Global test fixtures
var (
	WireMockContainer *wiremocktestcontainersgo.WireMockContainer
	WireMockBaseURL   string
	WireMockClient    *gowiremock.Client
)

// TestMain sets up shared test fixtures for all tests in this package
func TestMain(m *testing.M) {
	// Setup shared WireMock container
	ctx := context.Background()
	container, err := wiremocktestcontainersgo.RunContainerAndStopOnCleanup(
		ctx,
		&testing.T{},
		wiremocktestcontainersgo.WithImage("docker.io/wiremock/wiremock:3.9.1"),
	)
	if err != nil {
		fmt.Printf("Failed to start WireMock container: %v\n", err)
		os.Exit(1)
	}

	// Store global references
	WireMockContainer = container

	// Try to get the base URL using the standard method first
	baseURL, err := container.Endpoint(ctx, "")
	if err == nil {
		// Standard method worked (running outside DinD)
		// This uses the mapped port (e.g., localhost:59553)
		WireMockBaseURL = "http://" + baseURL
		WireMockClient = container.Client
	} else {
		// Standard method failed, use internal IP fallback (DinD environment)
		fmt.Printf("Standard endpoint resolution failed, using internal IP fallback: %v\n", err)

		inspect, err := container.Inspect(ctx)
		if err != nil {
			fmt.Printf("Failed to inspect WireMock container: %v\n", err)
			os.Exit(1)
		}

		// Find the IP address from the container's networks
		var containerIP string
		for _, network := range inspect.NetworkSettings.Networks {
			if network.IPAddress != "" {
				containerIP = network.IPAddress
				break
			}
		}

		if containerIP == "" {
			fmt.Printf("Failed to get WireMock container IP address\n")
			os.Exit(1)
		}

		// In DinD, use the internal port directly (8080 for WireMock HTTP)
		// Don't use the mapped port since it doesn't exist in this environment
		WireMockBaseURL = fmt.Sprintf("http://%s:8080", containerIP)

		// The container.Client was created with a bad URL, so we need a new one
		WireMockClient = gowiremock.NewClient(WireMockBaseURL)
	}

	fmt.Printf("WireMock available at: %s\n", WireMockBaseURL)

	// Run all tests
	code := m.Run()

	// Cleanup
	if WireMockContainer != nil {
		WireMockContainer.Terminate(ctx)
	}

	// Exit with the same code as the tests
	os.Exit(code)
}

func TestUserAttributeProfilesListWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Get(gowiremock.URLPathTemplate("/user-attribute-profiles")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"next": "next", "user_attribute_profiles": []interface{}{map[string]interface{}{"id": "id", "name": "name", "user_attributes": map[string]interface{}{"key": map[string]interface{}{"description": "description", "label": "label", "profile_required": true, "auth0_mapping": "auth0_mapping"}}}}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.ListUserAttributeProfileRequestParameters{
		From: management.String(
			"from",
		),
		Take: management.Int(
			1,
		),
	}
	_, invocationErr := client.UserAttributeProfiles.List(
		context.TODO(),
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUserAttributeProfilesCreateWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Post(gowiremock.URLPathTemplate("/user-attribute-profiles")).WithBodyPattern(gowiremock.MatchesJsonSchema(`{
                    "$schema": "https://json-schema.org/draft/2020-12/schema",
                    "type": "object",
                    "required": ["name", "user_attributes"],
                    "properties": {
                        "name": {"type": "string"}, "user_attributes": {"type": "object", "additionalProperties": {"type": "object"}}
                    },
                    "additionalProperties": true
                }`, "V202012")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"id": "id", "name": "name", "user_id": map[string]interface{}{"oidc_mapping": "sub", "saml_mapping": []interface{}{"saml_mapping"}, "scim_mapping": "scim_mapping"}, "user_attributes": map[string]interface{}{"key": map[string]interface{}{"description": "description", "label": "label", "profile_required": true, "auth0_mapping": "auth0_mapping", "oidc_mapping": map[string]interface{}{"mapping": "mapping"}, "saml_mapping": []interface{}{"saml_mapping"}, "scim_mapping": "scim_mapping"}}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.CreateUserAttributeProfileRequestContent{
		Name: "name",
		UserAttributes: map[string]*management.UserAttributeProfileUserAttributeAdditionalProperties{
			"key": &management.UserAttributeProfileUserAttributeAdditionalProperties{
				Description:     "description",
				Label:           "label",
				ProfileRequired: true,
				Auth0Mapping:    "auth0_mapping",
			},
		},
	}
	_, invocationErr := client.UserAttributeProfiles.Create(
		context.TODO(),
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUserAttributeProfilesListTemplatesWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Get(gowiremock.URLPathTemplate("/user-attribute-profiles/templates")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"user_attribute_profile_templates": []interface{}{map[string]interface{}{"id": "id", "display_name": "display_name"}}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	_, invocationErr := client.UserAttributeProfiles.ListTemplates(
		context.TODO(),
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUserAttributeProfilesGetTemplateWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Get(gowiremock.URLPathTemplate("/user-attribute-profiles/templates/{id}")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"id": "id", "display_name": "display_name", "template": map[string]interface{}{"name": "name", "user_id": map[string]interface{}{"oidc_mapping": "sub", "saml_mapping": []interface{}{"saml_mapping"}, "scim_mapping": "scim_mapping"}, "user_attributes": map[string]interface{}{"key": map[string]interface{}{"description": "description", "label": "label", "profile_required": true, "auth0_mapping": "auth0_mapping"}}}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	_, invocationErr := client.UserAttributeProfiles.GetTemplate(
		context.TODO(),
		"id",
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUserAttributeProfilesGetWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Get(gowiremock.URLPathTemplate("/user-attribute-profiles/{id}")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"id": "id", "name": "name", "user_id": map[string]interface{}{"oidc_mapping": "sub", "saml_mapping": []interface{}{"saml_mapping"}, "scim_mapping": "scim_mapping"}, "user_attributes": map[string]interface{}{"key": map[string]interface{}{"description": "description", "label": "label", "profile_required": true, "auth0_mapping": "auth0_mapping", "oidc_mapping": map[string]interface{}{"mapping": "mapping"}, "saml_mapping": []interface{}{"saml_mapping"}, "scim_mapping": "scim_mapping"}}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	_, invocationErr := client.UserAttributeProfiles.Get(
		context.TODO(),
		"id",
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUserAttributeProfilesDeleteWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Delete(gowiremock.URLPathTemplate("/user-attribute-profiles/{id}")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	invocationErr := client.UserAttributeProfiles.Delete(
		context.TODO(),
		"id",
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUserAttributeProfilesUpdateWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Patch(gowiremock.URLPathTemplate("/user-attribute-profiles/{id}")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WithBodyPattern(gowiremock.MatchesJsonSchema(`{
                    "$schema": "https://json-schema.org/draft/2020-12/schema",
                    "type": "object",
                    "required": [],
                    "properties": {
                        
                    },
                    "additionalProperties": true
                }`, "V202012")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"id": "id", "name": "name", "user_id": map[string]interface{}{"oidc_mapping": "sub", "saml_mapping": []interface{}{"saml_mapping"}, "scim_mapping": "scim_mapping"}, "user_attributes": map[string]interface{}{"key": map[string]interface{}{"description": "description", "label": "label", "profile_required": true, "auth0_mapping": "auth0_mapping", "oidc_mapping": map[string]interface{}{"mapping": "mapping"}, "saml_mapping": []interface{}{"saml_mapping"}, "scim_mapping": "scim_mapping"}}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.UpdateUserAttributeProfileRequestContent{}
	_, invocationErr := client.UserAttributeProfiles.Update(
		context.TODO(),
		"id",
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}
