-
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.
Merge branch 'develop' into 19_overview_users_page_principal
- Loading branch information
Showing
19 changed files
with
293 additions
and
17 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
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 |
---|---|---|
@@ -1,7 +1,15 @@ | ||
import { pathOr } from 'ramda' | ||
import { equals, pathOr, prop } from 'ramda' | ||
|
||
import { Session, User } from '../../common/types/types' | ||
|
||
export const extractIdFromCredential = pathOr('', ['credentialSubject', 'id']) | ||
|
||
export const extractIssuerIdFromCredential = pathOr('', ['issuer', 'id']) | ||
|
||
export const extractTypeFromCredential = pathOr('', ['credentialSubject', 'type']) | ||
|
||
export const isOwnUser = (user: User) => (session: Session) => | ||
equals(prop('id')(user))(pathOr('', ['user', 'pkh'])(session)) | ||
|
||
export const userIsIssuedByLoggedInUser = (user: User) => (session: Session) => | ||
equals(prop('issuerId')(user))(pathOr('', ['user', 'pkh'])(session)) |
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 |
---|---|---|
@@ -1,12 +1,26 @@ | ||
import { getServerSession } from '../../common/auth' | ||
import { getUserById } from '../../common/server' | ||
import { Dashboard } from '../../modules/Dashboard' | ||
import { User } from '../../modules/User' | ||
|
||
export default async function Index() { | ||
const session = await getServerSession() | ||
const user = await getUserById('did:pkh:tz:tz1SfdVU1mor3Sgej3FmmwMH4HM1EjTzqqeE') | ||
|
||
return session ? ( | ||
<Dashboard id={session?.user?.id} address={session?.user?.pkh} role={session?.user?.role} /> | ||
) : ( | ||
<div>Not logged in</div> | ||
return ( | ||
<> | ||
<main> | ||
<div className="mx-auto max-w-6xl"> | ||
{session ? ( | ||
<> | ||
<User {...user} /> | ||
<Dashboard id={session?.user?.id} address={session?.user?.pkh} role={session?.user?.role} /> | ||
</> | ||
) : ( | ||
<div>Not logged in</div> | ||
)} | ||
</div> | ||
</main> | ||
</> | ||
) | ||
} |
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,11 @@ | ||
'use client' | ||
|
||
export default function Error() { | ||
return ( | ||
<html> | ||
<body> | ||
<h2>Ah oh, Something went wrong!</h2> | ||
</body> | ||
</html> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { getServerSession as NAGetServerSession, NextAuthOptions } from 'next-auth' | ||
|
||
import { Session } from '../types/types' | ||
import { authOptions } from './auth' | ||
|
||
export const _getServerSession = (NAGetServerSession: any) => (authOptions: NextAuthOptions) => () => | ||
NAGetServerSession(authOptions) | ||
export const _getServerSession = | ||
(NAGetServerSession: (arg0: NextAuthOptions) => Promise<Session | null>) => (authOptions: NextAuthOptions) => () => | ||
NAGetServerSession(authOptions) | ||
|
||
export const getServerSession = _getServerSession(NAGetServerSession)(authOptions) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { getUserById } from './server' |
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,94 @@ | ||
import * as SUT from './server' | ||
|
||
describe('common/server/server', () => { | ||
it('should return a user as expected', async () => { | ||
// when ... we request a user by id | ||
// then ... it returns a user as expected | ||
|
||
const getServerSessionStub = jest.fn().mockImplementation(() => | ||
Promise.resolve({ | ||
user: { | ||
pkh: 'USER_PKH', | ||
}, | ||
}), | ||
) | ||
const user = { | ||
id: 'USER_PKH', | ||
name: 'USER_NAME', | ||
email: 'USER_EMAIL', | ||
vatId: 'USER_VAT_ID', | ||
privacyPolicyAccepted: 'USER_PRIVACY_POLICY_ACCEPTED', | ||
articlesOfAssociationAccepted: 'USER_ARTICLES_OF_ASSOCIATION_ACCEPTED', | ||
contributionRulesAccepted: 'USER_CONTRIBUTION_RULES_ACCEPTED', | ||
isAscsMember: true, | ||
isEnvitedMember: true, | ||
} | ||
const dbStub = jest.fn().mockImplementation(() => | ||
Promise.resolve({ | ||
getUserById: () => Promise.resolve([user]), | ||
}), | ||
) | ||
|
||
const result = await SUT._getUserById({ db: dbStub, getServerSession: getServerSessionStub })('USER_ID') | ||
expect(result).toEqual(user) | ||
}) | ||
|
||
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().mockImplementation(() => Promise.resolve(null)) | ||
const user = { | ||
id: 'USER_PKH', | ||
name: 'USER_NAME', | ||
email: 'USER_EMAIL', | ||
vatId: 'USER_VAT_ID', | ||
privacyPolicyAccepted: 'USER_PRIVACY_POLICY_ACCEPTED', | ||
articlesOfAssociationAccepted: 'USER_ARTICLES_OF_ASSOCIATION_ACCEPTED', | ||
contributionRulesAccepted: 'USER_CONTRIBUTION_RULES_ACCEPTED', | ||
isAscsMember: true, | ||
isEnvitedMember: true, | ||
} | ||
const dbStub = jest.fn().mockImplementation(() => | ||
Promise.resolve({ | ||
getUserById: () => Promise.resolve([user]), | ||
}), | ||
) | ||
|
||
await expect(SUT._getUserById({ 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().mockImplementation(() => | ||
Promise.resolve({ | ||
user: { | ||
pkh: 'ISSUER_PKH', | ||
}, | ||
}), | ||
) | ||
const user = { | ||
id: 'USER_PKH', | ||
issuerId: 'FEDERATOR_PKH', | ||
name: 'USER_NAME', | ||
email: 'USER_EMAIL', | ||
vatId: 'USER_VAT_ID', | ||
privacyPolicyAccepted: 'USER_PRIVACY_POLICY_ACCEPTED', | ||
articlesOfAssociationAccepted: 'USER_ARTICLES_OF_ASSOCIATION_ACCEPTED', | ||
contributionRulesAccepted: 'USER_CONTRIBUTION_RULES_ACCEPTED', | ||
isAscsMember: true, | ||
isEnvitedMember: true, | ||
} | ||
const dbStub = jest.fn().mockImplementation(() => | ||
Promise.resolve({ | ||
getUserById: () => Promise.resolve([user]), | ||
}), | ||
) | ||
|
||
await expect(SUT._getUserById({ db: dbStub, getServerSession: getServerSessionStub })('USER_ID')).rejects.toThrow( | ||
'Something went wrong', | ||
) | ||
}) | ||
}) |
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,35 @@ | ||
import { isNil } from 'ramda' | ||
|
||
import { isOwnUser, userIsIssuedByLoggedInUser } from '../../app/api/utils' | ||
import { getServerSession } from '../auth' | ||
import { db } from '../database/queries' | ||
import { Database } from '../database/types' | ||
import { User } from '../types' | ||
import { Session } from '../types/types' | ||
import { badRequestError, error, unauthorizedError } from '../utils' | ||
|
||
export const _getUserById = | ||
({ 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) && !isOwnUser(user)(session)) { | ||
throw badRequestError() | ||
} | ||
|
||
return user | ||
} catch (e) { | ||
console.log('error', e) | ||
throw error() | ||
} | ||
} | ||
|
||
export const getUserById = _getUserById({ 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export { Language, Columns, Size, ColorScheme, Role, CredentialType } from './types' | ||
export type { Action, Obj } from './types' | ||
export type { Action, Obj, User } from './types' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export const unauthorized = () => new Error('Unauthorized') | ||
|
||
export const badRequest = () => new Error('Bad Request') | ||
|
||
export const internalServerError = () => new Error('Internal Server Error') | ||
|
||
export const notFound = () => new Error('Not Found') | ||
|
||
export const error = () => new Error('Something went wrong') |
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 |
---|---|---|
@@ -1 +1,8 @@ | ||
export { ok, badRequest, noContent, notFound, internalServerError } from './utils' | ||
export { ok, badRequest, noContent, notFound, internalServerError, unauthorized } from './utils' | ||
export { | ||
badRequest as badRequestError, | ||
internalServerError as internalServerErrorError, | ||
notFound as notFoundError, | ||
unauthorized as unauthorizedError, | ||
error, | ||
} from './errors' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { FC } from 'react' | ||
|
||
import { User as IUser } from '../../common/types' | ||
|
||
export const User: FC<IUser> = ({ | ||
id, | ||
issuerId, | ||
// addressCountry, | ||
// addressLocality, | ||
// addressTypeId, | ||
// articlesOfAssociationAccepted, | ||
// contributionRulesAccepted, | ||
// createdAt, | ||
// email, | ||
// expirationDate, | ||
// isAscsMember, | ||
// isEnvitedMember, | ||
// issuanceDate, | ||
// name, | ||
// postalCode, | ||
// privacyPolicyAccepted, | ||
// streetAddress, | ||
// updatedAt, | ||
// vatId, | ||
}) => { | ||
return ( | ||
<div> | ||
<h1 className="text-3xl font-bold mb-5">User data:</h1> | ||
<dl className="mb-10"> | ||
<dt className="font-bold">User</dt> | ||
<dd className="ml-5 italic">{id}</dd> | ||
<dt className="font-bold">Issued by</dt> | ||
<dd className="ml-5 italic">{issuerId}</dd> | ||
</dl> | ||
</div> | ||
) | ||
} |
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 @@ | ||
export { User } from './User' |