Skip to content

Commit

Permalink
Respond HTTP status CREATED for system initialization API (halo-dev#4887
Browse files Browse the repository at this point in the history
)

#### What type of PR is this?

/kind improvement
/area core
/milestone 2.11.x

#### What this PR does / why we need it:

Respond HTTP status CREATED for system initialization API instead of string `true`.

#### Which issue(s) this PR fixes:

Fixes halo-dev#4885 

#### Does this PR introduce a user-facing change?

```release-note
None
```
  • Loading branch information
JohnNiang authored Nov 22, 2023
1 parent aa9f84e commit 457e059
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;
import static org.springdoc.core.fn.builders.apiresponse.Builder.responseBuilder;
import static org.springdoc.core.fn.builders.header.Builder.headerBuilder;
import static org.springdoc.core.fn.builders.requestbody.Builder.requestBodyBuilder;

import io.swagger.v3.oas.annotations.media.Schema;
import java.net.URI;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -13,6 +15,7 @@
import org.apache.commons.lang3.StringUtils;
import org.springdoc.webflux.core.fn.SpringdocRouteBuilder;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
Expand Down Expand Up @@ -56,7 +59,14 @@ public RouterFunction<ServerResponse> endpoint() {
.tag(tag)
.requestBody(requestBodyBuilder()
.implementation(SystemInitializationRequest.class))
.response(responseBuilder().implementation(Boolean.class))
.response(responseBuilder()
.responseCode(HttpStatus.CREATED.value() + "")
.description("System initialization successfully.")
.header(headerBuilder()
.name(HttpHeaders.LOCATION)
.description("Redirect URL.")
)
)
)
.build();
}
Expand Down Expand Up @@ -87,7 +97,7 @@ private Mono<ServerResponse> initialize(ServerRequest request) {
return initializeSystem(requestBody);
})
)
.then(ServerResponse.ok().bodyValue(true));
.then(ServerResponse.created(URI.create("/console")).build());
}

private Mono<Void> initializeSystem(SystemInitializationRequest requestBody) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
package run.halo.app.core.extension.endpoint;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.reactive.server.WebTestClient.bindToRouterFunction;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
import run.halo.app.core.extension.endpoint.SystemInitializationEndpoint.SystemInitializationRequest;
import run.halo.app.extension.ConfigMap;
import run.halo.app.extension.ReactiveExtensionClient;
import run.halo.app.infra.InitializationStateGetter;
import run.halo.app.infra.SystemSetting;
import run.halo.app.security.SuperAdminInitializer;
import run.halo.app.security.SuperAdminInitializer.InitializationParam;

/**
* Tests for {@link SystemInitializationEndpoint}.
Expand All @@ -18,6 +30,15 @@
@ExtendWith(MockitoExtension.class)
class SystemInitializationEndpointTest {

@Mock
InitializationStateGetter initializationStateGetter;

@Mock
SuperAdminInitializer superAdminInitializer;

@Mock
ReactiveExtensionClient client;

@InjectMocks
SystemInitializationEndpoint initializationEndpoint;

Expand All @@ -29,11 +50,40 @@ void setUp() {
}

@Test
void initialize() {
void initializeWithoutRequestBody() {
webTestClient.post()
.uri("/system/initialize")
.exchange()
.expectStatus()
.isBadRequest();
}

@Test
void initializeWithRequestBody() {
var initialization = new SystemInitializationRequest();
initialization.setUsername("faker");
initialization.setPassword("openfaker");
initialization.setEmail("faker@halo.run");
initialization.setSiteTitle("Fake Site");

when(initializationStateGetter.userInitialized()).thenReturn(Mono.just(false));
when(superAdminInitializer.initialize(any(InitializationParam.class)))
.thenReturn(Mono.empty());

var configMap = new ConfigMap();
when(client.get(ConfigMap.class, SystemSetting.SYSTEM_CONFIG))
.thenReturn(Mono.just(configMap));
when(client.update(configMap)).thenReturn(Mono.just(configMap));

webTestClient.post().uri("/system/initialize")
.bodyValue(initialization)
.exchange()
.expectStatus().isCreated()
.expectHeader().location("/console");

verify(initializationStateGetter).userInitialized();
verify(superAdminInitializer).initialize(any());
verify(client).get(ConfigMap.class, SystemSetting.SYSTEM_CONFIG);
verify(client).update(configMap);
}
}

0 comments on commit 457e059

Please sign in to comment.