From 74849dde2265e9d7460870f0f3145682038c49db Mon Sep 17 00:00:00 2001
From: simeng-li <simeng@silverhand.io>
Date: Thu, 10 Aug 2023 10:28:35 +0800
Subject: [PATCH] test(core): add hasActiveUser test case (#4307)

* test(core): add hasActiveUser test case

add hasActiveUser test case

* ci: remove integration-test code owner

remove integration-test code owner
---
 .github/CODEOWNERS                            |  1 -
 .../integration-tests/src/api/admin-user.ts   |  3 +-
 .../integration-tests/src/helpers/index.ts    |  4 +-
 .../src/tests/api/admin-user.search.test.ts   |  9 +----
 .../src/tests/ui/bootstrap.test.ts            | 38 +++++++++++++++++++
 5 files changed, 41 insertions(+), 14 deletions(-)

diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS
index 037820f5b..aed581e9e 100644
--- a/.github/CODEOWNERS
+++ b/.github/CODEOWNERS
@@ -2,5 +2,4 @@
 /packages/core @simeng-li @wangsijie @gao-sun
 /packages/console @wangsijie @charIeszhao
 /packages/ui @simeng-li @charIeszhao
-/packages/integration-tests @simeng-li
 /.changeset @gao-sun
diff --git a/packages/integration-tests/src/api/admin-user.ts b/packages/integration-tests/src/api/admin-user.ts
index 68194b21d..3a9806b24 100644
--- a/packages/integration-tests/src/api/admin-user.ts
+++ b/packages/integration-tests/src/api/admin-user.ts
@@ -8,10 +8,9 @@ type CreateUserPayload = Partial<{
   username: string;
   password: string;
   name: string;
-  isAdmin: boolean;
 }>;
 
-export const createUser = async (payload: CreateUserPayload) =>
+export const createUser = async (payload: CreateUserPayload = {}) =>
   authedAdminApi
     .post('users', {
       json: payload,
diff --git a/packages/integration-tests/src/helpers/index.ts b/packages/integration-tests/src/helpers/index.ts
index cd06018f2..8b978ccb1 100644
--- a/packages/integration-tests/src/helpers/index.ts
+++ b/packages/integration-tests/src/helpers/index.ts
@@ -15,8 +15,7 @@ export const createUserByAdmin = async (
   password?: string,
   primaryEmail?: string,
   primaryPhone?: string,
-  name?: string,
-  isAdmin = false
+  name?: string
 ) => {
   return createUser({
     username: username ?? generateUsername(),
@@ -24,7 +23,6 @@ export const createUserByAdmin = async (
     name: name ?? username ?? 'John',
     primaryEmail,
     primaryPhone,
-    isAdmin,
   });
 };
 
diff --git a/packages/integration-tests/src/tests/api/admin-user.search.test.ts b/packages/integration-tests/src/tests/api/admin-user.search.test.ts
index e4491057a..3f51986df 100644
--- a/packages/integration-tests/src/tests/api/admin-user.search.test.ts
+++ b/packages/integration-tests/src/tests/api/admin-user.search.test.ts
@@ -49,14 +49,7 @@ describe('admin console user search params', () => {
         const primaryPhone =
           phonePrefix[index % phonePrefix.length]! + index.toString().padStart(5, '0');
 
-        return createUserByAdmin(
-          prefix + username,
-          undefined,
-          primaryEmail,
-          primaryPhone,
-          name,
-          index < 3
-        );
+        return createUserByAdmin(prefix + username, undefined, primaryEmail, primaryPhone, name);
       })
     );
   });
diff --git a/packages/integration-tests/src/tests/ui/bootstrap.test.ts b/packages/integration-tests/src/tests/ui/bootstrap.test.ts
index 340aa9b87..e0d8c497d 100644
--- a/packages/integration-tests/src/tests/ui/bootstrap.test.ts
+++ b/packages/integration-tests/src/tests/ui/bootstrap.test.ts
@@ -1,5 +1,7 @@
+import { type User } from '@logto/schemas';
 import { appendPath } from '@silverhand/essentials';
 
+import { authedAdminTenantApi } from '#src/api/api.js';
 import {
   consolePassword,
   consoleUsername,
@@ -16,6 +18,42 @@ import { appendPathname, expectNavigation } from '#src/utils.js';
 describe('smoke testing for console admin account creation and sign-in', () => {
   const logtoConsoleUrl = new URL(logtoConsoleUrlString);
 
+  it('should not navigate to welcome page if admin tenant user table is not empty', async () => {
+    // Create a admin user
+    const { id } = await authedAdminTenantApi
+      .post('users', {
+        json: { username: 'test_admin_user' },
+      })
+      .json<User>();
+
+    await expectNavigation(page.goto(logtoConsoleUrl.href));
+
+    await expect(page).toMatchElement('#app');
+    expect(page.url()).not.toBe(new URL('console/welcome', logtoConsoleUrl).href);
+
+    // Clean up
+    await authedAdminTenantApi.delete(`users/${id}`);
+  });
+
+  it('should navigate to welcome page if all admin user are suspended', async () => {
+    // Create a admin user
+    const { id } = await authedAdminTenantApi
+      .post('users', {
+        json: { username: 'test_admin_user' },
+      })
+      .json<User>();
+
+    await authedAdminTenantApi.patch(`users/${id}/is-suspended`, { json: { isSuspended: true } });
+
+    await expectNavigation(page.goto(logtoConsoleUrl.href));
+
+    await expect(page).toMatchElement('#app');
+    expect(page.url()).toBe(new URL('console/welcome', logtoConsoleUrl).href);
+
+    // Clean up
+    await authedAdminTenantApi.delete(`users/${id}`);
+  });
+
   it('can open with app element and navigate to welcome page', async () => {
     await expectNavigation(page.goto(logtoConsoleUrl.href));