Skip to content

Commit

Permalink
add more get-users tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tryb3l committed Sep 3, 2024
1 parent b3f72d5 commit 7979ff2
Showing 1 changed file with 80 additions and 3 deletions.
83 changes: 80 additions & 3 deletions test/users/get-users.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ t.beforeEach(async (t) => {
t.context.refreshToken = refreshToken
})

t.test('GET /users 200', async (t) => {
t.test('GET /users 200 - List users', async (t) => {
// Arrange
const { app, accessToken, refreshToken } = t.context

Expand All @@ -30,9 +30,85 @@ t.test('GET /users 200', async (t) => {
// Assert
t.equal(response.statusCode, 200)
t.type(response.json(), 'object')
t.ok(Array.isArray(response.json().data))
t.type(response.json().totalCount, 'number')
})

t.test('GET /users 401', async (t) => {
t.test('GET /users 200 - List users with pagination', async (t) => {
// Arrange
const { app, accessToken, refreshToken } = t.context

// Act
const response = await app.inject({
method: 'GET',
url: '/users?skip=0&limit=5',
headers: {
contentType: 'application/json',
},
cookies: {
accessToken: accessToken,
refreshToken: refreshToken,
},
})

// Assert
t.equal(response.statusCode, 200)
t.type(response.json(), 'object')
t.ok(Array.isArray(response.json().data))
t.type(response.json().totalCount, 'number')
t.equal(response.json().data.length, 5)
})

t.test('GET /users 200 - List users with username filter', async (t) => {
// Arrange
const { app, accessToken, refreshToken } = t.context
const username = 'testuser'

// Act
const response = await app.inject({
method: 'GET',
url: `/users?username=${username}`,
headers: {
contentType: 'application/json',
},
cookies: {
accessToken: accessToken,
refreshToken: refreshToken,
},
})

// Assert
t.equal(response.statusCode, 200)
t.type(response.json(), 'object')
t.ok(Array.isArray(response.json().data))
t.type(response.json().totalCount, 'number')
t.ok(response.json().data.every((user) => user.username === username))
})

t.test('GET /users 400 - Invalid skip and limit', async (t) => {
// Arrange
const { app, accessToken, refreshToken } = t.context

// Act
const response = await app.inject({
method: 'GET',
url: '/users?skip=-1&limit=-5',
headers: {
contentType: 'application/json',
},
cookies: {
accessToken: accessToken,
refreshToken: refreshToken,
},
})

// Assert
t.equal(response.statusCode, 400)
t.type(response.json(), 'object')
t.equal(response.json().message, 'Skip and limit must be non-negative integers')
})

t.test('GET /users 401 - Unauthorized', async (t) => {
// Arrange
const { app } = t.context

Expand All @@ -50,7 +126,7 @@ t.test('GET /users 401', async (t) => {
t.type(response.json(), 'object')
})

t.test('GET /users 404', async (t) => {
t.test('GET /users 404 - User not found', async (t) => {
// Arrange
const { app, accessToken, refreshToken } = t.context

Expand All @@ -70,4 +146,5 @@ t.test('GET /users 404', async (t) => {
// Assert
t.equal(response.statusCode, 404)
t.type(response.json(), 'object')
t.equal(response.json().error, 'User not found')
})

0 comments on commit 7979ff2

Please sign in to comment.