Skip to content

Commit d33c24b

Browse files
authored
chore(test-project): Fix test-project generation script, and regenerate fixture (#9779)
1 parent 84d69af commit d33c24b

File tree

11 files changed

+83
-37
lines changed

11 files changed

+83
-37
lines changed

__fixtures__/test-project/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Welcome to [RedwoodJS](https://redwoodjs.com)!
44

55
> **Prerequisites**
66
>
7-
> - Redwood requires [Node.js](https://nodejs.org/en/) (=18.x) and [Yarn](https://yarnpkg.com/) (>=1.15)
7+
> - Redwood requires [Node.js](https://nodejs.org/en/) (=20.x) and [Yarn](https://yarnpkg.com/)
88
> - Are you on Windows? For best results, follow our [Windows development setup](https://redwoodjs.com/docs/how-to/windows-development-setup) guide
99
1010
Start by installing dependencies:

__fixtures__/test-project/api/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"private": true,
55
"dependencies": {
66
"@redwoodjs/api": "6.0.7",
7-
"@redwoodjs/auth-dbauth-api": "6.0.7",
7+
"@redwoodjs/auth-dbauth-api": "7.0.0-canary.789",
88
"@redwoodjs/graphql-server": "6.0.7"
99
}
1010
}

__fixtures__/test-project/api/src/functions/auth.ts

+30-16
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { APIGatewayProxyEvent, Context } from 'aws-lambda'
33
import { DbAuthHandler } from '@redwoodjs/auth-dbauth-api'
44
import type { DbAuthHandlerOptions, UserType } from '@redwoodjs/auth-dbauth-api'
55

6+
import { cookieName } from 'src/lib/auth'
67
import { db } from 'src/lib/db'
78

89
export const handler = async (
@@ -18,11 +19,20 @@ export const handler = async (
1819
// https://example.com/reset-password?resetToken=${user.resetToken}
1920
//
2021
// Whatever is returned from this function will be returned from
21-
// the `forgotPassword()` function that is destructured from `useAuth()`
22+
// the `forgotPassword()` function that is destructured from `useAuth()`.
2223
// You could use this return value to, for example, show the email
2324
// address in a toast message so the user will know it worked and where
2425
// to look for the email.
25-
handler: (user) => {
26+
//
27+
// Note that this return value is sent to the client in *plain text*
28+
// so don't include anything you wouldn't want prying eyes to see. The
29+
// `user` here has been sanitized to only include the fields listed in
30+
// `allowedUserFields` so it should be safe to return as-is.
31+
handler: (user, _resetToken) => {
32+
// TODO: Send user an email/message with a link to reset their password,
33+
// including the `resetToken`. The URL should look something like:
34+
// `http://localhost:8910/reset-password?resetToken=${resetToken}`
35+
2636
return user
2737
},
2838

@@ -115,12 +125,7 @@ export const handler = async (
115125
//
116126
// If this returns anything else, it will be returned by the
117127
// `signUp()` function in the form of: `{ message: 'String here' }`.
118-
handler: ({
119-
username,
120-
hashedPassword,
121-
salt,
122-
userAttributes
123-
}) => {
128+
handler: ({ username, hashedPassword, salt, userAttributes }) => {
124129
return db.user.create({
125130
data: {
126131
email: username,
@@ -165,17 +170,26 @@ export const handler = async (
165170
resetTokenExpiresAt: 'resetTokenExpiresAt',
166171
},
167172

173+
// A list of fields on your user object that are safe to return to the
174+
// client when invoking a handler that returns a user (like forgotPassword
175+
// and signup). This list should be as small as possible to be sure not to
176+
// leak any sensitive information to the client.
177+
allowedUserFields: ['id', 'email'],
178+
168179
// Specifies attributes on the cookie that dbAuth sets in order to remember
169180
// who is logged in. See https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies#restrict_access_to_cookies
170181
cookie: {
171-
HttpOnly: true,
172-
Path: '/',
173-
SameSite: 'Strict',
174-
Secure: process.env.NODE_ENV !== 'development',
175-
176-
// If you need to allow other domains (besides the api side) access to
177-
// the dbAuth session cookie:
178-
// Domain: 'example.com',
182+
attributes: {
183+
HttpOnly: true,
184+
Path: '/',
185+
SameSite: 'Strict',
186+
Secure: process.env.NODE_ENV !== 'development',
187+
188+
// If you need to allow other domains (besides the api side) access to
189+
// the dbAuth session cookie:
190+
// Domain: 'example.com',
191+
},
192+
name: cookieName,
179193
},
180194

181195
forgotPassword: forgotPasswordOptions,

__fixtures__/test-project/api/src/functions/graphql.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { authDecoder } from '@redwoodjs/auth-dbauth-api'
1+
import { createAuthDecoder } from '@redwoodjs/auth-dbauth-api'
22
import { createGraphQLHandler } from '@redwoodjs/graphql-server'
33

44
import directives from 'src/directives/**/*.{js,ts}'
55
import sdls from 'src/graphql/**/*.sdl.{js,ts}'
66
import services from 'src/services/**/*.{js,ts}'
77

8-
import { getCurrentUser } from 'src/lib/auth'
8+
import { cookieName, getCurrentUser } from 'src/lib/auth'
99
import { db } from 'src/lib/db'
1010
import { logger } from 'src/lib/logger'
1111

12+
const authDecoder = createAuthDecoder(cookieName)
13+
1214
export const handler = createGraphQLHandler({
1315
authDecoder,
1416
getCurrentUser,

__fixtures__/test-project/api/src/lib/auth.ts

+9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import { AuthenticationError, ForbiddenError } from '@redwoodjs/graphql-server'
33

44
import { db } from './db'
55

6+
/**
7+
* The name of the cookie that dbAuth sets
8+
*
9+
* %port% will be replaced with the port the api server is running on.
10+
* If you have multiple RW apps running on the same host, you'll need to
11+
* make sure they all use unique cookie names
12+
*/
13+
export const cookieName = 'session_%port%'
14+
615
/**
716
* The session object sent in as the first argument to getCurrentUser() will
817
* have a single key `id` containing the unique ID of the logged in user

__fixtures__/test-project/web/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
]
1212
},
1313
"dependencies": {
14-
"@redwoodjs/auth-dbauth-web": "6.0.7",
14+
"@redwoodjs/auth-dbauth-web": "7.0.0-canary.789",
1515
"@redwoodjs/forms": "6.0.7",
1616
"@redwoodjs/router": "6.0.7",
1717
"@redwoodjs/web": "6.0.7",
@@ -25,8 +25,8 @@
2525
"@types/react-dom": "18.2.15",
2626
"autoprefixer": "^10.4.16",
2727
"postcss": "^8.4.32",
28-
"postcss-loader": "^7.3.3",
28+
"postcss-loader": "^7.3.4",
2929
"prettier-plugin-tailwindcss": "0.4.1",
30-
"tailwindcss": "^3.3.6"
30+
"tailwindcss": "^3.4.0"
3131
}
3232
}

__fixtures__/test-project/web/vite.config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ import dns from 'dns'
33
import type { UserConfig } from 'vite'
44
import { defineConfig } from 'vite'
55

6-
// See: https://vitejs.dev/config/server-options.html#server-host
7-
// So that Vite will load on local instead of 127.0.0.1
8-
dns.setDefaultResultOrder('verbatim')
9-
106
import redwood from '@redwoodjs/vite'
117

8+
// So that Vite will load on localhost instead of `127.0.0.1`.
9+
// See: https://vitejs.dev/config/server-options.html#server-host.
10+
dns.setDefaultResultOrder('verbatim')
11+
1212
const viteConfig: UserConfig = {
1313
plugins: [redwood()],
1414
}

tasks/test-project/codemods/models.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const post = `model Post {
1010
}`
1111

1212
const contact = `model Contact {
13-
id Int @id @default(autoincrement())
13+
id Int @id @default(autoincrement())
1414
name String
1515
email String
1616
message String

tasks/test-project/templates/api/context.test.ts.template

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ test('Set a mock user on the context', async () => {
1010
})
1111

1212
test('Context is isolated between tests', () => {
13-
expect(context).toStrictEqual({ currentUser: undefined })
13+
expect(context).toStrictEqual({})
1414
})

tasks/test-project/tui-tasks.js

+27-8
Original file line numberDiff line numberDiff line change
@@ -387,9 +387,9 @@ async function webTasks(outputPath, { linkWithLatestFwBuild }) {
387387
async function addModel(schema) {
388388
const path = `${OUTPUT_PATH}/api/db/schema.prisma`
389389

390-
const current = fs.readFileSync(path)
390+
const current = fs.readFileSync(path, 'utf-8')
391391

392-
fs.writeFileSync(path, `${current}\n\n${schema}`)
392+
fs.writeFileSync(path, `${current.trim()}\n\n${schema}\n`)
393393
}
394394

395395
async function apiTasks(outputPath, { linkWithLatestFwBuild }) {
@@ -406,20 +406,39 @@ async function apiTasks(outputPath, { linkWithLatestFwBuild }) {
406406
},
407407
})
408408

409+
// At an earlier step we run `yarn rwfw project:copy` which gives us
410+
// auth-dbauth-setup@3.2.0 currently. We need that version to be a canary
411+
// version for auth-dbauth-api and auth-dbauth-web package installations
412+
// to work. So we update the package.json to make the setup use the latest
413+
// canary version for the api and web sides
414+
415+
const { stdout } = await exec(
416+
`yarn npm info @redwoodjs/auth-dbauth-setup --fields versions --json`,
417+
[],
418+
execaOptions
419+
)
420+
421+
const latestCanaryVersion = JSON.parse(stdout)
422+
.versions.filter((version) => version.includes('canary'))
423+
.at(-1)
424+
409425
const dbAuthSetupPath = path.join(
410426
outputPath,
411427
'node_modules',
412428
'@redwoodjs',
413429
'auth-dbauth-setup'
414430
)
415431

416-
// At an earlier step we run `yarn rwfw project:copy` which gives us
417-
// auth-dbauth-setup@3.2.0 currently. We need that version to be a canary
418-
// version for auth-dbauth-api and auth-dbauth-web package installations
419-
// to work. So we remove the current version and add a canary version
420-
// instead.
432+
const dbAuthSetupPackageJson = JSON.parse(
433+
fs.readFileSync(path.join(dbAuthSetupPath, 'package.json'), 'utf-8')
434+
)
435+
436+
dbAuthSetupPackageJson.version = latestCanaryVersion
421437

422-
fs.rmSync(dbAuthSetupPath, { recursive: true, force: true })
438+
fs.writeFileSync(
439+
path.join(dbAuthSetupPath, 'package.json'),
440+
JSON.stringify(dbAuthSetupPackageJson, null, 2)
441+
)
423442

424443
await exec(
425444
'yarn rw setup auth dbAuth --force --no-webauthn',

tasks/test-project/util.js

+2
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ async function exec(...args) {
9292
if (exitCode !== 0) {
9393
throw new ExecaError({ stdout, stderr, exitCode })
9494
}
95+
96+
return { stdout, stderr, exitCode }
9597
})
9698
.catch((error) => {
9799
if (error instanceof ExecaError) {

0 commit comments

Comments
 (0)