Skip to content

Commit a097c4a

Browse files
committed
test: fix db setup
1 parent b805718 commit a097c4a

File tree

2 files changed

+92
-82
lines changed

2 files changed

+92
-82
lines changed

test/google-oauth-flow.test.ts

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2-
import type { ModuleOptions, User } from '../src/types'
3-
import { defaultOptions } from '../src/module'
4-
import { useDb } from '../src/runtime/server/utils/db'
1+
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
2+
import type { Database } from 'db0'
3+
import type { ModuleOptions, User, DatabaseType, DatabaseConfig } from '../src/types'
4+
import { cleanupTestSetup, createTestSetup } from './test-setup'
5+
import { createUsersTable } from '../src/runtime/server/utils/create-users-table'
6+
import { createPersonalAccessTokensTable } from '../src/runtime/server/utils/create-personal-access-tokens-table'
7+
import { addActiveToUsers } from '../src/runtime/server/utils/add-active-to-users'
8+
import { addGoogleOauthFields } from '../src/runtime/server/utils/add-google-oauth-fields'
59
import bcrypt from 'bcrypt'
6-
import crypto from 'node:crypto'
710

811
/**
912
* Layer 2: Integration Tests - Google OAuth Complete Flow
@@ -17,22 +20,47 @@ import crypto from 'node:crypto'
1720
*/
1821

