Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaprasadreddy committed Nov 30, 2023
1 parent 4a70340 commit 4e7381d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

@Configuration
@EnableWebSecurity
public class SecurityConfig {
class SecurityConfig {

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfig {
static String KEYCLOAK_IMAGE = "quay.io/keycloak/keycloak:23.0";
static String KEYCLOAK_IMAGE = "quay.io/keycloak/keycloak:23.0.1";
static String realmImportFile = "/keycloaktcdemo-realm.json";
static String realmName = "keycloaktcdemo";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
@Import(ContainersConfig.class)
class ProductControllerTests {

public static final String GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials";
public static final String CLIENT_ID = "product-service";
public static final String CLIENT_SECRET = "jTJJqdzeCSt3DmypfHZa42vX8U9rQKZ9";

@LocalServerPort
private int port;

Expand All @@ -42,6 +46,22 @@ void shouldGetProductsWithoutAuthToken() {
when().get("/api/products").then().statusCode(200);
}

@Test
void shouldGetUnauthorizedWhenCreateProductWithoutAuthToken() {
given().contentType("application/json")
.body(
"""
{
"title": "New Product",
"description": "Brand New Product"
}
""")
.when()
.post("/api/products")
.then()
.statusCode(401);
}

@Test
void shouldCreateProductWithAuthToken() {
String token = getToken();
Expand All @@ -56,9 +76,9 @@ void shouldCreateProductWithAuthToken() {
}
""")
.when()
.get("/api/products")
.post("/api/products")
.then()
.statusCode(200);
.statusCode(201);
}

private String getToken() {
Expand All @@ -67,9 +87,9 @@ private String getToken() {
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
map.put("grant_type", singletonList("client_credentials"));
map.put("client_id", singletonList("product-service"));
map.put("client_secret", singletonList("jTJJqdzeCSt3DmypfHZa42vX8U9rQKZ9"));
map.put("grant_type", singletonList(GRANT_TYPE_CLIENT_CREDENTIALS));
map.put("client_id", singletonList(CLIENT_ID));
map.put("client_secret", singletonList(CLIENT_SECRET));

String authServerUrl =
oAuth2ResourceServerProperties.getJwt().getIssuerUri() + "/protocol/openid-connect/token";
Expand Down

0 comments on commit 4e7381d

Please sign in to comment.