Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): user from address #99

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions backend/src/routes/getUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { eq } from 'drizzle-orm/pg-core/expressions'
import type { FastifyInstance } from 'fastify'

import { registration } from '@/db/schema'

import { addressRegex } from '.'

export function getUserRoute(fastify: FastifyInstance) {
fastify.get(
'/get_user',

async (request, reply) => {
const { address } = request.query as { address?: string }

if (!address) {
return reply.status(400).send({ message: 'Address is required' })
}

// Validate address format
if (!addressRegex.test(address)) {
return reply.status(400).send({ message: 'Invalid address format' })
}

try {
const user = await fastify.db
.select({ user: registration.nickname })
.from(registration)
.where(eq(registration.contract_address, address))
.limit(1)
.execute()

if (!user.length) {
return reply.status(404).send({ message: 'User not found.' })
}

return reply.send(user[0])
} catch (error) {
console.error(error)
return reply.status(500).send({ message: 'Internal server error' })
}
},
)
}
2 changes: 2 additions & 0 deletions backend/src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { getHistoricalBalanceRoute } from './getHistoricalBalance'
import { getLimitRoute } from './getLimit'
import { getOtp } from './getOtp'
import { getTransactionHistory } from './getTransactionHistory'
import { getUserRoute } from './getUser'
import { verifyOtp } from './verifyOtp'

export const addressRegex = /^0x0[0-9a-fA-F]{63}$/
Expand All @@ -28,6 +29,7 @@ export function declareRoutes(fastify: FastifyInstance, deployer: Account, twili
getClaimRoute(fastify)
getLimitRoute(fastify)
getExecuteFromOutsideRoute(fastify, deployer)
getUserRoute(fastify)
}

function getStatusRoute(fastify: FastifyInstance) {
Expand Down
83 changes: 83 additions & 0 deletions backend/test/getUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { PostgreSqlContainer, type StartedPostgreSqlContainer } from '@testcontainers/postgresql'
import type { FastifyInstance } from 'fastify'
import { afterAll, beforeAll, describe, expect, test } from 'vitest'

import { buildApp } from '@/app'

import * as schema from '../src/db/schema'

describe('GET /get_user route', () => {
let container: StartedPostgreSqlContainer
let app: FastifyInstance
const testAddress = '0x064a24243f2aabae8d2148fa878276e6e6e452e3941b417f3c33b1649ea83e11'
const unknownAddress = '0x064a24243f2aabae8d2148fa878276e6e6e452e3941b417f3c33b1649ea83e12'
const testName = 'Jean Dupont'

beforeAll(async () => {
container = await new PostgreSqlContainer().start()
const connectionUri = container.getConnectionUri()
app = await buildApp({
database: {
connectionString: connectionUri,
},
app: {
port: 8080,
},
})

await app.ready()
await app.db.insert(schema.registration).values([
{
phone_number: '+33611223344',
contract_address: testAddress,
nickname: testName,
},
])
})

afterAll(async () => {
await app.close()
await container.stop()
})

test('should return the name for a valid registered address', async () => {
const response = await app.inject({
method: 'GET',
url: `/get_user?address=${testAddress}`,
})

expect(response.statusCode).toBe(200)
expect(response.json()).toHaveProperty('user', testName)
})

test('should return an error for an unknown address', async () => {
const response = await app.inject({
method: 'GET',
url: `/get_user?address=${unknownAddress}`,
})

expect(response.statusCode).toBe(404)
expect(response.json()).toHaveProperty('message', 'User not found.')
})

test('should return error, invalid address format', async () => {
const invalidAddress = '0x0'
const response = await app.inject({
method: 'GET',
url: `/get_user?address=${invalidAddress}`,
})

expect(response.statusCode).toBe(400)
expect(response.json()).toHaveProperty('message', 'Invalid address format')
})

test('should return error, no address provided', async () => {
const response = await app.inject({
method: 'GET',
url: '/get_user',
})

expect(response.statusCode).toBe(400)
expect(response.json()).toHaveProperty('message', 'Address is required')
})
})
Loading