|
1 | | -import { AllowService } from "./../../services/allow.service"; |
2 | | -import { OptimizeService } from "./../../services/optimize.service"; |
3 | 1 | import { Test, TestingModule } from "@nestjs/testing"; |
4 | 2 | import { OptimizeController } from "./optimize.controller"; |
| 3 | +import { OptimizeService } from "../../services/optimize.service"; |
| 4 | +import { AllowService } from "../../services/allow.service"; |
5 | 5 | import { ImgLoaderService } from "../../services/img-loader.service"; |
| 6 | +import { BadRequestException } from "@nestjs/common"; |
| 7 | +import { Response } from "express"; |
6 | 8 |
|
7 | 9 | describe("OptimizeController", () => { |
8 | 10 | let optimizeController: OptimizeController; |
| 11 | + let allowService: AllowService; |
9 | 12 |
|
10 | 13 | beforeEach(async () => { |
| 14 | + const mockAllowService = { |
| 15 | + isAllowedSources: jest.fn(), |
| 16 | + isAllowedSizes: jest.fn(), |
| 17 | + }; |
| 18 | + |
| 19 | + const mockImgLoaderService = { |
| 20 | + getImage: jest.fn(), |
| 21 | + }; |
| 22 | + |
| 23 | + const mockOptimizeService = { |
| 24 | + getOptimizedImage: jest.fn(), |
| 25 | + }; |
| 26 | + |
11 | 27 | const app: TestingModule = await Test.createTestingModule({ |
12 | 28 | controllers: [OptimizeController], |
13 | | - providers: [OptimizeService, AllowService, ImgLoaderService], |
| 29 | + providers: [ |
| 30 | + { |
| 31 | + provide: OptimizeService, |
| 32 | + useValue: mockOptimizeService, |
| 33 | + }, |
| 34 | + { |
| 35 | + provide: AllowService, |
| 36 | + useValue: mockAllowService, |
| 37 | + }, |
| 38 | + { |
| 39 | + provide: ImgLoaderService, |
| 40 | + useValue: mockImgLoaderService, |
| 41 | + }, |
| 42 | + ], |
14 | 43 | }).compile(); |
15 | 44 |
|
16 | 45 | optimizeController = app.get<OptimizeController>(OptimizeController); |
| 46 | + allowService = app.get<AllowService>(AllowService); |
17 | 47 | }); |
18 | 48 |
|
19 | | - describe("root", () => { |
20 | | - it('should return "Hello World!"', () => { |
21 | | - // write tests later |
22 | | - expect(optimizeController).toBeInstanceOf(OptimizeController); |
23 | | - expect("Hello World!").toBe("Hello World!"); |
| 49 | + describe("getPreview - negative scenarios for query parameters", () => { |
| 50 | + let mockResponse: Partial<Response>; |
| 51 | + |
| 52 | + beforeEach(() => { |
| 53 | + mockResponse = { |
| 54 | + setHeader: jest.fn().mockReturnThis(), |
| 55 | + status: jest.fn().mockReturnThis(), |
| 56 | + send: jest.fn(), |
| 57 | + }; |
| 58 | + }); |
| 59 | + |
| 60 | + it("should throw BadRequestException if src parameter is missing", async () => { |
| 61 | + await expect( |
| 62 | + optimizeController.getPreview( |
| 63 | + "", |
| 64 | + "1920", |
| 65 | + "jpeg", |
| 66 | + "80", |
| 67 | + mockResponse as Response, |
| 68 | + ), |
| 69 | + ).rejects.toThrow( |
| 70 | + new BadRequestException("Parameter 'src' is required."), |
| 71 | + ); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should throw BadRequestException if size parameter is missing", async () => { |
| 75 | + await expect( |
| 76 | + optimizeController.getPreview( |
| 77 | + "https://example.com/image.jpg", |
| 78 | + "", |
| 79 | + "jpeg", |
| 80 | + "80", |
| 81 | + mockResponse as Response, |
| 82 | + ), |
| 83 | + ).rejects.toThrow( |
| 84 | + new BadRequestException("Parameter 'size' is required."), |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + it("should throw BadRequestException if size is zero", async () => { |
| 89 | + await expect( |
| 90 | + optimizeController.getPreview( |
| 91 | + "https://example.com/image.jpg", |
| 92 | + "0", |
| 93 | + "jpeg", |
| 94 | + "80", |
| 95 | + mockResponse as Response, |
| 96 | + ), |
| 97 | + ).rejects.toThrow( |
| 98 | + new BadRequestException( |
| 99 | + "Parameter 'size' should be a positive integer.", |
| 100 | + ), |
| 101 | + ); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should throw BadRequestException if size is negative", async () => { |
| 105 | + await expect( |
| 106 | + optimizeController.getPreview( |
| 107 | + "https://example.com/image.jpg", |
| 108 | + "-100", |
| 109 | + "jpeg", |
| 110 | + "80", |
| 111 | + mockResponse as Response, |
| 112 | + ), |
| 113 | + ).rejects.toThrow( |
| 114 | + new BadRequestException( |
| 115 | + "Parameter 'size' should be a positive integer.", |
| 116 | + ), |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + it("should throw BadRequestException if format parameter is missing", async () => { |
| 121 | + await expect( |
| 122 | + optimizeController.getPreview( |
| 123 | + "https://example.com/image.jpg", |
| 124 | + "1920", |
| 125 | + "", |
| 126 | + "80", |
| 127 | + mockResponse as Response, |
| 128 | + ), |
| 129 | + ).rejects.toThrow( |
| 130 | + new BadRequestException("Parameter 'format' is required."), |
| 131 | + ); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should throw BadRequestException if format is not supported", async () => { |
| 135 | + await expect( |
| 136 | + optimizeController.getPreview( |
| 137 | + "https://example.com/image.jpg", |
| 138 | + "1920", |
| 139 | + "gif", |
| 140 | + "80", |
| 141 | + mockResponse as Response, |
| 142 | + ), |
| 143 | + ).rejects.toThrow( |
| 144 | + new BadRequestException("Parameter 'format' is not supported."), |
| 145 | + ); |
| 146 | + }); |
| 147 | + |
| 148 | + it("should throw BadRequestException if quality is not a number", async () => { |
| 149 | + await expect( |
| 150 | + optimizeController.getPreview( |
| 151 | + "https://example.com/image.jpg", |
| 152 | + "1920", |
| 153 | + "jpeg", |
| 154 | + "abc", |
| 155 | + mockResponse as Response, |
| 156 | + ), |
| 157 | + ).rejects.toThrow( |
| 158 | + new BadRequestException("Parameter 'quality' is not a number."), |
| 159 | + ); |
| 160 | + }); |
| 161 | + |
| 162 | + it("should throw BadRequestException if quality is less than 1", async () => { |
| 163 | + await expect( |
| 164 | + optimizeController.getPreview( |
| 165 | + "https://example.com/image.jpg", |
| 166 | + "1920", |
| 167 | + "jpeg", |
| 168 | + "0", |
| 169 | + mockResponse as Response, |
| 170 | + ), |
| 171 | + ).rejects.toThrow( |
| 172 | + new BadRequestException( |
| 173 | + "Parameter 'quality' must be in range 1-100.", |
| 174 | + ), |
| 175 | + ); |
| 176 | + }); |
| 177 | + |
| 178 | + it("should throw BadRequestException if quality is greater than 100", async () => { |
| 179 | + await expect( |
| 180 | + optimizeController.getPreview( |
| 181 | + "https://example.com/image.jpg", |
| 182 | + "1920", |
| 183 | + "jpeg", |
| 184 | + "101", |
| 185 | + mockResponse as Response, |
| 186 | + ), |
| 187 | + ).rejects.toThrow( |
| 188 | + new BadRequestException( |
| 189 | + "Parameter 'quality' must be in range 1-100.", |
| 190 | + ), |
| 191 | + ); |
| 192 | + }); |
| 193 | + |
| 194 | + it("should throw BadRequestException if src is not allowed", async () => { |
| 195 | + (allowService.isAllowedSources as jest.Mock).mockReturnValue(false); |
| 196 | + |
| 197 | + await expect( |
| 198 | + optimizeController.getPreview( |
| 199 | + "https://malicious.com/image.jpg", |
| 200 | + "1920", |
| 201 | + "jpeg", |
| 202 | + "80", |
| 203 | + mockResponse as Response, |
| 204 | + ), |
| 205 | + ).rejects.toThrow( |
| 206 | + new BadRequestException( |
| 207 | + "Parameter 'src' has an not allowed value.", |
| 208 | + ), |
| 209 | + ); |
| 210 | + }); |
| 211 | + |
| 212 | + it("should throw BadRequestException if size is not allowed", async () => { |
| 213 | + (allowService.isAllowedSources as jest.Mock).mockReturnValue(true); |
| 214 | + (allowService.isAllowedSizes as jest.Mock).mockReturnValue(false); |
| 215 | + |
| 216 | + await expect( |
| 217 | + optimizeController.getPreview( |
| 218 | + "https://example.com/image.jpg", |
| 219 | + "5000", |
| 220 | + "jpeg", |
| 221 | + "80", |
| 222 | + mockResponse as Response, |
| 223 | + ), |
| 224 | + ).rejects.toThrow( |
| 225 | + new BadRequestException( |
| 226 | + "Parameter 'size' has an not allowed value.", |
| 227 | + ), |
| 228 | + ); |
24 | 229 | }); |
25 | 230 | }); |
26 | 231 | }); |
0 commit comments