Skip to content

Commit

Permalink
feat: next-auth upgrade to 5
Browse files Browse the repository at this point in the history
  • Loading branch information
theexiile1305 committed Apr 29, 2024
1 parent 1efc0b5 commit 6df64fe
Show file tree
Hide file tree
Showing 26 changed files with 201 additions and 325 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ Create a file called `.env.local` in the root of the project and add the followi

```bash
# next-auth environment variables
NEXTAUTH_DEBUG="<true|false>"
NEXTAUTH_SECRET="<given-secret>"
NEXTAUTH_URL="<given-base-url>"
AUTH_DEBUG="<true|false>"
AUTH_SECRET="<given-secret>" # generate a secret using `pnpm exec auth secret`

# shibboleth oidc environment variables
SHIBBOLETH_OIDC_ISSUER_URL="<given-issuer-url>"
Expand Down
62 changes: 4 additions & 58 deletions __tests__/app/errorDescription.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,6 @@ import { describe, it, expect } from 'vitest';
import { getAuthErrorDescription } from '@/app/errorDescription';

describe('errorDescription', () => {
it('OAuthSignin should return value', () => {
expect(getAuthErrorDescription('OAuthSignin')).toEqual(
'Error in constructing an authorization URL.',
);
});

it('OAuthCallback should return value', () => {
expect(getAuthErrorDescription('OAuthCallback')).toEqual(
'Error in handling the response from an OAuth provider.',
);
});

it('OAuthCreateAccount should return value', () => {
expect(getAuthErrorDescription('OAuthCreateAccount')).toEqual(
'Could not create OAuth provider user in the database.',
);
});

it('EmailCreateAccount should return value', () => {
expect(getAuthErrorDescription('EmailCreateAccount')).toEqual(
'Could not create email provider user in the database.',
);
});

it('Callback should return value', () => {
expect(getAuthErrorDescription('Callback')).toEqual(
'Error in the OAuth callback handler route.',
);
});

it('OAuthAccountNotLinked should return value', () => {
expect(getAuthErrorDescription('OAuthAccountNotLinked')).toEqual(
'If the email on the account is already linked, but not with this OAuth account.',
);
});

it('EmailSignin should return value', () => {
expect(getAuthErrorDescription('EmailSignin')).toEqual(
'Sending the e-mail with the verification token failed.',
);
});

it('CredentialsSignin should return value', () => {
expect(getAuthErrorDescription('CredentialsSignin')).toEqual(
"The authorize callback returned null in the Credentials provider. We don't recommend providing information about which part of the credentials were wrong, as it might be abused by malicious hackers.",
);
});

it('SessionRequired should return value', () => {
expect(getAuthErrorDescription('SessionRequired')).toEqual(
'The content of this page requires you to be signed in at all times. See useSession for configuration.',
);
});

it('Configuration should return value', () => {
expect(getAuthErrorDescription('Configuration')).toEqual(
'There is a problem with the server configuration. Check if your options are correct.',
Expand All @@ -74,15 +20,15 @@ describe('errorDescription', () => {
);
});

it('default should return value', () => {
expect(getAuthErrorDescription('')).toEqual(
'Catch all, will apply, if none of the other errors matched.',
it('Default should return value', () => {
expect(getAuthErrorDescription('EmailCreateAccount')).toEqual(
'Catch all, will apply, if none of the above matched.',
);
});

it('default should return value', () => {
expect(getAuthErrorDescription(null)).toEqual(
'Catch all, will apply, if none of the other errors matched.',
'Catch all, will apply, if none of the above matched.',
);
});
});
2 changes: 1 addition & 1 deletion __tests__/app/not-found.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { cleanup, render, screen } from '@testing-library/react';

describe('NotFound', () => {
beforeEach(() => {
render(NotFound());
render(<NotFound/>);
});

it('should exists', () => {
Expand Down
10 changes: 5 additions & 5 deletions __tests__/app/page.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { cleanup, render, screen } from '@testing-library/react';
import Page from '../../app/page';
import { getServerSession } from 'next-auth';
import { auth } from '@/auth';

vi.mock('next-auth', () => ({
getServerSession: vi.fn(),
vi.mock('@/auth', () => ({
auth: vi.fn(),
}));

describe('Home, when user is not authenticated', () => {
beforeEach(async () => {
(getServerSession as Mock).mockReturnValue(null);
(auth as Mock).mockReturnValue(null);
render(await Page());
});

Expand Down Expand Up @@ -44,7 +44,7 @@ describe('Home, when user is authenticated', () => {
};

beforeEach(async () => {
(getServerSession as Mock).mockReturnValue(Promise.resolve(session));
(auth as Mock).mockReturnValue(Promise.resolve(session));
render(await Page());
});

Expand Down
4 changes: 2 additions & 2 deletions __tests__/app/ui/__snapshots__/navbar.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Navbar, when user is authenticated > Button should match snapshot 1`] =
<button
class="MuiButtonBase-root MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit css-sbfmij-MuiButtonBase-root-MuiButton-root"
tabindex="0"
type="button"
type="submit"
>
Abmelden
<span
Expand Down Expand Up @@ -46,7 +46,7 @@ exports[`Navbar, when user is not authenticated > Button should match snapshot 1
<button
class="MuiButtonBase-root MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit css-sbfmij-MuiButtonBase-root-MuiButton-root"
tabindex="0"
type="button"
type="submit"
>
Anmelden
<span
Expand Down
15 changes: 14 additions & 1 deletion __tests__/app/ui/__snapshots__/signInButton.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ exports[`SignInButton > should match snapshot 1`] = `
<button
class="MuiButtonBase-root MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit css-sbfmij-MuiButtonBase-root-MuiButton-root"
tabindex="0"
type="button"
type="submit"
>
Anmelden
<span
class="MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root"
/>
</button>
`;

exports[`SignInButton 1`] = `
<button
class="MuiButtonBase-root MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit css-sbfmij-MuiButtonBase-root-MuiButton-root"
tabindex="0"
type="submit"
>
Anmelden
<span
Expand Down
15 changes: 14 additions & 1 deletion __tests__/app/ui/__snapshots__/signOutButton.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ exports[`SignOutButton > should match snapshot 1`] = `
<button
class="MuiButtonBase-root MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit css-sbfmij-MuiButtonBase-root-MuiButton-root"
tabindex="0"
type="button"
type="submit"
>
Abmelden
<span
class="MuiTouchRipple-root css-8je8zh-MuiTouchRipple-root"
/>
</button>
`;

exports[`SignOutButton 1`] = `
<button
class="MuiButtonBase-root MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit MuiButton-root MuiButton-outlined MuiButton-outlinedInherit MuiButton-sizeMedium MuiButton-outlinedSizeMedium MuiButton-colorInherit css-sbfmij-MuiButtonBase-root-MuiButton-root"
tabindex="0"
type="submit"
>
Abmelden
<span
Expand Down
2 changes: 1 addition & 1 deletion __tests__/app/ui/authErrorView.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('AuthErrorView', () => {
};
});

render(AuthErrorView());
render(<AuthErrorView />);
});

it('should exists', () => {
Expand Down
10 changes: 5 additions & 5 deletions __tests__/app/ui/navbar.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { afterEach, beforeEach, describe, expect, it, Mock, vi } from 'vitest';
import { cleanup, render, screen } from '@testing-library/react';
import { getServerSession } from 'next-auth';
import Navbar from '@/app/ui/navbar';
import { auth } from '@/auth';

vi.mock('next-auth', () => ({
getServerSession: vi.fn(),
vi.mock('@/auth', () => ({
auth: vi.fn(),
}));

describe('Navbar, when user is not authenticated', () => {
beforeEach(async () => {
(getServerSession as Mock).mockReturnValue(null);
(auth as Mock).mockReturnValue(null);
render(await Navbar());
});

Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Navbar, when user is authenticated', () => {
};

beforeEach(async () => {
(getServerSession as Mock).mockReturnValue(Promise.resolve(session));
(auth as Mock).mockReturnValue(Promise.resolve(session));
render(await Navbar());
});

Expand Down
38 changes: 9 additions & 29 deletions __tests__/app/ui/signInButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { expect, it, test, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import SignInButton from '@/app/ui/signInButton';

describe('SignInButton', () => {
beforeEach(() => {
render(SignInButton());
});

it('should exists', () => {
expect(screen.getByRole('button')).toBeDefined();
});

it('should match snapshot', () => {
expect(screen.getByRole('button')).toMatchSnapshot();
});

it('should trigger signOut function', () => {
const { mockedSignIn } = vi.hoisted(() => {
return { mockedSignIn: vi.fn(() => Promise.resolve()) };
});
vi.mock('next-auth/react', () => {
return { signIn: mockedSignIn };
});

fireEvent.click(screen.getByText(/Anmelden/));
expect(mockedSignIn).toHaveBeenCalledOnce();
});

afterEach(() => {
cleanup();
test('SignInButton', () => {
vi.mock('@/auth', () => {
return { signIn: vi.fn(() => Promise.resolve()) };
});
render(<SignInButton/>);
expect(screen.getByRole('button')).toBeDefined();
expect(screen.getByRole('button')).toMatchSnapshot();
fireEvent.click(screen.getByText(/Anmelden/));
});
38 changes: 9 additions & 29 deletions __tests__/app/ui/signOutButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,13 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { test, expect, vi } from 'vitest';
import { fireEvent, render, screen } from '@testing-library/react';
import SignOutButton from '@/app/ui/signOutButton';

describe('SignOutButton', () => {
beforeEach(() => {
render(SignOutButton());
});

it('should exists', () => {
expect(screen.getByRole('button')).toBeDefined();
});

it('should match snapshot', () => {
expect(screen.getByRole('button')).toMatchSnapshot();
});

it('should trigger signOut function', () => {
const { mockedSignOut } = vi.hoisted(() => {
return { mockedSignOut: vi.fn(() => Promise.resolve()) };
});
vi.mock('next-auth/react', () => {
return { signOut: mockedSignOut };
});

fireEvent.click(screen.getByText(/Abmelden/));
expect(mockedSignOut).toHaveBeenCalledOnce();
});

afterEach(() => {
cleanup();
test('SignOutButton', () => {
vi.mock('@/auth', () => {
return { signOut: vi.fn(() => Promise.resolve()) };
});
render(<SignOutButton/>);
expect(screen.getByRole('button')).toBeDefined();
expect(screen.getByRole('button')).toMatchSnapshot();
fireEvent.click(screen.getByText(/Abmelden/));
});
Loading

0 comments on commit 6df64fe

Please sign in to comment.