|
| 1 | +import { faker } from "@faker-js/faker"; |
| 2 | +import nock from "nock"; |
| 3 | +import { container } from "tsyringe"; |
| 4 | + |
| 5 | +import { app } from "@src/app"; |
| 6 | +import { ApiKeyAuthService } from "@src/auth/services/api-key/api-key-auth.service"; |
| 7 | +import { UserWalletRepository } from "@src/billing/repositories"; |
| 8 | +import { UserRepository } from "@src/user/repositories"; |
| 9 | +import { apiNodeUrl } from "@src/utils/constants"; |
| 10 | + |
| 11 | +import { ApiKeySeeder } from "@test/seeders/api-key.seeder"; |
| 12 | +import { UserSeeder } from "@test/seeders/user.seeder"; |
| 13 | +import { UserWalletSeeder } from "@test/seeders/user-wallet.seeder"; |
| 14 | +import { DbTestingService } from "@test/services/db-testing.service"; |
| 15 | + |
| 16 | +jest.setTimeout(20000); |
| 17 | + |
| 18 | +xdescribe("Bids API", () => { |
| 19 | + const userRepository = container.resolve(UserRepository); |
| 20 | + const apiKeyAuthService = container.resolve(ApiKeyAuthService); |
| 21 | + const userWalletRepository = container.resolve(UserWalletRepository); |
| 22 | + const dbService = container.resolve(DbTestingService); |
| 23 | + |
| 24 | + async function mockUserAndApiKey() { |
| 25 | + const userId = faker.string.uuid(); |
| 26 | + const apiKey = faker.word.noun(); |
| 27 | + |
| 28 | + const user = UserSeeder.create({ userId }); |
| 29 | + const apiKeyForUser = ApiKeySeeder.create({ userId }); |
| 30 | + const walletsForUser = [UserWalletSeeder.create({ userId })]; |
| 31 | + |
| 32 | + jest.spyOn(userRepository, "findByUserId").mockImplementation(async (id: string) => { |
| 33 | + return Promise.resolve(id === userId ? { |
| 34 | + ...user, |
| 35 | + trial: false, |
| 36 | + userWallets: { isTrialing: false } |
| 37 | + } : undefined); |
| 38 | + }); |
| 39 | + |
| 40 | + jest.spyOn(apiKeyAuthService, "getAndValidateApiKeyFromHeader").mockImplementation(async (key: string) => { |
| 41 | + return Promise.resolve(key === apiKey ? apiKeyForUser : undefined); |
| 42 | + }); |
| 43 | + |
| 44 | + jest.spyOn(userWalletRepository, "findByUserId").mockImplementation(async (id: string) => { |
| 45 | + return Promise.resolve(id === userId ? walletsForUser : undefined); |
| 46 | + }); |
| 47 | + |
| 48 | + return { user, apiKey, walletsForUser } |
| 49 | + } |
| 50 | + |
| 51 | + afterEach(async () => { |
| 52 | + await dbService.cleanAll(); |
| 53 | + }); |
| 54 | + |
| 55 | + afterAll(() => { |
| 56 | + jest.restoreAllMocks(); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("GET /v1/bids", () => { |
| 60 | + it("returns 401 for an unauthenticated request", async () => { |
| 61 | + const response = await app.request("/v1/bids/1234", { |
| 62 | + method: "GET", |
| 63 | + headers: new Headers({ "Content-Type": "application/json" }) |
| 64 | + }); |
| 65 | + |
| 66 | + expect(response.status).toBe(401); |
| 67 | + }); |
| 68 | + |
| 69 | + it("returns 404 if no dseq set", async () => { |
| 70 | + const { apiKey } = await mockUserAndApiKey(); |
| 71 | + const response = await app.request("/v1/buds", { |
| 72 | + method: "GET", |
| 73 | + headers: new Headers({ "Content-Type": "application/json", 'x-api-key': apiKey }) |
| 74 | + }); |
| 75 | + |
| 76 | + expect(response.status).toBe(404); |
| 77 | + }); |
| 78 | + |
| 79 | + it("lists bids related to user and dseq", async () => { |
| 80 | + const { apiKey, walletsForUser } = await mockUserAndApiKey(); |
| 81 | + const dseq = '1234'; |
| 82 | + |
| 83 | + nock(apiNodeUrl).get(`/akash/market/v1beta4/bids/list?filters.owner=${walletsForUser[0].address}&filters.dseq=${dseq}`).reply(200, { |
| 84 | + bids: [{ |
| 85 | + bid: 'fake-bid', |
| 86 | + escrow_account: 'fake-escrow-account', |
| 87 | + }] |
| 88 | + }); |
| 89 | + |
| 90 | + const response = await app.request(`/v1/bids/${dseq}`, { |
| 91 | + method: "GET", |
| 92 | + headers: new Headers({ "Content-Type": "application/json", 'x-api-key': apiKey }) |
| 93 | + }); |
| 94 | + |
| 95 | + expect(response.status).toBe(200); |
| 96 | + const result = await response.json(); |
| 97 | + expect(result.data).toEqual([{ |
| 98 | + bid: 'fake-bid', |
| 99 | + escrow_account: 'fake-escrow-account', |
| 100 | + }]); |
| 101 | + }); |
| 102 | + }); |
| 103 | +}); |
0 commit comments