Skip to content

Commit

Permalink
one more thing... message test
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe committed Jun 21, 2024
1 parent c1f5cf5 commit 59d7a7f
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 21 deletions.
176 changes: 167 additions & 9 deletions packages/auth-providers/dbAuth/setup/src/__tests__/setup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,26 @@ import path from 'node:path'

import { vol } from 'memfs'

import { type AuthHandlerArgs } from '@redwoodjs/cli-helpers'

jest.mock('fs', () => require('memfs').fs)

import { createAuthDecoderFunction } from '../setupHandler'
import { createAuthDecoderFunction, handler } from '../setupHandler'

const RWJS_CWD = process.env.RWJS_CWD
const redwoodProjectPath = '/redwood-app'

jest.mock('../setupData', () => ({
notes: '',
extraTask: undefined,
}))

jest.mock('../shared', () => ({
hasModel: () => false,
generateAuthPagesTask: () => undefined,
}))

jest.mock('@redwoodjs/cli-helpers', () => {
return {
getGraphqlPath: () => {
return redwoodProjectPath + '/api/src/functions/graphql.ts'
},
addEnvVarTask: () => undefined,
getPaths: () => ({
base: redwoodProjectPath,
}),
Expand All @@ -34,6 +33,12 @@ jest.mock('@redwoodjs/cli-helpers', () => {
bold: (str: string) => str,
underline: (str: string) => str,
},
// I wish I could have used something like
// jest.requireActual(@redwoodjs/cli-helpers) here, but I couldn't because
// jest doesn't support ESM
standardAuthHandler: async (args: AuthHandlerArgs) => {
args.notes && console.log(`\n ${args.notes.join('\n ')}\n`)
},
}
})

Expand All @@ -45,13 +50,23 @@ afterAll(() => {
process.env.RWJS_CWD = RWJS_CWD
})

beforeEach(() => {
jest.spyOn(console, 'log').mockImplementation(() => {})
})

afterEach(() => {
jest.mocked(console).log.mockRestore?.()
})

describe('dbAuth setup command', () => {
it('does not duplicate authDecoder creation', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')
const graphqlTsPath = 'api/src/functions/graphql.ts'

vol.fromJSON(
{
[path.resolve(__dirname, '../../package.json')]:
'{ "version": "0.0.0" }',
'api/src/functions/graphql.ts': `
[packageJsonPath]: '{ "version": "0.0.0" }',
[graphqlTsPath]: `
import { createGraphQLHandler } from '@redwoodjs/graphql-server'
import directives from 'src/directives/**/*.{js,ts}'
Expand Down Expand Up @@ -91,4 +106,147 @@ export const handler = createGraphQLHandler({
vol.toJSON()[redwoodProjectPath + '/api/src/functions/graphql.ts']
expect(updatedGraphqlTs).toEqual(updatedGraphqlTs2)
})

describe('One More Thing... message', () => {
describe('page generation hint', () => {
it('is not included if page generation was already done as part of the setup process', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: false,
createUserModel: false,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).not.toContain(
'yarn rw generate dbAuth',
)
})

it('is not included for WebAuthn if page generation was already done as part of the setup process', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: true,
createUserModel: false,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).not.toContain(
'yarn rw generate dbAuth',
)
})

it('is not included if page generation and model generation was already done as part of the setup process', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: false,
createUserModel: true,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).not.toContain(
'yarn rw generate dbAuth',
)
})

it('is not included for WebAuthn if page generation and model generation was already done as part of the setup process', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: true,
createUserModel: true,
generateAuthPages: true,
force: false,
})

expect(jest.mocked(console).log.mock.calls[0][0]).not.toContain(
'yarn rw generate dbAuth',
)
})

it('is included if page generation was not done as part of the setup process', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: false,
createUserModel: false,
generateAuthPages: false,
force: false,
})

const firstLogMessage = jest.mocked(console).log.mock.calls[0][0]

// Included exactly once
expect(firstLogMessage.match(/yarn rw generate dbAuth/g)).toHaveLength(
1,
)
})

it('is included for WebAuthn if page generation was not done as part of the setup process', async () => {
const packageJsonPath = path.resolve(__dirname, '../../package.json')

vol.fromJSON(
{
[packageJsonPath]: '{ "version": "0.0.0" }',
},
redwoodProjectPath,
)

await handler({
webauthn: true,
createUserModel: false,
generateAuthPages: false,
force: false,
})

const firstLogMessage = jest.mocked(console).log.mock.calls[0][0]

// Included exactly once
expect(firstLogMessage.match(/and WebAuthn prompts/g)).toHaveLength(1)
expect(firstLogMessage.match(/yarn rw generate dbAuth/g)).toHaveLength(
1,
)
})
})
})
})
5 changes: 0 additions & 5 deletions packages/auth-providers/dbAuth/setup/src/setupData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ export const notes = [
'change this secret to a new value and deploy. To create a new secret, run:',
'',
' yarn rw generate secret',
'',
"Need simple Login, Signup and Forgot Password pages? We've got a generator",
'for those as well:',
'',
' yarn rw generate dbAuth',
]

export const notesCreatedUserModel = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ export const notes = [
'change this secret to a new value and deploy. To create a new secret, run:',
'',
' yarn rw generate secret',
'',
'Need simple Login, Signup, Forgot Password pages and WebAuthn prompts?',
"We've got a generator for those as well:",
'',
' yarn rw generate dbAuth',
]

// Only thing different here compared to the notes for when *not* setting up
Expand Down
4 changes: 2 additions & 2 deletions packages/cli-helpers/src/auth/setupHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const standardAuthBuilder = (yargs: Argv) => {
)
}

interface Args {
export interface AuthHandlerArgs {
basedir: string
forceArg: boolean
provider: string
Expand Down Expand Up @@ -79,7 +79,7 @@ export const standardAuthHandler = async ({
extraTasks,
notes,
verbose,
}: Args) => {
}: AuthHandlerArgs) => {
// @TODO detect if auth already setup. If it is, ask how to proceed:
// How would you like to proceed?
// 1. Replace existing auth provider with <provider>
Expand Down
1 change: 1 addition & 0 deletions packages/cli-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export * from './lib/paths.js'
export * from './lib/project.js'
export * from './lib/version.js'
export * from './auth/setupHelpers.js'
export type { AuthHandlerArgs } from './auth/setupHelpers.js'

export * from './lib/installHelpers.js'

Expand Down

0 comments on commit 59d7a7f

Please sign in to comment.