-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
19 changed files
with
1,650 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,5 @@ export default async function Index() { | |
|
||
return <Users users={users} /> | ||
} | ||
|
||
export const dynamic = 'force-dynamic' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
apps/envited.ascs.digital/common/serverActions/users/deleteUserById.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import * as SUT from './deleteUserById' | ||
|
||
describe('common/serverActions/users/deleteUserById', () => { | ||
it('should return a deleted user as expected', async () => { | ||
// when ... we request a user by id | ||
// then ... it returns a user as expected | ||
const getServerSessionStub = jest.fn().mockResolvedValue({ | ||
user: { | ||
pkh: 'ISSUER_PKH', | ||
}, | ||
}) | ||
const user = { | ||
id: 'USER_PKH', | ||
issuerId: 'ISSUER_PKH', | ||
} | ||
|
||
const expected = { | ||
updatedId: 'USER_PKH', | ||
} | ||
const dbStub = jest.fn().mockResolvedValue({ | ||
getUserById: jest.fn().mockResolvedValue([user]), | ||
deleteUserById: jest.fn().mockResolvedValue([{ updatedId: 'USER_PKH' }]), | ||
}) | ||
|
||
const result = await SUT._deleteUserById({ db: dbStub, getServerSession: getServerSessionStub })('USER_ID') | ||
expect(result).toEqual(expected) | ||
}) | ||
|
||
it('should throw because of missing session', async () => { | ||
// when ... we request a user by id without a session | ||
// then ... it throws as expected | ||
const getServerSessionStub = jest.fn().mockResolvedValue(null) | ||
const user = { | ||
id: 'USER_PKH', | ||
issuerId: 'ISSUER_PKH', | ||
} | ||
const dbStub = jest.fn().mockResolvedValue({ | ||
getUserById: jest.fn().mockResolvedValue([user]), | ||
deleteUserById: jest.fn().mockResolvedValue([{ updatedId: 'USER_PKH' }]), | ||
}) | ||
|
||
await expect( | ||
SUT._deleteUserById({ db: dbStub, getServerSession: getServerSessionStub })('USER_ID'), | ||
).rejects.toThrow('Something went wrong') | ||
}) | ||
|
||
it('should throw because requester is not allowed to get this resource', async () => { | ||
// when ... we request a user by id, but the requested user is not issued by the requester OR is not their own user | ||
// then ... it throws as expected | ||
const getServerSessionStub = jest.fn().mockResolvedValue({ | ||
user: { | ||
pkh: 'ISSUER_PKH', | ||
}, | ||
}) | ||
const user = { | ||
id: 'USER_PKH', | ||
issuerId: 'FEDERATOR_PKH', | ||
} | ||
const dbStub = jest.fn().mockResolvedValue({ | ||
getUserById: jest.fn().mockResolvedValue([user]), | ||
deleteUserById: jest.fn().mockResolvedValue([{ updatedId: 'USER_PKH' }]), | ||
}) | ||
|
||
await expect( | ||
SUT._deleteUserById({ db: dbStub, getServerSession: getServerSessionStub })('USER_ID'), | ||
).rejects.toThrow('Something went wrong') | ||
}) | ||
}) |
38 changes: 38 additions & 0 deletions
38
apps/envited.ascs.digital/common/serverActions/users/deleteUserById.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { isNil } from 'ramda' | ||
import { cache } from 'react' | ||
|
||
import { getServerSession } from '../../auth' | ||
import { db } from '../../database/queries' | ||
import { Database } from '../../database/types' | ||
import { userIsIssuedByLoggedInUser } from '../../guards' | ||
import { User } from '../../types' | ||
import { Session } from '../../types/types' | ||
import { badRequestError, error, unauthorizedError } from '../../utils' | ||
|
||
export const _deleteUserById = | ||
({ db, getServerSession }: { db: Database; getServerSession: () => Promise<Session | null> }) => | ||
async (id: string): Promise<User> => { | ||
try { | ||
const session = await getServerSession() | ||
|
||
if (isNil(session)) { | ||
throw unauthorizedError() | ||
} | ||
|
||
const connection = await db() | ||
const [user] = await connection.getUserById(id) | ||
|
||
if (!userIsIssuedByLoggedInUser(user)(session)) { | ||
throw badRequestError('Incorrect permissions') | ||
} | ||
|
||
const [deletedUser] = await connection.deleteUserById(user.id) | ||
|
||
return deletedUser | ||
} catch (e) { | ||
console.log('error', e) | ||
throw error() | ||
} | ||
} | ||
|
||
export const deleteUserById = cache(_deleteUserById({ db, getServerSession })) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
apps/envited.ascs.digital/drizzle/development/0006_complete_caretaker.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE "user" ADD COLUMN "is_active" boolean DEFAULT true; |
Oops, something went wrong.