1922
describe('Google OAuth Complete Flow Integration', () => {
23+
let db: Database
2024
let testOptions: ModuleOptions
21-
let db: Awaited<ReturnType<typeof useDb>>
25+
let dbType: DatabaseType
26+
let dbConfig: DatabaseConfig
2227

2328
beforeEach(async () => {
24-
vi.clearAllMocks()
29+
dbType = process.env.DB_CONNECTOR as DatabaseType || 'sqlite'
30+
if (dbType === 'sqlite') {
31+
dbConfig = {
32+
path: './_google_oauth_flow',
33+
}
34+
}
35+
if (dbType === 'mysql') {
36+
dbConfig = {
37+
host: process.env.DB_HOST,
38+
port: Number.parseInt(process.env.DB_PORT || '3306'),
39+
user: process.env.DB_USER,
40+
password: process.env.DB_PASSWORD,
41+
database: process.env.DB_NAME
42+
}
43+
}
44+
if (dbType === 'postgresql') {
45+
dbConfig = {
46+
host: process.env.DB_HOST,
47+
port: Number.parseInt(process.env.DB_PORT || '5432'),
48+
user: process.env.DB_USER,
49+
password: process.env.DB_PASSWORD,
50+
database: process.env.DB_NAME
51+
}
52+
}
53+
54+
const settings = await createTestSetup({
55+
dbType,
56+
dbConfig,
57+
})
2558

59+
db = settings.db
2660
testOptions = {
27-
...defaultOptions,
28-
connector: {
29-
name: 'sqlite',
30-
options: {
31-
path: ':memory:'
32-
}
33-
},
61+
...settings.testOptions,
3462
auth: {
35-
...defaultOptions.auth,
63+
...settings.testOptions.auth,
3664
google: {
3765
clientId: 'test-client-id',
3866
clientSecret: 'test-client-secret',
@@ -43,45 +71,15 @@ describe('Google OAuth Complete Flow Integration', () => {
4371
}
4472
}
4573

46-
// Initialize database
47-
db = await useDb(testOptions)
48-
49-
// Create tables
50-
await db.sql`
51-
CREATE TABLE IF NOT EXISTS users (
52-
id INTEGER PRIMARY KEY AUTOINCREMENT,
53-
email TEXT NOT NULL UNIQUE,
54-
name TEXT NOT NULL,
55-
password TEXT NOT NULL,
56-
role TEXT NOT NULL DEFAULT 'user',
57-
google_id TEXT UNIQUE,
58-
profile_picture TEXT,
59-
active BOOLEAN DEFAULT TRUE,
60-
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
61-
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
62-
)
63-
`
64-
65-
await db.sql`
66-
CREATE TABLE IF NOT EXISTS personal_access_tokens (
67-
id INTEGER PRIMARY KEY AUTOINCREMENT,
68-
tokenable_type TEXT NOT NULL,
69-
tokenable_id INTEGER NOT NULL,
70-
name TEXT NOT NULL,
71-
token TEXT NOT NULL UNIQUE,
72-
abilities TEXT,
73-
last_used_at DATETIME,
74-
expires_at DATETIME,
75-
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
76-
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
77-
)
78-
`
74+
await createUsersTable(testOptions)
75+
await addActiveToUsers(testOptions)
76+
await addGoogleOauthFields(testOptions)
77+
await createPersonalAccessTokensTable(testOptions)
7978
})
8079

8180
afterEach(async () => {
82-
await db.sql`DROP TABLE IF EXISTS users`
83-
await db.sql`DROP TABLE IF EXISTS personal_access_tokens`
84-
vi.clearAllMocks()
81+
await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.users)
82+
await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.personalAccessTokens)
8583
})
8684

8785
describe('Scenario 1: New User Registration via OAuth', () => {

test/unit/utils.google-oauth.test.ts

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
2-
import type { ModuleOptions, User } from '../../src/types'
3-
import { defaultOptions } from '../../src/module'
4-
import { useDb } from '../../src/runtime/server/utils/db'
2+
import type { Database } from 'db0'
3+
import type { ModuleOptions, User, DatabaseType, DatabaseConfig } from '../../src/types'
4+
import { cleanupTestSetup, createTestSetup } from '../test-setup'
5+
import { createUsersTable } from '../../src/runtime/server/utils/create-users-table'
6+
import { addActiveToUsers } from '../../src/runtime/server/utils/add-active-to-users'
7+
import { addGoogleOauthFields } from '../../src/runtime/server/utils/add-google-oauth-fields'
58
import bcrypt from 'bcrypt'
69

710
// Mock Google APIs
@@ -34,45 +37,54 @@ const {
3437
} = await import('../../src/runtime/server/utils/google-oauth')
3538

3639
describe('Google OAuth Utilities', () => {
40+
let db: Database
3741
let testOptions: ModuleOptions
38-
let db: Awaited<ReturnType<typeof useDb>>
42+
let dbType: DatabaseType
43+
let dbConfig: DatabaseConfig
3944

4045
beforeEach(async () => {
4146
vi.clearAllMocks()
4247

43-
testOptions = {
44-
...defaultOptions,
45-
connector: {
46-
name: 'sqlite',
47-
options: {
48-
path: ':memory:'
49-
}
48+
dbType = process.env.DB_CONNECTOR as DatabaseType || 'sqlite'
49+
if (dbType === 'sqlite') {
50+
dbConfig = {
51+
path: './_google_oauth_utils',
52+
}
53+
}
54+
if (dbType === 'mysql') {
55+
dbConfig = {
56+
host: process.env.DB_HOST,
57+
port: Number.parseInt(process.env.DB_PORT || '3306'),
58+
user: process.env.DB_USER,
59+
password: process.env.DB_PASSWORD,
60+
database: process.env.DB_NAME
61+
}
62+
}
63+
if (dbType === 'postgresql') {
64+
dbConfig = {
65+
host: process.env.DB_HOST,
66+
port: Number.parseInt(process.env.DB_PORT || '5432'),
67+
user: process.env.DB_USER,
68+
password: process.env.DB_PASSWORD,
69+
database: process.env.DB_NAME
5070
}
5171
}
5272

53-
// Initialize database
54-
db = await useDb(testOptions)
55-
56-
// Create users table
57-
await db.sql`
58-
CREATE TABLE IF NOT EXISTS users (
59-
id INTEGER PRIMARY KEY AUTOINCREMENT,
60-
email TEXT NOT NULL UNIQUE,
61-
name TEXT NOT NULL,
62-
password TEXT NOT NULL,
63-
role TEXT NOT NULL DEFAULT 'user',
64-
google_id TEXT UNIQUE,
65-
profile_picture TEXT,
66-
active BOOLEAN DEFAULT TRUE,
67-
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
68-
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
69-
)
70-
`
73+
const settings = await createTestSetup({
74+
dbType,
75+
dbConfig,
76+
})
77+
78+
db = settings.db
79+
testOptions = settings.testOptions
80+
81+
await createUsersTable(testOptions)
82+
await addActiveToUsers(testOptions)
83+
await addGoogleOauthFields(testOptions)
7184
})
7285

7386
afterEach(async () => {
74-
// Clean up database
75-
await db.sql`DROP TABLE IF EXISTS users`
87+
await cleanupTestSetup(dbType, db, [testOptions.connector!.options.path!], testOptions.tables.users)
7688
vi.clearAllMocks()
7789
})
7890

0 commit comments

Comments
 (0)