Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
ruibaby committed Nov 23, 2023
2 parents dbaf0a7 + 457e059 commit 168e45a
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public static boolean isPublished(MetadataOperator metadata) {
return labels != null && parseBoolean(labels.getOrDefault(PUBLISHED_LABEL, "false"));
}

public static boolean isPublic(PostSpec spec) {
return spec.getVisible() == null || VisibleEnum.PUBLIC.equals(spec.getVisible());
}

@Data
public static class PostSpec {
@Schema(requiredMode = RequiredMode.REQUIRED, minLength = 1)
Expand Down
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
Expand Up @@ -40,7 +40,8 @@ public Mono<Void> rebuildPostIndices() {

private Mono<Void> rebuildPostIndices(PostSearchService searchService) {
return postFinder.listAll()
.filter(post -> Post.isPublished(post.getMetadata()))
.filter(
post -> Post.isPublished(post.getMetadata()) && Post.isPublic(post.getSpec()))
.flatMap(listedPostVo -> {
PostVo postVo = PostVo.from(listedPostVo);
return postFinder.content(postVo.getMetadata().getName())
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);
}
}
14 changes: 9 additions & 5 deletions console/src/vite/config-builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fileURLToPath, URL } from "url";
import { fileURLToPath } from "url";
import fs from "fs";
import { defineConfig, type Plugin } from "vite";
import Vue from "@vitejs/plugin-vue";
Expand All @@ -7,6 +7,7 @@ import { VitePWA } from "vite-plugin-pwa";
import Icons from "unplugin-icons/vite";
import { setupLibraryExternal } from "./library-external";
import GzipPlugin from "rollup-plugin-gzip";
import path from "path";

interface Options {
base: string;
Expand Down Expand Up @@ -49,6 +50,9 @@ export function createViteConfig(options: Options) {

const { base, entryFile, port, outDir, plugins } = options;

const currentFileDir = path.dirname(fileURLToPath(import.meta.url));
const rootDir = path.resolve(currentFileDir, "../..");

return defineConfig({
base,
plugins: [
Expand All @@ -58,9 +62,9 @@ export function createViteConfig(options: Options) {
],
resolve: {
alias: {
"@": fileURLToPath(new URL("/src", import.meta.url)),
"@console": fileURLToPath(new URL("/console-src", import.meta.url)),
"@uc": fileURLToPath(new URL("/uc-src", import.meta.url)),
"@": path.resolve(rootDir, "src"),
"@console": path.resolve(rootDir, "console-src"),
"@uc": path.resolve(rootDir, "uc-src"),
},
},
server: {
Expand All @@ -70,7 +74,7 @@ export function createViteConfig(options: Options) {
},
},
build: {
outDir: fileURLToPath(new URL(outDir, import.meta.url)),
outDir: path.resolve(rootDir, outDir),
emptyOutDir: true,
chunkSizeWarningLimit: 2048,
},
Expand Down

0 comments on commit 168e45a

Please sign in to comment.