Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.

Commit 1ab6fd7

Browse files
authored
Fix Admin API Get (#625)
Fix a bug so that if you GET a user on the Admin API that has no associated addresses, you now get a 200 with a payload with an empty address array rather than a 404 - Not Found.
1 parent b03fa01 commit 1ab6fd7

File tree

7 files changed

+69
-7
lines changed

7 files changed

+69
-7
lines changed

src/data-access/users.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,27 @@ import knex from '../db/knex'
77
import { Account, Address, AddressInformation } from '../types/database'
88
import logger from '../utils/logger'
99

10+
/**
11+
* Checks if a given PayID exists in the account table in the PayID database.
12+
*
13+
* @param payId - The PayID to insert in the account table.
14+
*
15+
* @returns A boolean indicating whether the PayID exists.
16+
*
17+
* Note: This could actually be done in getAllAddressInfoFromDatabase,
18+
* if we changed the return type a bit, which would let us always do a single database call,
19+
* instead of having to call this as a follow-up check, but it's probably cleaner to
20+
* let them have separate concerns until performance becomes an issue.
21+
*/
22+
// TODO:(hbergren): Type payId better
23+
export async function checkUserExistence(payId: string): Promise<boolean> {
24+
const result = await knex.select(
25+
knex.raw('exists(select 1 from account where pay_id = ?)', payId),
26+
)
27+
28+
return result[0].exists
29+
}
30+
1031
/**
1132
* Inserts a new user/PayID into the account table in the PayID database.
1233
*
@@ -174,6 +195,10 @@ async function insertAddresses(
174195
addresses: readonly DatabaseAddress[],
175196
transaction: Transaction,
176197
): Promise<readonly AddressInformation[]> {
198+
if (addresses.length === 0) {
199+
return []
200+
}
201+
177202
// TODO:(hbergren) Verify that the number of inserted addresses matches the input address array length?
178203
return knex
179204
.insert(addresses)

src/db/seed/seeded_values_for_testing.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ INSERT INTO account(id, pay_id) VALUES
77
('223ece9c-2a15-48e1-9df6-d9ac77c5db90', 'bob$127.0.0.1'),
88
('ec06236e-d134-4a7b-b69e-0606fb54b67b', 'alice$xpring.money'),
99
('69b0d20a-cdef-4bb9-adf9-2109979a12af', 'bob$xpring.money'),
10-
('b253bed2-79ce-45d0-bbdd-96867aa85fd5', 'zebra$xpring.money');
10+
('b253bed2-79ce-45d0-bbdd-96867aa85fd5', 'zebra$xpring.money'),
11+
('8a75f884-ab16-40c4-a82a-aca454dad6b2', 'empty$xpring.money');
1112

1213
INSERT INTO address(account_id, payment_network, environment, details) VALUES
1314
('232370e9-045e-4269-96ec-5a79091d65ff', 'XRPL', 'MAINNET', '{"address": "rw2ciyaNshpHe7bCHo4bRWq6pqqynnWKQg", "tag": "67298042"}'),

src/middlewares/users.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
replaceUser,
88
removeUser,
99
replaceUserPayId,
10+
checkUserExistence,
1011
} from '../data-access/users'
1112
import {
1213
LookupError,
@@ -45,16 +46,16 @@ export async function getUser(
4546
}
4647

4748
// TODO:(hbergren) Does not work for multiple accounts
48-
const addresses = await getAllAddressInfoFromDatabase(payId)
49-
50-
// TODO:(hbergren) Should it be possible to have a PayID with no addresses?
51-
if (addresses.length === 0) {
49+
const doesUserExist = await checkUserExistence(payId)
50+
if (!doesUserExist) {
5251
throw new LookupError(
5352
`No information could be found for the PayID ${payId}.`,
5453
LookupErrorType.Unknown,
5554
)
5655
}
5756

57+
const addresses = await getAllAddressInfoFromDatabase(payId)
58+
5859
res.locals.response = {
5960
payId,
6061
addresses,

test/integration/data-access/reports.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('Data Access - getPayIdCounts()', function (): void {
4646

4747
it('getPayIdCount - Returns a count of PayIDs', async function () {
4848
const payIdCount = await getPayIdCount()
49-
const expectedPayIdCount = 5
49+
const expectedPayIdCount = 6
5050

5151
assert.strictEqual(payIdCount, expectedPayIdCount)
5252
})

test/integration/e2e/admin-api/getUsers.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ describe('E2E - adminApiRouter - GET /users', function (): void {
4343
.expect(HttpStatus.OK, expectedResponse, done)
4444
})
4545

46+
it('Returns a 200 and correct information for a user known to exist without any addresses', function (done): void {
47+
// GIVEN a PayID known to resolve to an account on the PayID service
48+
const payId = 'empty$xpring.money'
49+
const expectedResponse = {
50+
payId: 'empty$xpring.money',
51+
addresses: [],
52+
}
53+
54+
// WHEN we make a GET request to /users/ with that PayID as our user
55+
request(app.adminApiExpress)
56+
.get(`/users/${payId}`)
57+
.set('PayID-API-Version', payIdApiVersion)
58+
.expect('Content-Type', /json/u)
59+
// THEN We expect back a 200 - OK, with the account information
60+
.expect(HttpStatus.OK, expectedResponse, done)
61+
})
62+
4663
it('Returns a 200 and correct information for a user known to exist, when we use an uppercase PayID', function (done): void {
4764
// GIVEN a PayID known to resolve to an account on the PayID service
4865
const payId = 'ALICE$XPRING.MONEY'

test/integration/e2e/admin-api/metrics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ describe('E2E - adminApiRouter - GET /metrics', function (): void {
100100

101101
// We create 8 PayIDs in the tests before this one,
102102
// and start with 5 seeded PayIDs, for a total of 13.
103-
await assertMetrics(/actual_payid_count\{org="127.0.0.1"\} 13/u)
103+
await assertMetrics(/actual_payid_count\{org="127.0.0.1"\} 14/u)
104104
})
105105

106106
/**

test/integration/e2e/admin-api/putUsers.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,24 @@ describe('E2E - adminApiRouter - PUT /users', function (): void {
7171
.expect(HttpStatus.OK, updatedInformation, done)
7272
})
7373

74+
it('Returns a 200 and updated user payload when removing all addresses', function (done): void {
75+
// GIVEN a PayID known to resolve to an account on the PayID service
76+
const payId = 'empty$xpring.money'
77+
const updatedInformation = {
78+
payId: 'empty$xpring.money',
79+
addresses: [],
80+
}
81+
82+
// WHEN we make a PUT request to /users/ with the new information to update
83+
request(app.adminApiExpress)
84+
.put(`/users/${payId}`)
85+
.set('PayID-API-Version', payIdApiVersion)
86+
.send(updatedInformation)
87+
.expect('Content-Type', /json/u)
88+
// THEN we expect back a 200-OK, with the updated user information
89+
.expect(HttpStatus.OK, updatedInformation, done)
90+
})
91+
7492
it('Returns a 201 and inserted user payload for a Admin API PUT creating a new user', function (done): void {
7593
// GIVEN a PayID known to not exist on the PayID service
7694
const payId = 'notjohndoe$xpring.money'

0 commit comments

Comments
 (0)