Skip to content

Commit

Permalink
feat(api-client): Create controller for User module (#484)
Browse files Browse the repository at this point in the history
  • Loading branch information
anudeeps352 authored Oct 13, 2024
1 parent 256d659 commit f9d8e83
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 0 deletions.
66 changes: 66 additions & 0 deletions packages/api-client/src/controllers/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { APIClient } from '@api-client/core/client'
import { parseResponse } from '@api-client/core/response-parser'
import { ClientResponse } from '@api-client/types/index.types'
import {
GetSelfResponse,
UpdateSelfRequest,
UpdateSelfResponse,
ValidateEmailChangeOTPRequest,
ValidateEmailChangeOTPResponse,
ResendEmailChangeOTPRequest,
DeleteSelfResponse,
ResendEmailChangeOTPResponse
} from '@api-client/types/user.types'

export default class UserController {
private apiClient: APIClient

constructor(private readonly backendURL: string) {
this.apiClient = new APIClient(this.backendURL)
}
async getSelf(
headers?: Record<string, string>
): Promise<ClientResponse<GetSelfResponse>> {
const response = await this.apiClient.get(`/api/user`, headers)
return await parseResponse<GetSelfResponse>(response)
}

async updateSelf(
request: UpdateSelfRequest,
headers?: Record<string, string>
): Promise<ClientResponse<UpdateSelfResponse>> {
const response = await this.apiClient.put(`./api/user`, request, headers)
return await parseResponse<UpdateSelfResponse>(response)
}

async deleteSelf(
headers?: Record<string, string>
): Promise<ClientResponse<DeleteSelfResponse>> {
const response = await this.apiClient.delete(`./api/user`, headers)
return await parseResponse<DeleteSelfResponse>(response)
}

async validateEmailChangeOTP(
request: ValidateEmailChangeOTPRequest,
headers?: Record<string, string>
): Promise<ClientResponse<ValidateEmailChangeOTPResponse>> {
const response = await this.apiClient.post(
`./api/user/validate-email-change-otp?otp=${request.otp}`,
request,
headers
)
return await parseResponse<ValidateEmailChangeOTPResponse>(response)
}

async resendEmailChangeOTP(
request: ResendEmailChangeOTPRequest,
headers?: Record<string, string>
): Promise<ClientResponse<ResendEmailChangeOTPResponse>> {
const response = await this.apiClient.post(
`./api/user/resend-email-change-otp`,
request,
headers
)
return await parseResponse<ResendEmailChangeOTPResponse>(response)
}
}
34 changes: 34 additions & 0 deletions packages/api-client/src/types/user.types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { PageRequest, PageResponse } from '@api-client/index'
import { Workspace } from '@api-client/types/workspace.types'

export interface GetSelfResponse {
id: string
email: string
name: string
profilePictureUrl: string | null
isActive: boolean
isOnboardingFinished: boolean
isAdmin: boolean
authProvider: string
defaultWorkspace: Workspace
}
export interface UpdateSelfRequest {
name?: string
profilePictureUrl?: string
isOnboardingFinished?: boolean
email?: string
}
export interface UpdateSelfResponse
extends Partial<Omit<GetSelfResponse, 'defaultWorkspace'>> {}

export interface DeleteSelfRequest {}
export interface DeleteSelfResponse {}

export interface ValidateEmailChangeOTPRequest {
otp: string
}
export interface ValidateEmailChangeOTPResponse
extends Partial<Omit<GetSelfResponse, 'defaultWorkspace'>> {}

export interface ResendEmailChangeOTPRequest {}
export interface ResendEmailChangeOTPResponse {}
100 changes: 100 additions & 0 deletions packages/api-client/tests/user.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { APIClient } from '@api-client/core/client'
import UserController from '@api-client/controllers/user'

describe('User Controller Tests', () => {
const backendURL = process.env.BACKEND_URL

const client = new APIClient(backendURL)
const userController = new UserController(backendURL)

const email = 'johndoe@example.com'
let projectSlug: string | null
let workspaceSlug: string | null
let environmentSlug: string | null

beforeAll(async () => {
//Create the user's workspace
const workspaceResponse = (await (
await client.post(
'/api/workspace',
{
name: 'My Workspace'
},
{
'x-e2e-user-email': email
}
)
).json()) as any

workspaceSlug = workspaceResponse.slug

// Create a project
const projectResponse = (await (
await client.post(
`/api/project/${workspaceSlug}`,
{
name: 'Project',
storePrivateKey: true
},
{
'x-e2e-user-email': email
}
)
).json()) as any

projectSlug = projectResponse.slug

const createEnvironmentResponse = (await (
await client.post(
`/api/environment/${projectSlug}`,
{
name: 'Dev'
},
{
'x-e2e-user-email': email
}
)
).json()) as any

environmentSlug = createEnvironmentResponse.slug
})

afterAll(async () => {
// Delete the workspace
await client.delete(`/api/workspace/${workspaceSlug}`, {
'x-e2e-user-email': email
})
})

// Get Current User
it('should get current user', async () => {
const user = await userController.getSelf({ 'x-e2e-user-email': email })

expect(user.data.defaultWorkspace.name).toBe('My Workspace')
expect(user.data.email).toBe('johndoe@example.com')
})

// Update Current User
it('should update current user', async () => {
const user = await userController.updateSelf(
{ name: 'Jane Doe', email: 'janedoe@example.com' },
{ 'x-e2e-user-email': email }
)

expect(user.data.name).toBe('Jane Doe')
expect(user.data.email).toBe('janedoe@example.com')
})

// Delete Current User
it('should update current user', async () => {
const deleteUser = await userController.updateSelf(
{ name: 'Jane Doe', email: 'janedoe@example.com' },
{ 'x-e2e-user-email': email }
)

expect(deleteUser.success).toBe(true)
})

// Validate email change OTP
// resend validate email OTP tests
})

0 comments on commit f9d8e83

Please sign in to comment.