fix: 开启自助注册须先有 8-64 字符安全码
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
import { config, mount, flushPromises } from "@vue/test-utils";
|
||||
import { createPinia, setActivePinia } from "pinia";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
NConfigProvider,
|
||||
NDialogProvider,
|
||||
NMessageProvider,
|
||||
dateZhCN,
|
||||
zhCN,
|
||||
} from "naive-ui";
|
||||
import { defineComponent, h } from "vue";
|
||||
import RegistrationView from "./RegistrationView.vue";
|
||||
import { mockApi } from "@/api/mock";
|
||||
|
||||
vi.mock("@/api/admin", async () => import("@/api/admin-mock"));
|
||||
|
||||
config.global.stubs = { teleport: true };
|
||||
|
||||
function wrap(Comp: object) {
|
||||
return defineComponent({
|
||||
setup() {
|
||||
return () =>
|
||||
h(NConfigProvider, { locale: zhCN, dateLocale: dateZhCN, size: "small" }, {
|
||||
default: () =>
|
||||
h(NMessageProvider, null, {
|
||||
default: () =>
|
||||
h(NDialogProvider, null, {
|
||||
default: () => h(Comp),
|
||||
}),
|
||||
}),
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
describe("RegistrationView", () => {
|
||||
beforeEach(() => {
|
||||
setActivePinia(createPinia());
|
||||
mockApi._setSession();
|
||||
});
|
||||
|
||||
it("已有安全码时不显示缺码提示", async () => {
|
||||
const pinia = createPinia();
|
||||
const w = mount(wrap(RegistrationView), {
|
||||
global: { plugins: [pinia] },
|
||||
attachTo: document.body,
|
||||
});
|
||||
await flushPromises();
|
||||
expect(w.get('[data-testid="reg-enabled"]').exists()).toBe(true);
|
||||
expect(w.find('[data-testid="reg-code-missing"]').exists()).toBe(false);
|
||||
w.unmount();
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { NButton, NForm, NFormItem, NInput, NSpace, NSpin, NSwitch, NText } from "naive-ui";
|
||||
import { computed, onMounted, ref } from "vue";
|
||||
import { NAlert, NButton, NForm, NFormItem, NInput, NSpace, NSpin, NSwitch, NText } from "naive-ui";
|
||||
import PageHeader from "@/components/PageHeader.vue";
|
||||
import HelpTip from "@/components/HelpTip.vue";
|
||||
import { getRegistration, updateRegistration, type RegistrationSettings } from "@/api/admin";
|
||||
@@ -12,6 +12,9 @@ const saving = ref(false);
|
||||
const data = ref<RegistrationSettings | null>(null);
|
||||
const codeDraft = ref("");
|
||||
|
||||
const hasSavedCode = computed(() => (data.value?.code ?? "").length >= 8);
|
||||
const switchDisabled = computed(() => !hasSavedCode.value && !data.value?.enabled);
|
||||
|
||||
async function load() {
|
||||
loading.value = true;
|
||||
try {
|
||||
@@ -28,6 +31,10 @@ onMounted(() => {
|
||||
|
||||
async function onToggle(enabled: boolean) {
|
||||
if (!data.value) return;
|
||||
if (enabled && !hasSavedCode.value) {
|
||||
message.error("请先保存或生成安全码");
|
||||
return;
|
||||
}
|
||||
saving.value = true;
|
||||
try {
|
||||
data.value = await updateRegistration({ enabled });
|
||||
@@ -66,6 +73,13 @@ async function generateCode() {
|
||||
<div class="page-body">
|
||||
<n-spin :show="loading">
|
||||
<n-form v-if="data" label-placement="left" label-width="100" :show-feedback="false" style="max-width: 560px">
|
||||
<n-alert
|
||||
v-if="!hasSavedCode"
|
||||
type="warning"
|
||||
title="请先保存或生成安全码"
|
||||
data-testid="reg-code-missing"
|
||||
style="margin-bottom: 12px"
|
||||
/>
|
||||
<n-form-item>
|
||||
<template #label>
|
||||
开放注册
|
||||
@@ -74,6 +88,7 @@ async function generateCode() {
|
||||
<n-switch
|
||||
:value="data.enabled"
|
||||
:loading="saving"
|
||||
:disabled="switchDisabled"
|
||||
data-testid="reg-enabled"
|
||||
@update:value="onToggle"
|
||||
/>
|
||||
@@ -88,7 +103,7 @@ async function generateCode() {
|
||||
<n-button type="primary" :loading="saving" data-testid="reg-save-code" @click="saveCode">
|
||||
保存安全码
|
||||
</n-button>
|
||||
<n-button :loading="saving" @click="generateCode">生成安全码</n-button>
|
||||
<n-button :loading="saving" data-testid="reg-generate-code" @click="generateCode">生成安全码</n-button>
|
||||
</n-space>
|
||||
</n-form>
|
||||
</n-spin>
|
||||
|
||||
Reference in New Issue
Block a user