-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
✅ test: add unit tests for NextAuthUserService #11337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Reviewer's GuideAdds a comprehensive Vitest unit test suite for NextAuthUserService, using a fully mocked LobeChatDatabase, UserModel, and logger to validate user, session, account, authenticator, verification-token operations and webhook-safe handlers across happy paths and failure scenarios. Sequence diagram for safeUpdateUser webhook handler in NextAuthUserServicesequenceDiagram
actor ProviderWebhook
participant NextAuthUserService
participant LobeChatDatabase
participant UserModel
participant Logger
ProviderWebhook->>NextAuthUserService: safeUpdateUser(accountKey, updateData)
NextAuthUserService->>Logger: info("nextauth safeUpdateUser: updating user")
NextAuthUserService->>LobeChatDatabase: select().from(users).innerJoin(nextauthAccounts).where(accountKey)
LobeChatDatabase-->>NextAuthUserService: userWithAccountRows
alt user found
NextAuthUserService->>UserModel: updateUser(updateData)
UserModel-->>NextAuthUserService: updatedUser
NextAuthUserService-->>ProviderWebhook: { status: 200, body: updatedUser }
else user not found
NextAuthUserService->>Logger: warn("nextauth safeUpdateUser: no user was found")
NextAuthUserService-->>ProviderWebhook: { status: 200, body: null }
end
Class diagram for NextAuthUserService and its mocked collaboratorsclassDiagram
class NextAuthUserService {
- LobeChatDatabase db
+ NextAuthUserService(db)
+ createUser(adapterUser)
+ getUser(userId)
+ getUserByEmail(email)
+ getUserByAccount(account)
+ updateUser(adapterUser)
+ deleteUser(userId)
+ createSession(sessionData)
+ getSessionAndUser(sessionToken)
+ updateSession(sessionData)
+ deleteSession(sessionToken)
+ linkAccount(account)
+ unlinkAccount(accountKey)
+ getAccount(providerAccountId, provider)
+ createAuthenticator(authenticatorData)
+ getAuthenticator(credentialID)
+ listAuthenticatorsByUserId(userId)
+ updateAuthenticatorCounter(credentialID, counter)
+ createVerificationToken(tokenData)
+ useVerificationToken(tokenKey)
+ safeUpdateUser(accountKey, updateData)
+ safeSignOutUser(accountKey)
}
class LobeChatDatabase {
+ select()
+ insert()
+ update()
+ delete()
}
class UserModel {
+ findById(db, userId)
+ findByEmail(db, email)
+ createUser(db, userData)
+ deleteUser(db, userId)
+ updateUser(updateData)
}
class Logger {
+ info(message)
+ warn(message)
}
class nextauthAccounts {
}
class nextauthSessions {
}
class nextauthAuthenticators {
}
class nextauthVerificationTokens {
}
class users {
}
NextAuthUserService --> LobeChatDatabase : uses
NextAuthUserService --> UserModel : uses
NextAuthUserService --> Logger : logs
LobeChatDatabase --> nextauthAccounts : table
LobeChatDatabase --> nextauthSessions : table
LobeChatDatabase --> nextauthAuthenticators : table
LobeChatDatabase --> nextauthVerificationTokens : table
LobeChatDatabase --> users : table
Flow diagram for createUser logic in NextAuthUserServiceflowchart TD
A["createUser(adapterUser)"] --> B{"email is non-empty and not whitespace?"}
B -- No --> C{"adapterUser.providerAccountId exists?"}
B -- Yes --> D["UserModel.findByEmail(db, email)"]
D --> E{"user found by email?"}
E -- Yes --> F["return mapped existing user"]
E -- No --> C
C -- Yes --> G["UserModel.findById(db, providerAccountId)"]
G --> H{"user found by providerAccountId?"}
H -- Yes --> I["return mapped existing user"]
H -- No --> J["use providerAccountId as new user id"]
C -- No --> K["use adapterUser.id as new user id"]
J --> L
K --> L
L["UserModel.createUser(db, newUserData)"] --> M["return mapped created user"]
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Summary
NextAuthUserServiceChanges
Module Processed
src/server/services/nextAuthUser/Test Coverage
Functions tested:
User Management (7 tests):
createUser- user creation with duplicate detection by email and providerAccountIdgetUser- user retrieval by IDgetUserByEmail- user retrieval with email validationgetUserByAccount- user retrieval via provider accountupdateUser- user updates with error handlingdeleteUser- user deletion with validationSession Management (4 tests):
createSession- session creationgetSessionAndUser- session retrieval with user joinupdateSession- session updatesdeleteSession- session deletionAccount Management (4 tests):
linkAccount- account linking with error handlingunlinkAccount- account unlinkinggetAccount- account retrievalAuthenticator Operations (5 tests):
createAuthenticator- authenticator creationgetAuthenticator- single authenticator retrieval with error handlinglistAuthenticatorsByUserId- authenticator list retrievalupdateAuthenticatorCounter- counter updates with error handlingVerification Tokens (2 tests):
createVerificationToken- token creationuseVerificationToken- token consumption and deletionWebhook Handlers (4 tests):
safeUpdateUser- safe user updates via provider webhookssafeSignOutUser- safe sign-out via provider webhooksCoverage type:
Unit tests with comprehensive mocking
Test approach:
🤖 Generated with Claude Code
Summary by Sourcery
Add comprehensive unit tests for the NextAuthUserService covering its NextAuth adapter behavior.
Tests: