|
| 1 | +import { Given, When, Then } from '@cucumber/cucumber'; |
| 2 | +import { strict as assert } from 'assert'; |
| 3 | +import Zenko from '../../world/Zenko'; |
| 4 | +import { IdentityEnum } from 'cli-testing'; |
| 5 | + |
| 6 | +interface LocationUsage { |
| 7 | + bytesTotal: number; |
| 8 | + objectsTotal: number; |
| 9 | +} |
| 10 | + |
| 11 | +interface ReportingUsageResponse { |
| 12 | + isTruncated: boolean; |
| 13 | + marker: string | null; |
| 14 | + accounts: Record<string, Record<string, LocationUsage>>; |
| 15 | +} |
| 16 | + |
| 17 | +Given('an identity with the {string} keycloak persona', function (this: Zenko, persona: string) { |
| 18 | + const username = (this.parameters as Record<string, string>)[persona] || persona; |
| 19 | + this.addToSaved('keycloakPersona', username); |
| 20 | +}); |
| 21 | + |
| 22 | +async function fetchStorageUsageReport(world: Zenko) { |
| 23 | + const persona = world.getSaved<string>('keycloakPersona'); |
| 24 | + const result = await world.managementAPIRequest( |
| 25 | + 'GET', |
| 26 | + `/instance/${world.parameters.InstanceID}/reporting/usage`, |
| 27 | + {}, |
| 28 | + {}, |
| 29 | + persona, |
| 30 | + ); |
| 31 | + world.addToSaved('lastHttpResponse', result); |
| 32 | + return result; |
| 33 | +} |
| 34 | + |
| 35 | +When('the user tries to retrieve the storage usage report', async function (this: Zenko) { |
| 36 | + await fetchStorageUsageReport(this); |
| 37 | +}); |
| 38 | + |
| 39 | +When('the user retrieves the storage usage report', async function (this: Zenko) { |
| 40 | + const result = await fetchStorageUsageReport(this); |
| 41 | + assert.strictEqual(result.statusCode, 200, |
| 42 | + `Expected status 200 but got ${result.statusCode}`); |
| 43 | +}); |
| 44 | + |
| 45 | +Then('the storage usage report response has a valid structure', function (this: Zenko) { |
| 46 | + const response = this.getSaved<{ statusCode: number; data: ReportingUsageResponse }>( |
| 47 | + 'lastHttpResponse'); |
| 48 | + const data = response.data; |
| 49 | + assert.strictEqual(typeof data.isTruncated, 'boolean', |
| 50 | + 'isTruncated should be a boolean'); |
| 51 | + assert.ok(typeof data.marker === 'string' || data.marker === null, |
| 52 | + 'marker should be a string or null'); |
| 53 | + assert.strictEqual(typeof data.accounts, 'object', |
| 54 | + 'accounts should be an object'); |
| 55 | +}); |
| 56 | + |
| 57 | +Then('the storage usage report contains the additional accounts', async function (this: Zenko) { |
| 58 | + const response = this.getSaved<{ statusCode: number; data: ReportingUsageResponse }>( |
| 59 | + 'lastHttpResponse'); |
| 60 | + const accountNames = this.getSavedIdentities() |
| 61 | + .filter(id => id.identityType === IdentityEnum.ACCOUNT) |
| 62 | + .map(id => id.accountName); |
| 63 | + for (const accountName of accountNames) { |
| 64 | + assert.ok(accountName in response.data.accounts, |
| 65 | + `Account ${accountName} should be present in the report`); |
| 66 | + } |
| 67 | +}); |
| 68 | + |
| 69 | +Then('the report contains the test account with location {string}', async function (this: Zenko, locationName: string) { |
| 70 | + const response = this.getSaved<{ statusCode: number; data: ReportingUsageResponse }>( |
| 71 | + 'lastHttpResponse'); |
| 72 | + const accountName = this.getSaved<string>('accountName'); |
| 73 | + |
| 74 | + assert.ok(accountName in response.data.accounts, |
| 75 | + `Account ${accountName} should be present in the report`); |
| 76 | + |
| 77 | + const accountData = response.data.accounts[accountName]; |
| 78 | + assert.ok(locationName in accountData, |
| 79 | + `Location ${locationName} should be present for account ${accountName}`); |
| 80 | + |
| 81 | + this.addToSaved('reportedLocationUsage', accountData[locationName]); |
| 82 | +}); |
| 83 | + |
| 84 | +Then('the report shows {int} objects and {int} bytes', function (this: Zenko, objects: number, bytes: number) { |
| 85 | + const usage = this.getSaved<LocationUsage>('reportedLocationUsage'); |
| 86 | + assert.strictEqual(usage.objectsTotal, objects, |
| 87 | + `Expected ${objects} objects but got ${usage.objectsTotal}`); |
| 88 | + assert.strictEqual(usage.bytesTotal, bytes, |
| 89 | + `Expected ${bytes} bytes but got ${usage.bytesTotal}`); |
| 90 | +}); |
0 commit comments