|
| 1 | +/** |
| 2 | + * Copyright 2025 LY Corporation |
| 3 | + * |
| 4 | + * LY Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +import type { Server } from 'net'; |
| 17 | +import { faker } from '@faker-js/faker'; |
| 18 | +import type { INestApplication } from '@nestjs/common'; |
| 19 | +import { ConfigService } from '@nestjs/config'; |
| 20 | +import type { TestingModule } from '@nestjs/testing'; |
| 21 | +import { Test } from '@nestjs/testing'; |
| 22 | +import { getDataSourceToken } from '@nestjs/typeorm'; |
| 23 | +import request from 'supertest'; |
| 24 | +import type { DataSource } from 'typeorm'; |
| 25 | +import { initializeTransactionalContext } from 'typeorm-transactional'; |
| 26 | + |
| 27 | +import { AppModule } from '@/app.module'; |
| 28 | +import { OpensearchRepository } from '@/common/repositories'; |
| 29 | +import { AuthService } from '@/domains/admin/auth/auth.service'; |
| 30 | +import { ApiKeyService } from '@/domains/admin/project/api-key/api-key.service'; |
| 31 | +import { CreateApiKeyRequestDto } from '@/domains/admin/project/api-key/dtos/requests'; |
| 32 | +import type { FindApiKeysResponseDto } from '@/domains/admin/project/api-key/dtos/responses'; |
| 33 | +import type { ProjectEntity } from '@/domains/admin/project/project/project.entity'; |
| 34 | +import { ProjectService } from '@/domains/admin/project/project/project.service'; |
| 35 | +import { SetupTenantRequestDto } from '@/domains/admin/tenant/dtos/requests'; |
| 36 | +import { TenantService } from '@/domains/admin/tenant/tenant.service'; |
| 37 | +import { clearAllEntities, signInTestUser } from '@/test-utils/util-functions'; |
| 38 | + |
| 39 | +describe('ApiKeyController (integration)', () => { |
| 40 | + let app: INestApplication; |
| 41 | + |
| 42 | + let dataSource: DataSource; |
| 43 | + let authService: AuthService; |
| 44 | + let tenantService: TenantService; |
| 45 | + let projectService: ProjectService; |
| 46 | + let _apiKeyService: ApiKeyService; |
| 47 | + let configService: ConfigService; |
| 48 | + let opensearchRepository: OpensearchRepository; |
| 49 | + |
| 50 | + let project: ProjectEntity; |
| 51 | + let accessToken: string; |
| 52 | + |
| 53 | + beforeAll(async () => { |
| 54 | + initializeTransactionalContext(); |
| 55 | + const module: TestingModule = await Test.createTestingModule({ |
| 56 | + imports: [AppModule], |
| 57 | + }).compile(); |
| 58 | + |
| 59 | + app = module.createNestApplication(); |
| 60 | + await app.init(); |
| 61 | + |
| 62 | + dataSource = module.get(getDataSourceToken()); |
| 63 | + authService = module.get(AuthService); |
| 64 | + tenantService = module.get(TenantService); |
| 65 | + projectService = module.get(ProjectService); |
| 66 | + _apiKeyService = module.get(ApiKeyService); |
| 67 | + configService = module.get(ConfigService); |
| 68 | + opensearchRepository = module.get(OpensearchRepository); |
| 69 | + |
| 70 | + await clearAllEntities(module); |
| 71 | + if (configService.get('opensearch.use')) { |
| 72 | + await opensearchRepository.deleteAllIndexes(); |
| 73 | + } |
| 74 | + |
| 75 | + const dto = new SetupTenantRequestDto(); |
| 76 | + dto.siteName = faker.string.sample(); |
| 77 | + dto.password = '12345678'; |
| 78 | + await tenantService.create(dto); |
| 79 | + |
| 80 | + project = await projectService.create({ |
| 81 | + name: faker.lorem.words(), |
| 82 | + description: faker.lorem.lines(1), |
| 83 | + timezone: { |
| 84 | + countryCode: 'KR', |
| 85 | + name: 'Asia/Seoul', |
| 86 | + offset: '+09:00', |
| 87 | + }, |
| 88 | + }); |
| 89 | + |
| 90 | + const { jwt } = await signInTestUser(dataSource, authService); |
| 91 | + accessToken = jwt.accessToken; |
| 92 | + }); |
| 93 | + |
| 94 | + describe('/admin/projects/:projectId/api-keys (POST)', () => { |
| 95 | + it('should create an API key', async () => { |
| 96 | + const dto = new CreateApiKeyRequestDto(); |
| 97 | + dto.value = 'TestApiKey1234567890'; |
| 98 | + |
| 99 | + return request(app.getHttpServer() as Server) |
| 100 | + .post(`/admin/projects/${project.id}/api-keys`) |
| 101 | + .set('Authorization', `Bearer ${accessToken}`) |
| 102 | + .send(dto) |
| 103 | + .expect(201) |
| 104 | + .then( |
| 105 | + ({ |
| 106 | + body, |
| 107 | + }: { |
| 108 | + body: { |
| 109 | + id: number; |
| 110 | + value: string; |
| 111 | + createdAt: Date; |
| 112 | + }; |
| 113 | + }) => { |
| 114 | + expect(body).toHaveProperty('id'); |
| 115 | + expect(body).toHaveProperty('value'); |
| 116 | + expect(body).toHaveProperty('createdAt'); |
| 117 | + expect(body.value).toBe('TestApiKey1234567890'); |
| 118 | + }, |
| 119 | + ); |
| 120 | + }); |
| 121 | + |
| 122 | + it('should create an API key with auto-generated value when not provided', async () => { |
| 123 | + const dto = new CreateApiKeyRequestDto(); |
| 124 | + |
| 125 | + return request(app.getHttpServer() as Server) |
| 126 | + .post(`/admin/projects/${project.id}/api-keys`) |
| 127 | + .set('Authorization', `Bearer ${accessToken}`) |
| 128 | + .send(dto) |
| 129 | + .expect(201) |
| 130 | + .then( |
| 131 | + ({ |
| 132 | + body, |
| 133 | + }: { |
| 134 | + body: { |
| 135 | + id: number; |
| 136 | + value: string; |
| 137 | + createdAt: Date; |
| 138 | + }; |
| 139 | + }) => { |
| 140 | + expect(body).toHaveProperty('id'); |
| 141 | + expect(body).toHaveProperty('value'); |
| 142 | + expect(body).toHaveProperty('createdAt'); |
| 143 | + expect(body.value).toMatch(/^[A-F0-9]{20}$/); |
| 144 | + }, |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should return 400 for invalid API key length', async () => { |
| 149 | + const dto = new CreateApiKeyRequestDto(); |
| 150 | + dto.value = 'ShortKey'; |
| 151 | + |
| 152 | + return request(app.getHttpServer() as Server) |
| 153 | + .post(`/admin/projects/${project.id}/api-keys`) |
| 154 | + .set('Authorization', `Bearer ${accessToken}`) |
| 155 | + .send(dto) |
| 156 | + .expect(400); |
| 157 | + }); |
| 158 | + |
| 159 | + it('should return 401 when unauthorized', async () => { |
| 160 | + const dto = new CreateApiKeyRequestDto(); |
| 161 | + dto.value = 'TestApiKey1234567890'; |
| 162 | + |
| 163 | + return request(app.getHttpServer() as Server) |
| 164 | + .post(`/admin/projects/${project.id}/api-keys`) |
| 165 | + .send(dto) |
| 166 | + .expect(401); |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + describe('/admin/projects/:projectId/api-keys (GET)', () => { |
| 171 | + beforeEach(async () => { |
| 172 | + const dto = new CreateApiKeyRequestDto(); |
| 173 | + dto.value = 'TestApiKeyForList123'; |
| 174 | + |
| 175 | + await request(app.getHttpServer() as Server) |
| 176 | + .post(`/admin/projects/${project.id}/api-keys`) |
| 177 | + .set('Authorization', `Bearer ${accessToken}`) |
| 178 | + .send(dto); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should find API keys by project id', async () => { |
| 182 | + return request(app.getHttpServer() as Server) |
| 183 | + .get(`/admin/projects/${project.id}/api-keys`) |
| 184 | + .set('Authorization', `Bearer ${accessToken}`) |
| 185 | + .expect(200) |
| 186 | + .then(({ body }: { body: FindApiKeysResponseDto }) => { |
| 187 | + const responseBody = body; |
| 188 | + expect(responseBody.items.length).toBeGreaterThan(0); |
| 189 | + expect(responseBody.items[0]).toHaveProperty('id'); |
| 190 | + expect(responseBody.items[0]).toHaveProperty('value'); |
| 191 | + expect(responseBody.items[0]).toHaveProperty('createdAt'); |
| 192 | + expect(responseBody.items[0]).toHaveProperty('deletedAt'); |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should return 401 when unauthorized', async () => { |
| 197 | + return request(app.getHttpServer() as Server) |
| 198 | + .get(`/admin/projects/${project.id}/api-keys`) |
| 199 | + .expect(401); |
| 200 | + }); |
| 201 | + }); |
| 202 | + |
| 203 | + describe('/admin/projects/:projectId/api-keys/:apiKeyId (DELETE)', () => { |
| 204 | + let apiKeyId: number; |
| 205 | + |
| 206 | + beforeEach(async () => { |
| 207 | + const dto = new CreateApiKeyRequestDto(); |
| 208 | + dto.value = 'TestApiKeyForDelete1'; |
| 209 | + |
| 210 | + const response = await request(app.getHttpServer() as Server) |
| 211 | + .post(`/admin/projects/${project.id}/api-keys`) |
| 212 | + .set('Authorization', `Bearer ${accessToken}`) |
| 213 | + .send(dto); |
| 214 | + |
| 215 | + apiKeyId = (response.body as { id: number }).id; |
| 216 | + }); |
| 217 | + |
| 218 | + it('should delete API key', async () => { |
| 219 | + await request(app.getHttpServer() as Server) |
| 220 | + .delete(`/admin/projects/${project.id}/api-keys/${apiKeyId}`) |
| 221 | + .set('Authorization', `Bearer ${accessToken}`) |
| 222 | + .expect(200); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should return 401 when unauthorized', async () => { |
| 226 | + return request(app.getHttpServer() as Server) |
| 227 | + .delete(`/admin/projects/${project.id}/api-keys/${apiKeyId}`) |
| 228 | + .expect(401); |
| 229 | + }); |
| 230 | + }); |
| 231 | + |
| 232 | + afterAll(async () => { |
| 233 | + const delay = (ms: number) => |
| 234 | + new Promise((resolve) => setTimeout(resolve, ms)); |
| 235 | + |
| 236 | + await delay(500); |
| 237 | + await app.close(); |
| 238 | + }); |
| 239 | +}); |
0 commit comments