-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into dt-async-mock-directive
- Loading branch information
Showing
12 changed files
with
507 additions
and
12 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
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,56 @@ | ||
import Listr from 'listr' | ||
|
||
import { | ||
existsAnyExtensionSync, | ||
deleteFile, | ||
readFile, | ||
writeFile, | ||
getGraphqlPath, | ||
} from '../../../lib' | ||
import c from '../../../lib/colors' | ||
import { getOutputPath } from '../../setup/graphiql/graphiql' | ||
|
||
const removeGraphiqlFromGraphqlHandler = () => { | ||
const graphqlPath = getGraphqlPath() | ||
let content = readFile(graphqlPath).toString() | ||
const [_, hasHeaderImport] = | ||
content.match(/(import .* from 'src\/lib\/generateGraphiQLHeader.*')/s) || | ||
[] | ||
if (hasHeaderImport) { | ||
// remove header import statement | ||
content = content.replace( | ||
`\n\nimport generateGraphiQLHeader from 'src/lib/generateGraphiQLHeader'`, | ||
'' | ||
) | ||
// remove object from handler | ||
content = content.replace(`generateGraphiQLHeader,\n`, '') | ||
} | ||
writeFile(graphqlPath, content, { | ||
overwriteExisting: true, | ||
}) | ||
} | ||
export const command = 'graphiql' | ||
export const description = 'Destroy graphiql header' | ||
|
||
export const handler = () => { | ||
const path = getOutputPath() | ||
const tasks = new Listr( | ||
[ | ||
{ | ||
title: 'Destroying graphiql files...', | ||
skip: () => !existsAnyExtensionSync(path) && `File doesn't exist`, | ||
task: () => deleteFile(path), | ||
}, | ||
{ | ||
title: 'Removing graphiql import from createGraphQLHandler', | ||
task: removeGraphiqlFromGraphqlHandler, | ||
}, | ||
], | ||
{ collapse: false, exitOnError: true } | ||
) | ||
try { | ||
tasks.run() | ||
} catch (e) { | ||
console.log(c.error(e.message)) | ||
} | ||
} |
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
80 changes: 80 additions & 0 deletions
80
packages/cli/src/commands/setup/graphiql/__tests__/graphiqlHandler.test.js
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,80 @@ | ||
global.__dirname = __dirname | ||
|
||
import '../../../../lib/mockTelemetry' | ||
|
||
jest.mock('@redwoodjs/internal', () => { | ||
return { | ||
registerApiSideBabelHook: () => null, | ||
} | ||
}) | ||
jest.mock('../../../../lib', () => ({ | ||
getPaths: () => ({ | ||
api: { lib: '', functions: '' }, | ||
}), | ||
existsAnyExtensionSync: () => false, | ||
})) | ||
|
||
jest.mock('listr') | ||
import chalk from 'chalk' | ||
import listr from 'listr' | ||
|
||
import * as graphiql from '../graphiql' | ||
|
||
describe('Graphiql generator tests', () => { | ||
const processExitSpy = jest | ||
.spyOn(process, 'exit') | ||
.mockImplementation(() => {}) | ||
const cSpy = jest.spyOn(console, 'error').mockImplementation(() => {}) | ||
|
||
const mockListrRun = jest.fn() | ||
listr.mockImplementation(() => { | ||
return { | ||
run: mockListrRun, | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
processExitSpy.mockReset() | ||
cSpy.mockReset() | ||
}) | ||
|
||
it('throws an error if source path does not exist when viewing headers', async () => { | ||
jest.spyOn(graphiql, 'getOutputPath').mockImplementation(() => '') | ||
await graphiql.handler({ view: true, provider: 'dbAuth' }) | ||
expect(console.error).toHaveBeenCalledWith( | ||
chalk.bold.red( | ||
'Must run yarn rw setup graphiql <provider> to generate headers before viewing' | ||
) | ||
) | ||
expect(processExitSpy).toHaveBeenCalledWith(1) | ||
}) | ||
|
||
it('throws an error if auth provider is dbAuth and no user id is provided', () => { | ||
try { | ||
graphiql.generatePayload('dbAuth') | ||
} catch (e) { | ||
expect(e.message).toBe('Require an unique id to generate session cookie') | ||
} | ||
}) | ||
|
||
it('throws an error if auth provider is dbAuth and no supabase env is set', () => { | ||
process.env.SESSION_SECRET = null | ||
try { | ||
graphiql.generatePayload('dbAuth', 'user-id-123') | ||
} catch (e) { | ||
expect(e.message).toBe( | ||
'dbAuth requires a SESSION_SECRET environment variable that is used to encrypt session cookies. Use `yarn rw g secret` to create one, then add to your `.env` file. DO NOT check this variable in your version control system!!' | ||
) | ||
} | ||
}) | ||
|
||
it('returns a payload if a token is provided', async () => { | ||
const provider = 'supabase' | ||
const token = 'mock-token' | ||
const response = graphiql.generatePayload(provider, null, token) | ||
expect(response).toEqual({ | ||
'auth-provider': provider, | ||
authorization: `Bearer ${token}`, | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.