From bd020b00d343b11b29c11dc297074c2404acbd86 Mon Sep 17 00:00:00 2001 From: tanya732 Date: Fri, 20 Dec 2024 13:12:14 +0530 Subject: [PATCH 1/3] Added Support for Self-Service Profile --- .../com/auth0/client/mgmt/ManagementAPI.java | 8 + .../mgmt/SelfServiceProfilesEntity.java | 132 +++++++++++++ .../filter/PageBasedPaginationFilter.java | 31 +++ .../mgmt/selfserviceprofiles/Branding.java | 46 +++++ .../json/mgmt/selfserviceprofiles/Color.java | 43 ++++ .../SelfServiceProfile.java | 103 ++++++++++ .../SelfServiceProfileResponse.java | 64 ++++++ .../SelfServiceProfileResponsePage.java | 21 ++ ...erviceProfileResponsePageDeserializer.java | 23 +++ .../selfserviceprofiles/UserAttribute.java | 83 ++++++++ .../java/com/auth0/client/MockServer.java | 2 + .../mgmt/SelfServiceProfilesEntityTest.java | 185 ++++++++++++++++++ .../resources/mgmt/self_service_profile.json | 17 ++ .../mgmt/self_service_profiles_list.json | 41 ++++ 14 files changed, 799 insertions(+) create mode 100644 src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java create mode 100644 src/main/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilter.java create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Branding.java create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Color.java create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfile.java create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponse.java create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePage.java create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageDeserializer.java create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/UserAttribute.java create mode 100644 src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java create mode 100644 src/test/resources/mgmt/self_service_profile.json create mode 100644 src/test/resources/mgmt/self_service_profiles_list.json diff --git a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java index bc2a2d8f..6d4b03c2 100644 --- a/src/main/java/com/auth0/client/mgmt/ManagementAPI.java +++ b/src/main/java/com/auth0/client/mgmt/ManagementAPI.java @@ -388,6 +388,14 @@ public PromptsEntity prompts() { return new PromptsEntity(client, baseUrl, tokenProvider); } + /** + * Getter for the SelfServiceProfiles Entity + * @return the SelfServiceProfiles Entity + */ + public SelfServiceProfilesEntity selfServiceProfiles() { + return new SelfServiceProfilesEntity(client, baseUrl, tokenProvider); + } + /** * Builder for {@link ManagementAPI} API client instances. */ diff --git a/src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java b/src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java new file mode 100644 index 00000000..cd801564 --- /dev/null +++ b/src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java @@ -0,0 +1,132 @@ +package com.auth0.client.mgmt; + +import com.auth0.client.mgmt.filter.PageBasedPaginationFilter; +import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfile; +import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponse; +import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponsePage; +import com.auth0.net.BaseRequest; +import com.auth0.net.Request; +import com.auth0.net.VoidRequest; +import com.auth0.net.client.Auth0HttpClient; +import com.auth0.net.client.HttpMethod; +import com.auth0.utils.Asserts; +import com.fasterxml.jackson.core.type.TypeReference; +import okhttp3.HttpUrl; + +import java.util.Map; + +public class SelfServiceProfilesEntity extends BaseManagementEntity { + + private final static String ORGS_PATH = "api/v2/self-service-profiles"; + + SelfServiceProfilesEntity(Auth0HttpClient client, HttpUrl baseUrl, TokenProvider tokenProvider) { + super(client, baseUrl, tokenProvider); + } + + /** + * Request the list of self-service profiles. + * A token with scope read:self_service_profiles is needed + * See https://auth0.com/docs/api/management/v2/self-service-profiles/get-self-service-profiles + * @param pageFilter the pagination filter to apply. Can be null to use the default values. + * @return a Request to execute. + */ + public Request get(PageBasedPaginationFilter pageFilter) { + HttpUrl.Builder builder = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH); + + if (pageFilter != null) { + for (Map.Entry e : pageFilter.getAsMap().entrySet()) { + builder.addQueryParameter(e.getKey(), String.valueOf(e.getValue())); + } + } + + String url = builder.build().toString(); + return new BaseRequest<>(this.client, tokenProvider, url, HttpMethod.GET, new TypeReference() { + }); + } + + /** + * Create a new self-service profile. + * A token with scope create:self_service_profiles is needed + * See https://auth0.com/docs/api/management/v2/self-service-profiles/post-self-service-profiles + * @param selfServiceProfile the self-service profile to create. + * @return a Request to execute. + */ + public Request create(SelfServiceProfile selfServiceProfile) { + Asserts.assertNotNull(selfServiceProfile, "self service profile"); + Asserts.assertNotNull(selfServiceProfile.getName(), "name"); + + String url = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .build() + .toString(); + + BaseRequest request = new BaseRequest<>(this.client, tokenProvider, url, HttpMethod.POST, new TypeReference() { + }); + request.setBody(selfServiceProfile); + return request; + } + + /** + * Request the self-service profile with the given ID. + * A token with scope read:self_service_profiles is needed + * See https://auth0.com/docs/api/management/v2/self-service-profiles/get-self-service-profiles-by-id + * @param id the self-service profile ID. + * @return a Request to execute. + */ + public Request getById(String id) { + Asserts.assertNotNull(id, "id"); + + String url = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .addPathSegment(id) + .build() + .toString(); + + return new BaseRequest<>(this.client, tokenProvider, url, HttpMethod.GET, new TypeReference() { + }); + } + + /** + * Delete the self-service profile with the given ID. + * A token with scope delete:self_service_profiles is needed + * See https://auth0.com/docs/api/management/v2/self-service-profiles/delete-self-service-profiles-by-id + * @param id the self-service profile ID. + * @return a Request to execute. + */ + public Request delete(String id) { + Asserts.assertNotNull(id, "id"); + + String url = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .addPathSegment(id) + .build() + .toString(); + + return new VoidRequest(this.client, tokenProvider, url, HttpMethod.DELETE); + } + + /** + * Update the self-service profile with the given ID. + * A token with scope update:self_service_profiles is needed + * See https://auth0.com/docs/api/management/v2/self-service-profiles/patch-self-service-profiles-by-id + * @param selfServiceProfile the self-service profile to update. + * @param id the self-service profile ID. + * @return a Request to execute. + */ + public Request update(SelfServiceProfile selfServiceProfile, String id) { + Asserts.assertNotNull(selfServiceProfile, "self service profile"); + Asserts.assertNotNull(id, "id"); + + String url = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .addPathSegment(id) + .build() + .toString(); + + BaseRequest request = new BaseRequest<>(this.client, tokenProvider, url, HttpMethod.PATCH, new TypeReference() { + }); + request.setBody(selfServiceProfile); + return request; + } +} diff --git a/src/main/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilter.java b/src/main/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilter.java new file mode 100644 index 00000000..a11b69b8 --- /dev/null +++ b/src/main/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilter.java @@ -0,0 +1,31 @@ +package com.auth0.client.mgmt.filter; + +/** + * Class that represents a filter to be used when requesting a list of items with pagination. + */ +public class PageBasedPaginationFilter extends BaseFilter{ + + /** + * Filter by page + * + * @param pageNumber the page number to retrieve. + * @param amountPerPage the amount of items per page to retrieve. + * @return this filter instance + */ + public PageBasedPaginationFilter withPage(int pageNumber, int amountPerPage) { + parameters.put("page", pageNumber); + parameters.put("per_page", amountPerPage); + return this; + } + + /** + * Include the query summary + * + * @param includeTotals whether to include or not the query summary. + * @return this filter instance + */ + public PageBasedPaginationFilter withTotals(boolean includeTotals) { + parameters.put("include_totals", includeTotals); + return this; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Branding.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Branding.java new file mode 100644 index 00000000..55433ba7 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Branding.java @@ -0,0 +1,46 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class Branding { + @JsonProperty("logo_url") + private String logoUrl; + @JsonProperty("colors") + private Color colors; + + /** + * Getter for the logo URL. + * @return the logo URL. + */ + public String getLogoUrl() { + return logoUrl; + } + + /** + * Setter for the logo URL. + * @param logoUrl the logo URL to set. + */ + public void setLogoUrl(String logoUrl) { + this.logoUrl = logoUrl; + } + + /** + * Getter for the colors. + * @return the colors. + */ + public Color getColors() { + return colors; + } + + /** + * Setter for the colors. + * @param colors the colors to set. + */ + public void setColors(Color colors) { + this.colors = colors; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Color.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Color.java new file mode 100644 index 00000000..87891fba --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/Color.java @@ -0,0 +1,43 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class Color { + @JsonProperty("primary") + private String primary; + + /** + * Creates a new instance of the Color class. + */ + public Color() {} + + /** + * Creates a new instance of the Color class. + * @param primary the primary color. + */ + @JsonCreator + public Color(@JsonProperty("primary") String primary) { + this.primary = primary; + } + + /** + * Getter for the primary color. + * @return the primary color. + */ + public String getPrimary() { + return primary; + } + + /** + * Setter for the primary color. + * @param primary the primary color to set. + */ + public void setPrimary(String primary) { + this.primary = primary; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfile.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfile.java new file mode 100644 index 00000000..1a81aa81 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfile.java @@ -0,0 +1,103 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class SelfServiceProfile { + @JsonProperty("name") + private String name; + @JsonProperty("description") + private String description; + @JsonProperty("user_attributes") + private List userAttributes; + @JsonProperty("branding") + private Branding branding; + @JsonProperty("allowed_strategies") + private List allowedStrategies; + + /** + * Getter for the name of the self-service profile. + * @return the name of the self-service profile. + */ + public String getName() { + return name; + } + + /** + * Setter for the name of the self-service profile. + * @param name the name of the self-service profile to set. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Getter for the description of the self-service profile. + * @return the description of the self-service profile. + */ + public String getDescription() { + return description; + } + + /** + * Setter for the description of the self-service profile. + * @param description the description of the self-service profile to set. + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Getter for the user attributes of the self-service profile. + * @return the user attributes of the self-service profile. + */ + public List getUserAttributes() { + return userAttributes; + } + + /** + * Setter for the user attributes of the self-service profile. + * @param userAttributes the user attributes of the self-service profile to set. + */ + public void setUserAttributes(List userAttributes) { + this.userAttributes = userAttributes; + } + + /** + * Getter for the branding of the self-service profile. + * @return the branding of the self-service profile. + */ + public Branding getBranding() { + return branding; + } + + /** + * Setter for the branding of the self-service profile. + * @param branding the branding of the self-service profile to set. + */ + public void setBranding(Branding branding) { + this.branding = branding; + } + + /** + * Getter for the allowed strategies of the self-service profile. + * @return the allowed strategies of the self-service profile. + */ + public List getAllowedStrategies() { + return allowedStrategies; + } + + /** + * Setter for the allowed strategies of the self-service profile. + * @param allowedStrategies the allowed strategies of the self-service profile to set. + */ + public void setAllowedStrategies(List allowedStrategies) { + this.allowedStrategies = allowedStrategies; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponse.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponse.java new file mode 100644 index 00000000..838bb9da --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponse.java @@ -0,0 +1,64 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +public class SelfServiceProfileResponse extends SelfServiceProfile { + @JsonProperty("id") + private String id; + @JsonProperty("created_at") + private String createdAt; + @JsonProperty("updated_at") + private String updatedAt; + + /** + * Getter for the id of the self-service profile. + * @return the id of the self-service profile. + */ + public String getId() { + return id; + } + + /** + * Setter for the id of the self-service profile. + * @param id the id of the self-service profile to set. + */ + public void setId(String id) { + this.id = id; + } + + /** + * Getter for the created at of the self-service profile. + * @return the created at of the self-service profile. + */ + public String getCreatedAt() { + return createdAt; + } + + /** + * Setter for the created at of the self-service profile. + * @param createdAt the created at of the self-service profile to set. + */ + public void setCreatedAt(String createdAt) { + this.createdAt = createdAt; + } + + /** + * Getter for the updated at of the self-service profile. + * @return the updated at of the self-service profile. + */ + public String getUpdatedAt() { + return updatedAt; + } + + /** + * Setter for the updated at of the self-service profile. + * @param updatedAt the updated at of the self-service profile to set. + */ + public void setUpdatedAt(String updatedAt) { + this.updatedAt = updatedAt; + } +} diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePage.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePage.java new file mode 100644 index 00000000..e28d4cf7 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePage.java @@ -0,0 +1,21 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.auth0.json.mgmt.Page; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +import java.util.List; + +@JsonIgnoreProperties(ignoreUnknown = true) +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonDeserialize(using = SelfServiceProfileResponsePageDeserializer.class) +public class SelfServiceProfileResponsePage extends Page { + public SelfServiceProfileResponsePage(List items) { + super(items); + } + + public SelfServiceProfileResponsePage(Integer start, Integer length, Integer total, Integer limit, List items) { + super(start, length, total, limit, items); + } +} diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageDeserializer.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageDeserializer.java new file mode 100644 index 00000000..bebdbade --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageDeserializer.java @@ -0,0 +1,23 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.auth0.json.mgmt.PageDeserializer; + +import java.util.List; + + +public class SelfServiceProfileResponsePageDeserializer extends PageDeserializer { + + protected SelfServiceProfileResponsePageDeserializer() { + super(SelfServiceProfileResponse.class, "self_service_profiles"); + } + + @Override + protected SelfServiceProfileResponsePage createPage(List items) { + return new SelfServiceProfileResponsePage(items); + } + + @Override + protected SelfServiceProfileResponsePage createPage(Integer start, Integer length, Integer total, Integer limit, List items) { + return new SelfServiceProfileResponsePage(start, length, total, limit, items); + } +} diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/UserAttribute.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/UserAttribute.java new file mode 100644 index 00000000..33db0ad6 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/UserAttribute.java @@ -0,0 +1,83 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class UserAttribute { + @JsonProperty("name") + private String name; + @JsonProperty("description") + private String description; + @JsonProperty("is_optional") + private boolean isOptional; + + /** + * Creates a new instance of the UserAttribute class. + */ + public UserAttribute() { } + + /** + * Creates a new instance of the UserAttribute class. + * @param name the name of the user attribute. + * @param description the description of the user attribute. + * @param isOptional the isOptional of the user attribute. + */ + @JsonCreator + public UserAttribute(@JsonProperty("name") String name, @JsonProperty("description") String description, @JsonProperty("is_optional") boolean isOptional) { + this.name = name; + this.description = description; + this.isOptional = isOptional; + } + + /** + * Getter for the name of the user attribute. + * @return the name of the user attribute. + */ + public String getName() { + return name; + } + + /** + * Setter for the name of the user attribute. + * @param name the name of the user attribute to set. + */ + public void setName(String name) { + this.name = name; + } + + /** + * Getter for the description of the user attribute. + * @return the description of the user attribute. + */ + public String getDescription() { + return description; + } + + /** + * Setter for the description of the user attribute. + * @param description the description of the user attribute to set. + */ + public void setDescription(String description) { + this.description = description; + } + + /** + * Getter for the isOptional of the user attribute. + * @return the isOptional of the user attribute. + */ + public boolean getIsOptional() { + return isOptional; + } + + /** + * Setter for the isOptional of the user attribute. + * @param isOptional the isOptional of the user attribute to set. + */ + public void setIsOptional(boolean isOptional) { + this.isOptional = isOptional; + } +} diff --git a/src/test/java/com/auth0/client/MockServer.java b/src/test/java/com/auth0/client/MockServer.java index cc65cab6..c7a349ac 100644 --- a/src/test/java/com/auth0/client/MockServer.java +++ b/src/test/java/com/auth0/client/MockServer.java @@ -157,6 +157,8 @@ public class MockServer { public static final String ENCRYPTION_KEY = "src/test/resources/mgmt/encryption_key.json"; public static final String ENCRYPTION_KEYS_LIST = "src/test/resources/mgmt/encryption_keys_list.json"; public static final String RATE_LIMIT_ERROR = "src/test/resources/mgmt/rate_limit_error.json"; + public static final String SELF_SERVICE_PROFILES_LIST = "src/test/resources/mgmt/self_service_profiles_list.json"; + public static final String SELF_SERVICE_PROFILE = "src/test/resources/mgmt/self_service_profile.json"; private final MockWebServer server; diff --git a/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java b/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java new file mode 100644 index 00000000..2f7bd5f9 --- /dev/null +++ b/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java @@ -0,0 +1,185 @@ +package com.auth0.client.mgmt; + +import com.auth0.client.mgmt.filter.PageBasedPaginationFilter; +import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfile; +import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponse; +import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponsePage; +import com.auth0.net.Request; +import com.auth0.net.client.HttpMethod; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.Test; + +import java.util.Map; + +import static com.auth0.AssertsUtil.verifyThrows; +import static com.auth0.client.MockServer.*; +import static com.auth0.client.RecordedRequestMatcher.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class SelfServiceProfilesEntityTest extends BaseMgmtEntityTest { + + @Test + public void shouldGetSelfServiceProfilesWithoutFilter() throws Exception{ + Request request = api.selfServiceProfiles().get(null); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILES_LIST, 200); + SelfServiceProfileResponsePage response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/self-service-profiles")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + assertThat(response, is(notNullValue())); + assertThat(response.getItems(), hasSize(2)); + } + + @Test + public void shouldGetSelfServiceProfilesWithPage() throws Exception { + PageBasedPaginationFilter filter = new PageBasedPaginationFilter().withPage(1, 5); + Request request = api.selfServiceProfiles().get(filter); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILES_LIST, 200); + SelfServiceProfileResponsePage response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/self-service-profiles")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + assertThat(recordedRequest, hasQueryParameter("page", "1")); + assertThat(recordedRequest, hasQueryParameter("per_page", "5")); + + assertThat(response, is(notNullValue())); + assertThat(response.getItems(), hasSize(2)); + } + + @Test + public void shouldGetSelfServiceProfilesWithTotals() throws Exception { + PageBasedPaginationFilter filter = new PageBasedPaginationFilter().withTotals(true); + Request request = api.selfServiceProfiles().get(filter); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILES_LIST, 200); + SelfServiceProfileResponsePage response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/self-service-profiles")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + assertThat(recordedRequest, hasQueryParameter("include_totals", "true")); + + assertThat(response, is(notNullValue())); + assertThat(response.getItems(), hasSize(2)); + } + + @Test + public void shouldThrowOnCreateWhenSelfServiceProfileIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().create(null), "'self service profile' cannot be null!"); + } + + @Test + public void shouldThrowOnCreateWhenNameInSelfServiceProfileIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().create(new SelfServiceProfile()), "'name' cannot be null!"); + } + + @Test + public void shouldCreateSelfServiceProfile() throws Exception { + SelfServiceProfile profile = new SelfServiceProfile(); + profile.setName("Test"); + profile.setDescription("This is for Test"); + Request request = api.selfServiceProfiles().create(profile); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILE, 201); + SelfServiceProfileResponse response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/self-service-profiles")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + Map body = bodyFromRequest(recordedRequest); + assertThat(body.size(), is(2)); + assertThat(body, hasEntry("name", "Test")); + assertThat(body, hasEntry("description", "This is for Test")); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnGetByIdWhenIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().getById(null), "'id' cannot be null!"); + } + + @Test + public void shouldGetSelfServiceProfileById() throws Exception { + Request request = api.selfServiceProfiles().getById("id"); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILE, 200); + SelfServiceProfileResponse response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/self-service-profiles/id")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnDeleteWhenIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().delete(null), "'id' cannot be null!"); + } + + @Test + public void shouldDeleteSelfServiceProfile() throws Exception { + Request request = api.selfServiceProfiles().delete("id"); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILE, 200); + request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.DELETE, "/api/v2/self-service-profiles/id")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + } + + @Test + public void shouldThrowOnUpdateWhenIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().update(new SelfServiceProfile(), null), "'id' cannot be null!"); + } + + @Test + public void shouldThrowOnUpdateWhenSelfServiceProfileIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().update(null, "id"), "'self service profile' cannot be null!"); + } + + @Test + public void shouldUpdateSelfServiceProfile() throws Exception { + Request request = api.selfServiceProfiles().update(new SelfServiceProfile(), "id"); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILE, 200); + SelfServiceProfileResponse response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.PATCH, "/api/v2/self-service-profiles/id")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + Map body = bodyFromRequest(recordedRequest); + + assertThat(response, is(notNullValue())); + } +} diff --git a/src/test/resources/mgmt/self_service_profile.json b/src/test/resources/mgmt/self_service_profile.json new file mode 100644 index 00000000..fff5f3d2 --- /dev/null +++ b/src/test/resources/mgmt/self_service_profile.json @@ -0,0 +1,17 @@ +{ + "id": "id", + "name": "Test", + "description": "This is for Test", + "user_attributes": [ + { + "name": "Phone", + "description": "This is Phone Number", + "is_optional": true + } + ], + "allowed_strategies": [ + "oidc" + ], + "created_at": "2024-12-16T15:29:06.119Z", + "updated_at": "2024-12-16T15:29:06.119Z" +} diff --git a/src/test/resources/mgmt/self_service_profiles_list.json b/src/test/resources/mgmt/self_service_profiles_list.json new file mode 100644 index 00000000..7e39825f --- /dev/null +++ b/src/test/resources/mgmt/self_service_profiles_list.json @@ -0,0 +1,41 @@ +[ + { + "id": "id1", + "name": "test1", + "description": "This is for Test1", + "user_attributes": [ + { + "name": "Phone", + "description": "This is Phone Number", + "is_optional": true + } + ], + "allowed_strategies": [ + "google-apps" + ], + "created_at": "2024-12-16T15:26:39.015Z", + "updated_at": "2024-12-16T15:28:04.933Z" + }, + { + "id": "id2", + "name": "Test2", + "description": "This is for Test2", + "user_attributes": [ + { + "name": "Phone", + "description": "This is Phone Number", + "is_optional": true + }, + { + "name": "UserName", + "description": "This is User Name", + "is_optional": true + } + ], + "allowed_strategies": [ + "oidc" + ], + "created_at": "2024-12-16T15:29:06.119Z", + "updated_at": "2024-12-16T15:29:06.119Z" + } +] From 3ab7a58f963fcd4ea216baba792ee1ed6811be24 Mon Sep 17 00:00:00 2001 From: tanya732 Date: Fri, 20 Dec 2024 16:18:46 +0530 Subject: [PATCH 2/3] Added Test Cases --- .../java/com/auth0/client/MockServer.java | 1 + .../mgmt/SelfServiceProfilesEntityTest.java | 14 +- .../filter/PageBasedPaginationFilterTest.java | 39 ++++ .../SelfServiceProfileResponsePageTest.java | 173 ++++++++++++++++++ .../SelfServiceProfileResponseTest.java | 73 ++++++++ .../SelfServiceProfileTest.java | 62 +++++++ .../resources/mgmt/self_service_profile.json | 10 +- .../mgmt/self_service_profile_response.json | 23 +++ .../mgmt/self_service_profiles_list.json | 25 ++- 9 files changed, 409 insertions(+), 11 deletions(-) create mode 100644 src/test/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilterTest.java create mode 100644 src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageTest.java create mode 100644 src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponseTest.java create mode 100644 src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileTest.java create mode 100644 src/test/resources/mgmt/self_service_profile_response.json diff --git a/src/test/java/com/auth0/client/MockServer.java b/src/test/java/com/auth0/client/MockServer.java index c7a349ac..541992b2 100644 --- a/src/test/java/com/auth0/client/MockServer.java +++ b/src/test/java/com/auth0/client/MockServer.java @@ -158,6 +158,7 @@ public class MockServer { public static final String ENCRYPTION_KEYS_LIST = "src/test/resources/mgmt/encryption_keys_list.json"; public static final String RATE_LIMIT_ERROR = "src/test/resources/mgmt/rate_limit_error.json"; public static final String SELF_SERVICE_PROFILES_LIST = "src/test/resources/mgmt/self_service_profiles_list.json"; + public static final String SELF_SERVICE_PROFILE_RESPONSE = "src/test/resources/mgmt/self_service_profile_response.json"; public static final String SELF_SERVICE_PROFILE = "src/test/resources/mgmt/self_service_profile.json"; private final MockWebServer server; diff --git a/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java b/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java index 2f7bd5f9..b71fd0be 100644 --- a/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java @@ -33,7 +33,7 @@ public void shouldGetSelfServiceProfilesWithoutFilter() throws Exception{ assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); assertThat(response, is(notNullValue())); - assertThat(response.getItems(), hasSize(2)); + assertThat(response.getItems(), hasSize(3)); } @Test @@ -53,7 +53,7 @@ public void shouldGetSelfServiceProfilesWithPage() throws Exception { assertThat(recordedRequest, hasQueryParameter("per_page", "5")); assertThat(response, is(notNullValue())); - assertThat(response.getItems(), hasSize(2)); + assertThat(response.getItems(), hasSize(3)); } @Test @@ -72,7 +72,7 @@ public void shouldGetSelfServiceProfilesWithTotals() throws Exception { assertThat(recordedRequest, hasQueryParameter("include_totals", "true")); assertThat(response, is(notNullValue())); - assertThat(response.getItems(), hasSize(2)); + assertThat(response.getItems(), hasSize(3)); } @Test @@ -95,7 +95,7 @@ public void shouldCreateSelfServiceProfile() throws Exception { Request request = api.selfServiceProfiles().create(profile); assertThat(request, is(notNullValue())); - server.jsonResponse(SELF_SERVICE_PROFILE, 201); + server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 201); SelfServiceProfileResponse response = request.execute().getBody(); RecordedRequest recordedRequest = server.takeRequest(); @@ -122,7 +122,7 @@ public void shouldGetSelfServiceProfileById() throws Exception { Request request = api.selfServiceProfiles().getById("id"); assertThat(request, is(notNullValue())); - server.jsonResponse(SELF_SERVICE_PROFILE, 200); + server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 200); SelfServiceProfileResponse response = request.execute().getBody(); RecordedRequest recordedRequest = server.takeRequest(); @@ -144,7 +144,7 @@ public void shouldDeleteSelfServiceProfile() throws Exception { Request request = api.selfServiceProfiles().delete("id"); assertThat(request, is(notNullValue())); - server.jsonResponse(SELF_SERVICE_PROFILE, 200); + server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 200); request.execute().getBody(); RecordedRequest recordedRequest = server.takeRequest(); @@ -170,7 +170,7 @@ public void shouldUpdateSelfServiceProfile() throws Exception { Request request = api.selfServiceProfiles().update(new SelfServiceProfile(), "id"); assertThat(request, is(notNullValue())); - server.jsonResponse(SELF_SERVICE_PROFILE, 200); + server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 200); SelfServiceProfileResponse response = request.execute().getBody(); RecordedRequest recordedRequest = server.takeRequest(); diff --git a/src/test/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilterTest.java b/src/test/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilterTest.java new file mode 100644 index 00000000..282cfc69 --- /dev/null +++ b/src/test/java/com/auth0/client/mgmt/filter/PageBasedPaginationFilterTest.java @@ -0,0 +1,39 @@ +package com.auth0.client.mgmt.filter; + +import org.hamcrest.Matchers; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.MatcherAssert.assertThat; + +public class PageBasedPaginationFilterTest { + + private PageBasedPaginationFilter filter; + + @BeforeEach + public void setUp() { + filter = new PageBasedPaginationFilter(); + } + + @Test + public void shouldFilterByPage() { + PageBasedPaginationFilter instance = filter.withPage(5, 10); + + assertThat(filter, is(instance)); + assertThat(filter.getAsMap(), is(notNullValue())); + assertThat(filter.getAsMap(), Matchers.hasEntry("per_page", 10)); + assertThat(filter.getAsMap(), Matchers.hasEntry("page", 5)); + } + + @Test + public void shouldIncludeTotals() { + PageBasedPaginationFilter instance = filter.withTotals(true); + + assertThat(filter, is(instance)); + assertThat(filter.getAsMap(), is(notNullValue())); + assertThat(filter.getAsMap(), Matchers.hasEntry("include_totals", true)); + } + +} diff --git a/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageTest.java b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageTest.java new file mode 100644 index 00000000..108e0312 --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponsePageTest.java @@ -0,0 +1,173 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.auth0.json.JsonTest; +import org.junit.jupiter.api.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class SelfServiceProfileResponsePageTest extends JsonTest { + + private static final String jsonWithoutTotals = + "[\n" + + " {\n" + + " \"id\": \"id1\",\n" + + " \"name\": \"test1\",\n" + + " \"description\": \"This is for testing\",\n" + + " \"user_attributes\": [\n" + + " {\n" + + " \"name\": \"Phone\",\n" + + " \"description\": \"This is Phone Number\",\n" + + " \"is_optional\": true\n" + + " }\n" + + " ],\n" + + " \"allowed_strategies\": [\n" + + " \"google-apps\"\n" + + " ],\n" + + " \"created_at\": \"2024-12-16T15:26:39.015Z\",\n" + + " \"updated_at\": \"2024-12-16T15:28:04.933Z\"\n" + + " },\n" + + " {\n" + + " \"id\": \"id2\",\n" + + " \"name\": \"Test2\",\n" + + " \"description\": \"This is for Test2\",\n" + + " \"user_attributes\": [\n" + + " {\n" + + " \"name\": \"Phone\",\n" + + " \"description\": \"This is Phone Number\",\n" + + " \"is_optional\": true\n" + + " },\n" + + " {\n" + + " \"name\": \"UserName\",\n" + + " \"description\": \"This is User Name\",\n" + + " \"is_optional\": true\n" + + " }\n" + + " ],\n" + + " \"allowed_strategies\": [\n" + + " \"oidc\"\n" + + " ],\n" + + " \"created_at\": \"2024-12-16T15:29:06.119Z\",\n" + + " \"updated_at\": \"2024-12-16T15:29:06.119Z\"\n" + + " },\n" + + " {\n" + + " \"id\": \"id3\",\n" + + " \"name\": \"Test3\",\n" + + " \"description\": \"This is a Test3\",\n" + + " \"user_attributes\": [\n" + + " {\n" + + " \"name\": \"Name\",\n" + + " \"description\": \"Name Field\",\n" + + " \"is_optional\": true\n" + + " }\n" + + " ],\n" + + " \"allowed_strategies\": [\n" + + " \"oidc\"\n" + + " ],\n" + + " \"created_at\": \"2024-12-20T09:32:13.885Z\",\n" + + " \"updated_at\": \"2024-12-20T09:32:13.885Z\",\n" + + " \"branding\": {\n" + + " \"logo_url\": \"https://www.google.com\",\n" + + " \"colors\": {\n" + + " \"primary\": \"#ffffff\"\n" + + " }\n" + + " }\n" + + " }\n" + + "]\n"; + + private static final String jsonWithTotals = + "{\n" + + " \"self_service_profiles\": [\n" + + " {\n" + + " \"id\": \"id1\",\n" + + " \"name\": \"test1\",\n" + + " \"description\": \"This is for testing\",\n" + + " \"user_attributes\": [\n" + + " {\n" + + " \"name\": \"Phone\",\n" + + " \"description\": \"This is Phone Number\",\n" + + " \"is_optional\": true\n" + + " }\n" + + " ],\n" + + " \"allowed_strategies\": [\n" + + " \"google-apps\"\n" + + " ],\n" + + " \"created_at\": \"2024-12-16T15:26:39.015Z\",\n" + + " \"updated_at\": \"2024-12-16T15:28:04.933Z\"\n" + + " },\n" + + " {\n" + + " \"id\": \"id2\",\n" + + " \"name\": \"Test2\",\n" + + " \"description\": \"This is for Test2\",\n" + + " \"user_attributes\": [\n" + + " {\n" + + " \"name\": \"Phone\",\n" + + " \"description\": \"This is Phone Number\",\n" + + " \"is_optional\": true\n" + + " },\n" + + " {\n" + + " \"name\": \"UserName\",\n" + + " \"description\": \"This is User Name\",\n" + + " \"is_optional\": true\n" + + " }\n" + + " ],\n" + + " \"allowed_strategies\": [\n" + + " \"oidc\"\n" + + " ],\n" + + " \"created_at\": \"2024-12-16T15:29:06.119Z\",\n" + + " \"updated_at\": \"2024-12-16T15:29:06.119Z\"\n" + + " },\n" + + " {\n" + + " \"id\": \"id3\",\n" + + " \"name\": \"Test3\",\n" + + " \"description\": \"This is a Test3\",\n" + + " \"user_attributes\": [\n" + + " {\n" + + " \"name\": \"Name\",\n" + + " \"description\": \"Name Field\",\n" + + " \"is_optional\": true\n" + + " }\n" + + " ],\n" + + " \"allowed_strategies\": [\n" + + " \"oidc\"\n" + + " ],\n" + + " \"created_at\": \"2024-12-20T09:32:13.885Z\",\n" + + " \"updated_at\": \"2024-12-20T09:32:13.885Z\",\n" + + " \"branding\": {\n" + + " \"logo_url\": \"https://www.google.com\",\n" + + " \"colors\": {\n" + + " \"primary\": \"#ffffff\"\n" + + " }\n" + + " }\n" + + " }\n" + + " ],\n" + + " \"start\": 0,\n" + + " \"limit\": 10,\n" + + " \"total\": 3\n" + + "}"; + + @Test + public void shouldDeserializeWithoutTotals() throws Exception { + SelfServiceProfileResponsePage page = fromJSON(jsonWithoutTotals, SelfServiceProfileResponsePage.class); + + assertThat(page, is(notNullValue())); + assertThat(page.getStart(), is(nullValue())); + assertThat(page.getTotal(), is(nullValue())); + assertThat(page.getLimit(), is(nullValue())); + assertThat(page.getItems(), is(notNullValue())); + assertThat(page.getItems().size(), is(3)); + assertThat(page.getNext(), is(nullValue())); + } + + @Test + public void shouldDeserializeWithTotals() throws Exception { + SelfServiceProfileResponsePage page = fromJSON(jsonWithTotals, SelfServiceProfileResponsePage.class); + + assertThat(page, is(notNullValue())); + assertThat(page.getStart(), is(0)); + assertThat(page.getTotal(), is(3)); + assertThat(page.getLimit(), is(10)); + assertThat(page.getItems(), is(notNullValue())); + assertThat(page.getItems().size(), is(3)); + assertThat(page.getNext(), is(nullValue())); + } +} diff --git a/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponseTest.java b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponseTest.java new file mode 100644 index 00000000..3a1173e9 --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileResponseTest.java @@ -0,0 +1,73 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.auth0.json.JsonTest; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static com.auth0.json.JsonMatcher.hasEntry; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class SelfServiceProfileResponseTest extends JsonTest { + + private final static String SELF_SERVICE_PROFILE_RESPONSE_JSON = "src/test/resources/mgmt/self_service_profile_response.json"; + + @Test + public void deserialize() throws Exception { + SelfServiceProfileResponse deserialized = fromJSON(readTextFile(SELF_SERVICE_PROFILE_RESPONSE_JSON), SelfServiceProfileResponse.class); + + assertThat(deserialized.getId(), is("id")); + assertThat(deserialized.getName(), is("Test")); + assertThat(deserialized.getDescription(), is("This is a Test")); + assertThat(deserialized.getUserAttributes().get(0).getName(), is("Phone")); + assertThat(deserialized.getUserAttributes().get(0).getDescription(), is("This is Phone Number")); + assertThat(deserialized.getUserAttributes().get(0).getIsOptional(), is(true)); + assertThat(deserialized.getBranding().getColors().getPrimary(), is("#ffffff")); + assertThat(deserialized.getBranding().getLogoUrl(), is("https://www.google.com")); + assertThat(deserialized.getAllowedStrategies().get(0), is("oidc")); + assertThat(deserialized.getCreatedAt(), is("2024-12-20T09:32:13.885Z")); + assertThat(deserialized.getCreatedAt(), is("2024-12-20T09:32:13.885Z")); + } + + @Test + public void serialize() throws Exception { + SelfServiceProfileResponse selfServiceProfileResponse = new SelfServiceProfileResponse(); + selfServiceProfileResponse.setId("id"); + selfServiceProfileResponse.setName("Test"); + selfServiceProfileResponse.setDescription("This is for Test"); + + UserAttribute userAttribute = new UserAttribute("Phone", "This is Phone Number", true); + List userAttributes = new ArrayList<>(); + userAttributes.add(userAttribute); + selfServiceProfileResponse.setUserAttributes(userAttributes); + + Branding branding = new Branding(); + branding.setColors(new Color("#ffffff")); + branding.setLogoUrl("https://www.google.com"); + selfServiceProfileResponse.setBranding(branding); + + List allowedStrategies = new ArrayList<>(); + allowedStrategies.add("oidc"); + selfServiceProfileResponse.setAllowedStrategies(allowedStrategies); + + selfServiceProfileResponse.setCreatedAt("2024-12-20T09:32:13.885Z"); + selfServiceProfileResponse.setUpdatedAt("2024-12-20T09:32:13.885Z"); + + String serialized = toJSON(selfServiceProfileResponse); + assertThat(serialized, is(notNullValue())); + + assertThat(serialized, hasEntry("id", "id")); + assertThat(serialized, hasEntry("name", "Test")); + assertThat(serialized, hasEntry("description", "This is for Test")); + assertThat(serialized, hasEntry("user_attributes", notNullValue())); + assertThat(serialized, containsString("\"user_attributes\":[{\"name\":\"Phone\",\"description\":\"This is Phone Number\",\"is_optional\":true}]")); + assertThat(serialized, hasEntry("branding", notNullValue())); + assertThat(serialized, containsString("\"branding\":{\"logo_url\":\"https://www.google.com\",\"colors\":{\"primary\":\"#ffffff\"}}")); + assertThat(serialized, hasEntry("allowed_strategies", notNullValue())); + assertThat(serialized, containsString("\"allowed_strategies\":[\"oidc\"]")); + assertThat(serialized, hasEntry("created_at", "2024-12-20T09:32:13.885Z")); + assertThat(serialized, hasEntry("updated_at", "2024-12-20T09:32:13.885Z")); + } +} diff --git a/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileTest.java b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileTest.java new file mode 100644 index 00000000..2f7e1b8c --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SelfServiceProfileTest.java @@ -0,0 +1,62 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.auth0.json.JsonTest; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static com.auth0.json.JsonMatcher.hasEntry; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class SelfServiceProfileTest extends JsonTest { + private final static String SELF_SERVICE_PROFILE_JSON = "src/test/resources/mgmt/self_service_profile.json"; + + @Test + public void deserialize() throws Exception { + SelfServiceProfile deserialized = fromJSON(readTextFile(SELF_SERVICE_PROFILE_JSON), SelfServiceProfile.class); + + assertThat(deserialized.getName(), is("Test")); + assertThat(deserialized.getDescription(), is("This is for Test")); + assertThat(deserialized.getUserAttributes().get(0).getName(), is("Phone")); + assertThat(deserialized.getUserAttributes().get(0).getDescription(), is("This is Phone Number")); + assertThat(deserialized.getUserAttributes().get(0).getIsOptional(), is(true)); + assertThat(deserialized.getBranding().getColors().getPrimary(), is("#ffffff")); + assertThat(deserialized.getBranding().getLogoUrl(), is("https://www.google.com")); + assertThat(deserialized.getAllowedStrategies().get(0), is("oidc")); + } + + @Test + public void serialize() throws Exception { + SelfServiceProfile selfServiceProfile = new SelfServiceProfile(); + selfServiceProfile.setName("Test"); + selfServiceProfile.setDescription("This is for Test"); + + UserAttribute userAttribute = new UserAttribute("Phone", "This is Phone Number", true); + List userAttributes = new ArrayList<>(); + userAttributes.add(userAttribute); + selfServiceProfile.setUserAttributes(userAttributes); + + Branding branding = new Branding(); + branding.setColors(new Color("#ffffff")); + branding.setLogoUrl("https://www.google.com"); + selfServiceProfile.setBranding(branding); + + List allowedStrategies = new ArrayList<>(); + allowedStrategies.add("oidc"); + selfServiceProfile.setAllowedStrategies(allowedStrategies); + + String serialized = toJSON(selfServiceProfile); + assertThat(serialized, is(notNullValue())); + + assertThat(serialized, hasEntry("name", "Test")); + assertThat(serialized, hasEntry("description", "This is for Test")); + assertThat(serialized, hasEntry("user_attributes", notNullValue())); + assertThat(serialized, containsString("\"user_attributes\":[{\"name\":\"Phone\",\"description\":\"This is Phone Number\",\"is_optional\":true}]")); + assertThat(serialized, hasEntry("branding", notNullValue())); + assertThat(serialized, containsString("\"branding\":{\"logo_url\":\"https://www.google.com\",\"colors\":{\"primary\":\"#ffffff\"}}")); + assertThat(serialized, hasEntry("allowed_strategies", notNullValue())); + assertThat(serialized, containsString("\"allowed_strategies\":[\"oidc\"]")); + } +} diff --git a/src/test/resources/mgmt/self_service_profile.json b/src/test/resources/mgmt/self_service_profile.json index fff5f3d2..6c090caa 100644 --- a/src/test/resources/mgmt/self_service_profile.json +++ b/src/test/resources/mgmt/self_service_profile.json @@ -9,9 +9,13 @@ "is_optional": true } ], + "branding": { + "logo_url": "https://www.google.com", + "colors": { + "primary": "#ffffff" + } + }, "allowed_strategies": [ "oidc" - ], - "created_at": "2024-12-16T15:29:06.119Z", - "updated_at": "2024-12-16T15:29:06.119Z" + ] } diff --git a/src/test/resources/mgmt/self_service_profile_response.json b/src/test/resources/mgmt/self_service_profile_response.json new file mode 100644 index 00000000..46b5bcd8 --- /dev/null +++ b/src/test/resources/mgmt/self_service_profile_response.json @@ -0,0 +1,23 @@ +{ + "id": "id", + "name": "Test", + "description": "This is a Test", + "user_attributes": [ + { + "name": "Phone", + "description": "This is Phone Number", + "is_optional": true + } + ], + "allowed_strategies": [ + "oidc" + ], + "created_at": "2024-12-20T09:32:13.885Z", + "updated_at": "2024-12-20T09:32:13.885Z", + "branding": { + "logo_url": "https://www.google.com", + "colors": { + "primary": "#ffffff" + } + } +} diff --git a/src/test/resources/mgmt/self_service_profiles_list.json b/src/test/resources/mgmt/self_service_profiles_list.json index 7e39825f..53001be8 100644 --- a/src/test/resources/mgmt/self_service_profiles_list.json +++ b/src/test/resources/mgmt/self_service_profiles_list.json @@ -2,7 +2,7 @@ { "id": "id1", "name": "test1", - "description": "This is for Test1", + "description": "This is for testing", "user_attributes": [ { "name": "Phone", @@ -37,5 +37,28 @@ ], "created_at": "2024-12-16T15:29:06.119Z", "updated_at": "2024-12-16T15:29:06.119Z" + }, + { + "id": "id3", + "name": "Test3", + "description": "This is a Test3", + "user_attributes": [ + { + "name": "Name", + "description": "Name Field", + "is_optional": true + } + ], + "allowed_strategies": [ + "oidc" + ], + "created_at": "2024-12-20T09:32:13.885Z", + "updated_at": "2024-12-20T09:32:13.885Z", + "branding": { + "logo_url": "https://www.google.com", + "colors": { + "primary": "#ffffff" + } + } } ] From 489399be71934084073f4d971436a3f400c65af5 Mon Sep 17 00:00:00 2001 From: tanya732 Date: Thu, 2 Jan 2025 17:49:38 +0530 Subject: [PATCH 3/3] added remaining API's --- .../mgmt/SelfServiceProfilesEntity.java | 133 ++++++++++++++-- .../SsoAccessTicketResponse.java | 41 +++++ .../java/com/auth0/client/MockServer.java | 3 +- .../mgmt/SelfServiceProfilesEntityTest.java | 142 +++++++++++++++++- .../SsoAccessTicketResponseTest.java | 31 ++++ .../self_service_profile_custom_text.json | 3 + .../mgmt/self_service_profile_sso_ticket.json | 3 + 7 files changed, 341 insertions(+), 15 deletions(-) create mode 100644 src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponse.java create mode 100644 src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponseTest.java create mode 100644 src/test/resources/mgmt/self_service_profile_custom_text.json create mode 100644 src/test/resources/mgmt/self_service_profile_sso_ticket.json diff --git a/src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java b/src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java index cd801564..cf1cbdfe 100644 --- a/src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java +++ b/src/main/java/com/auth0/client/mgmt/SelfServiceProfilesEntity.java @@ -4,9 +4,8 @@ import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfile; import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponse; import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponsePage; -import com.auth0.net.BaseRequest; -import com.auth0.net.Request; -import com.auth0.net.VoidRequest; +import com.auth0.json.mgmt.selfserviceprofiles.SsoAccessTicketResponse; +import com.auth0.net.*; import com.auth0.net.client.Auth0HttpClient; import com.auth0.net.client.HttpMethod; import com.auth0.utils.Asserts; @@ -25,8 +24,8 @@ public class SelfServiceProfilesEntity extends BaseManagementEntity { /** * Request the list of self-service profiles. - * A token with scope read:self_service_profiles is needed - * See https://auth0.com/docs/api/management/v2/self-service-profiles/get-self-service-profiles + * A token with {@code read:self_service_profiles} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/get-self-service-profiles * @param pageFilter the pagination filter to apply. Can be null to use the default values. * @return a Request to execute. */ @@ -47,8 +46,8 @@ public Request get(PageBasedPaginationFilter pag /** * Create a new self-service profile. - * A token with scope create:self_service_profiles is needed - * See https://auth0.com/docs/api/management/v2/self-service-profiles/post-self-service-profiles + * A token with {@code create:self_service_profiles} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/post-self-service-profiles * @param selfServiceProfile the self-service profile to create. * @return a Request to execute. */ @@ -69,8 +68,8 @@ public Request create(SelfServiceProfile selfService /** * Request the self-service profile with the given ID. - * A token with scope read:self_service_profiles is needed - * See https://auth0.com/docs/api/management/v2/self-service-profiles/get-self-service-profiles-by-id + * A token with {@code read:self_service_profiles} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/get-self-service-profiles-by-id * @param id the self-service profile ID. * @return a Request to execute. */ @@ -89,8 +88,8 @@ public Request getById(String id) { /** * Delete the self-service profile with the given ID. - * A token with scope delete:self_service_profiles is needed - * See https://auth0.com/docs/api/management/v2/self-service-profiles/delete-self-service-profiles-by-id + * A token with {@code delete:self_service_profiles} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/delete-self-service-profiles-by-id * @param id the self-service profile ID. * @return a Request to execute. */ @@ -108,8 +107,8 @@ public Request delete(String id) { /** * Update the self-service profile with the given ID. - * A token with scope update:self_service_profiles is needed - * See https://auth0.com/docs/api/management/v2/self-service-profiles/patch-self-service-profiles-by-id + * A token with {@code update:self_service_profiles} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/patch-self-service-profiles-by-id * @param selfServiceProfile the self-service profile to update. * @param id the self-service profile ID. * @return a Request to execute. @@ -129,4 +128,112 @@ public Request update(SelfServiceProfile selfService request.setBody(selfServiceProfile); return request; } + + /** + * Get the custom text for specific self-service profile and language. + * A token with {@code read:self_service_profile_custom_texts} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/get-self-service-profile-custom-text + * @param id the self-service profile ID. + * @param language the language. + * @param page the page. + * @return a Request to execute. + */ + public Request getCustomText(String id, String language, String page) { + Asserts.assertNotNull(id, "id"); + Asserts.assertNotNull(language, "language"); + Asserts.assertNotNull(page, "page"); + + HttpUrl.Builder builder = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .addPathSegment(id) + .addPathSegment("custom-text") + .addPathSegment(language) + .addPathSegment(page); + + String url = builder.build().toString(); + + return new BaseRequest<>(this.client, tokenProvider, url, HttpMethod.GET, new TypeReference() { + }); + } + + /** + * Set the custom text for specific self-service profile and language. + * A token with {@code update:self_service_profile_custom_texts} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/put-self-service-profile-custom-text + * @param id the self-service profile ID. + * @param language the language. + * @param page the page. + * @param customText the custom text. + * @return a Request to execute. + */ + public Request setCustomText(String id, String language, String page, Object customText) { + Asserts.assertNotNull(id, "id"); + Asserts.assertNotNull(language, "language"); + Asserts.assertNotNull(page, "page"); + Asserts.assertNotNull(customText, "custom text"); + + HttpUrl.Builder builder = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .addPathSegment(id) + .addPathSegment("custom-text") + .addPathSegment(language) + .addPathSegment(page); + + String url = builder.build().toString(); + + BaseRequest request = new BaseRequest<>(this.client, tokenProvider, url, HttpMethod.PUT, new TypeReference() { + }); + request.setBody(customText); + return request; + } + + /** + * Create a new SSO access ticket. + * A token with {@code create:sso_access_tickets} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/post-sso-ticket + * @param id the self-service profile ID. + * @param payload the payload. + * @return a Request to execute. + */ + public Request createSsoAccessTicket(String id, Object payload) { + Asserts.assertNotNull(id, "id"); + Asserts.assertNotNull(payload, "payload"); + + HttpUrl.Builder builder = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .addPathSegment(id) + .addPathSegment("sso-ticket"); + + String url = builder.build().toString(); + + BaseRequest request = new BaseRequest<>(this.client, tokenProvider, url, HttpMethod.POST, new TypeReference() { + }); + request.setBody(payload); + return request; + } + + /** + * Revoke an SSO ticket. + * A token with {@code delete:sso_access_tickets} scope is needed + * @see https://auth0.com/docs/api/management/v2#!/self-service-profiles/post-revoke + * @param id the self-service profile ID. + * @param ticketId the ticket ID. + * @return a Request to execute. + */ + public Request revokeSsoTicket(String id, String ticketId) { + Asserts.assertNotNull(id, "id"); + Asserts.assertNotNull(ticketId, "ticket id"); + + HttpUrl.Builder builder = baseUrl.newBuilder() + .addPathSegments(ORGS_PATH) + .addPathSegment(id) + .addPathSegment("sso-ticket") + .addPathSegment(ticketId) + .addPathSegment("revoke"); + + String url = builder.build().toString(); + + return new EmptyBodyVoidRequest<>(this.client, tokenProvider, url, HttpMethod.POST, new TypeReference() { + }); + } } diff --git a/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponse.java b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponse.java new file mode 100644 index 00000000..ab4bf344 --- /dev/null +++ b/src/main/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponse.java @@ -0,0 +1,41 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.fasterxml.jackson.annotation.*; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonIgnoreProperties(ignoreUnknown = true) +public class SsoAccessTicketResponse { + @JsonProperty("ticket") + private String ticket; + + /** + * Creates a new instance. + */ + public SsoAccessTicketResponse() { + } + + /** + * Creates a new instance with the given ticket. + * @param ticket the ticket. + */ + @JsonCreator + public SsoAccessTicketResponse(@JsonProperty("ticket") String ticket) { + this.ticket = ticket; + } + + /** + * Getter for the ticket. + * @return the ticket. + */ + public String getTicket() { + return ticket; + } + + /** + * Setter for the ticket. + * @param ticket the ticket to set. + */ + public void setTicket(String ticket) { + this.ticket = ticket; + } +} diff --git a/src/test/java/com/auth0/client/MockServer.java b/src/test/java/com/auth0/client/MockServer.java index 541992b2..3c47603b 100644 --- a/src/test/java/com/auth0/client/MockServer.java +++ b/src/test/java/com/auth0/client/MockServer.java @@ -159,7 +159,8 @@ public class MockServer { public static final String RATE_LIMIT_ERROR = "src/test/resources/mgmt/rate_limit_error.json"; public static final String SELF_SERVICE_PROFILES_LIST = "src/test/resources/mgmt/self_service_profiles_list.json"; public static final String SELF_SERVICE_PROFILE_RESPONSE = "src/test/resources/mgmt/self_service_profile_response.json"; - public static final String SELF_SERVICE_PROFILE = "src/test/resources/mgmt/self_service_profile.json"; + public static final String SELF_SERVICE_PROFILE_CUSTOM_TEXT = "src/test/resources/mgmt/self_service_profile_custom_text.json"; + public static final String SELF_SERVICE_PROFILE_SSO_TICKET = "src/test/resources/mgmt/self_service_profile_sso_ticket.json"; private final MockWebServer server; diff --git a/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java b/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java index b71fd0be..227bcfe0 100644 --- a/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java +++ b/src/test/java/com/auth0/client/mgmt/SelfServiceProfilesEntityTest.java @@ -4,11 +4,13 @@ import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfile; import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponse; import com.auth0.json.mgmt.selfserviceprofiles.SelfServiceProfileResponsePage; +import com.auth0.json.mgmt.selfserviceprofiles.SsoAccessTicketResponse; import com.auth0.net.Request; import com.auth0.net.client.HttpMethod; import okhttp3.mockwebserver.RecordedRequest; import org.junit.jupiter.api.Test; +import java.util.HashMap; import java.util.Map; import static com.auth0.AssertsUtil.verifyThrows; @@ -167,7 +169,9 @@ public void shouldThrowOnUpdateWhenSelfServiceProfileIsNull() { @Test public void shouldUpdateSelfServiceProfile() throws Exception { - Request request = api.selfServiceProfiles().update(new SelfServiceProfile(), "id"); + SelfServiceProfile profile = new SelfServiceProfile(); + profile.setDescription("This is Test is updated"); + Request request = api.selfServiceProfiles().update(profile, "id"); assertThat(request, is(notNullValue())); server.jsonResponse(SELF_SERVICE_PROFILE_RESPONSE, 200); @@ -179,7 +183,143 @@ public void shouldUpdateSelfServiceProfile() throws Exception { assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); Map body = bodyFromRequest(recordedRequest); + assertThat(body.size(), is(1)); + assertThat(body, hasEntry("description", "This is Test is updated")); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnGetCustomTextWhenIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().getCustomText(null, "language", "page"), "'id' cannot be null!"); + } + + @Test + public void shouldThrowOnGetCustomTextWhenLanguageIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().getCustomText("id", null, "page"), "'language' cannot be null!"); + } + + @Test + public void shouldThrowOnGetCustomTextWhenPageIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().getCustomText("id", "language", null), "'page' cannot be null!"); + } + + @Test + public void shouldGetCustomText() throws Exception { + Request request = api.selfServiceProfiles().getCustomText("id", "language", "page"); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILE_CUSTOM_TEXT, 200); + Object response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.GET, "/api/v2/self-service-profiles/id/custom-text/language/page")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnSetCustomTextWhenIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().setCustomText(null, "language", "page", new Object()), "'id' cannot be null!"); + } + + @Test + public void shouldThrowOnSetCustomTextWhenLanguageIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().setCustomText("id", null, "page", new Object()), "'language' cannot be null!"); + } + + @Test + public void shouldThrowOnSetCustomTextWhenPageIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().setCustomText("id", "language", null, new Object()), "'page' cannot be null!"); + } + + @Test + public void shouldThrowOnSetCustomTextWhenCustomTextIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().setCustomText("id", "language", "page", null), "'custom text' cannot be null!"); + } + + @Test + public void shouldSetCustomText() throws Exception { + Map customText = new HashMap<>(); + customText.put("introduction", "Welcome! With only a few steps"); + Request request = api.selfServiceProfiles().setCustomText("id", "language", "page", customText); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILE_CUSTOM_TEXT, 200); + Object response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.PUT, "/api/v2/self-service-profiles/id/custom-text/language/page")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); assertThat(response, is(notNullValue())); } + + @Test + public void shouldThrowOnCreateSsoAccessTicketWhenIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().createSsoAccessTicket(null, new Object()), "'id' cannot be null!"); + } + + @Test + public void shouldThrowOnCreateSsoAccessTicketWhenPayloadIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().createSsoAccessTicket("id", null), "'payload' cannot be null!"); + } + + @Test + public void shouldCreateSsoAccessTicket() throws Exception{ + Map payload = new HashMap<>(); + + payload.put("connection_id", "test-connection"); + + Request request = api.selfServiceProfiles().createSsoAccessTicket("id", payload); + assertThat(request, is(notNullValue())); + + server.jsonResponse(SELF_SERVICE_PROFILE_SSO_TICKET, 200); + SsoAccessTicketResponse response = request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/self-service-profiles/id/sso-ticket")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + + assertThat(response, is(notNullValue())); + } + + @Test + public void shouldThrowOnRevokeSsoTicketWhenIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().revokeSsoTicket(null, "ticketId"), "'id' cannot be null!"); + } + + @Test + public void shouldThrowOnRevokeSsoTicketWhenTicketIdIsNull() { + verifyThrows(IllegalArgumentException.class, + () -> api.selfServiceProfiles().revokeSsoTicket("id", null), "'ticket id' cannot be null!"); + } + + @Test + public void shouldRevokeSsoTicket() throws Exception{ + Request request = api.selfServiceProfiles().revokeSsoTicket("id", "ticketId"); + assertThat(request, is(notNullValue())); + + server.noContentResponse(); + request.execute().getBody(); + RecordedRequest recordedRequest = server.takeRequest(); + + assertThat(recordedRequest, hasMethodAndPath(HttpMethod.POST, "/api/v2/self-service-profiles/id/sso-ticket/ticketId/revoke")); + assertThat(recordedRequest, hasHeader("Content-Type", "application/json")); + assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken")); + } } diff --git a/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponseTest.java b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponseTest.java new file mode 100644 index 00000000..3c1eb147 --- /dev/null +++ b/src/test/java/com/auth0/json/mgmt/selfserviceprofiles/SsoAccessTicketResponseTest.java @@ -0,0 +1,31 @@ +package com.auth0.json.mgmt.selfserviceprofiles; + +import com.auth0.json.JsonTest; +import org.junit.jupiter.api.Test; + +import static com.auth0.json.JsonMatcher.hasEntry; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +public class SsoAccessTicketResponseTest extends JsonTest { + + private final static String SELF_SERVICE_PROFILE_SSO_TICKET_JSON = "src/test/resources/mgmt/self_service_profile_sso_ticket.json"; + + @Test + public void deserialize() throws Exception { + SsoAccessTicketResponse deserialized = fromJSON(readTextFile(SELF_SERVICE_PROFILE_SSO_TICKET_JSON), SsoAccessTicketResponse.class); + + assertThat(deserialized.getTicket(), is("https://example.auth0.com/self-service/connections-flow?ticket=ticket-1234")); + } + + @Test + public void serialize() throws Exception { + SsoAccessTicketResponse ssoAccessTicketResponse = new SsoAccessTicketResponse(); + ssoAccessTicketResponse.setTicket("https://example.auth0.com/self-service/connections-flow?ticket=ticket-1234"); + + String serialized = toJSON(ssoAccessTicketResponse); + assertThat(serialized, is(notNullValue())); + + assertThat(serialized, hasEntry("ticket", "https://example.auth0.com/self-service/connections-flow?ticket=ticket-1234")); + } +} diff --git a/src/test/resources/mgmt/self_service_profile_custom_text.json b/src/test/resources/mgmt/self_service_profile_custom_text.json new file mode 100644 index 00000000..ef8aac8d --- /dev/null +++ b/src/test/resources/mgmt/self_service_profile_custom_text.json @@ -0,0 +1,3 @@ +{ + "introduction": "Welcome! With only a few steps" +} diff --git a/src/test/resources/mgmt/self_service_profile_sso_ticket.json b/src/test/resources/mgmt/self_service_profile_sso_ticket.json new file mode 100644 index 00000000..8b8c3e83 --- /dev/null +++ b/src/test/resources/mgmt/self_service_profile_sso_ticket.json @@ -0,0 +1,3 @@ +{ + "ticket": "https://example.auth0.com/self-service/connections-flow?ticket=ticket-1234" +}