-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update all internal uses of createItems (#3301)
* Update usage of createItems
- Loading branch information
1 parent
a8cb4e4
commit acaf19c
Showing
23 changed files
with
314 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
'@keystonejs/api-tests': patch | ||
'@keystonejs/demo-project-blog': patch | ||
'@keystonejs/demo-project-meetup': patch | ||
'@keystonejs/server-side-graphql-client': patch | ||
'@keystonejs/cypress-project-access-control': patch | ||
'@keystonejs/cypress-project-basic': patch | ||
'@keystonejs/cypress-project-client-validation': patch | ||
'@keystonejs/cypress-project-login': patch | ||
'@keystonejs/cypress-project-social-login': patch | ||
--- | ||
|
||
No functional changes. Update all internal usages of `keystone.createItems` to the new `createItems` function. |
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 |
---|---|---|
|
@@ -2,18 +2,23 @@ const { PasswordAuthStrategy } = require('@keystonejs/auth-password'); | |
const { Text, Password, DateTime } = require('@keystonejs/fields'); | ||
const { multiAdapterRunners, networkedGraphqlRequest } = require('@keystonejs/test-utils'); | ||
const { setupServer } = require('@keystonejs/test-utils'); | ||
const { createItems } = require('@keystonejs/server-side-graphql-client'); | ||
|
||
const initialData = { | ||
User: [ | ||
{ | ||
name: 'Boris Bozic', | ||
email: '[email protected]', | ||
password: 'correctbattery', | ||
data: { | ||
name: 'Boris Bozic', | ||
email: '[email protected]', | ||
password: 'correctbattery', | ||
}, | ||
}, | ||
{ | ||
name: 'Jed Watson', | ||
email: '[email protected]', | ||
password: 'horsestaple', | ||
data: { | ||
name: 'Jed Watson', | ||
email: '[email protected]', | ||
password: 'horsestaple', | ||
}, | ||
}, | ||
], | ||
}; | ||
|
@@ -77,14 +82,19 @@ function login(app, email, password) { | |
}); | ||
} | ||
|
||
const schemaName = 'testing'; | ||
|
||
multiAdapterRunners().map(({ runner, adapterName }) => | ||
describe(`Adapter: ${adapterName}`, () => { | ||
describe('Auth testing', () => { | ||
test( | ||
'Gives access denied when not logged in', | ||
runner(setupKeystone, async ({ keystone, app }) => { | ||
// seed the db | ||
await keystone.createItems(initialData); | ||
const context = keystone.createContext({ schemaName, skipAccessControl: true }); | ||
for (const [listKey, items] of Object.entries(initialData)) { | ||
await createItems({ keystone, listKey, items, context }); | ||
} | ||
const { data, errors } = await networkedGraphqlRequest({ | ||
app, | ||
query: '{ allUsers { id } }', | ||
|
@@ -98,11 +108,14 @@ multiAdapterRunners().map(({ runner, adapterName }) => | |
test( | ||
'Allows access with bearer token', | ||
runner(setupKeystone, async ({ keystone, app }) => { | ||
await keystone.createItems(initialData); | ||
const context = keystone.createContext({ schemaName, skipAccessControl: true }); | ||
for (const [listKey, items] of Object.entries(initialData)) { | ||
await createItems({ keystone, listKey, items, context }); | ||
} | ||
const { token } = await login( | ||
app, | ||
initialData.User[0].email, | ||
initialData.User[0].password | ||
initialData.User[0].data.email, | ||
initialData.User[0].data.password | ||
); | ||
|
||
expect(token).toBeTruthy(); | ||
|
@@ -123,11 +136,14 @@ multiAdapterRunners().map(({ runner, adapterName }) => | |
test( | ||
'Allows access with cookie', | ||
runner(setupKeystone, async ({ keystone, app }) => { | ||
await keystone.createItems(initialData); | ||
const context = keystone.createContext({ schemaName, skipAccessControl: true }); | ||
for (const [listKey, items] of Object.entries(initialData)) { | ||
await createItems({ keystone, listKey, items, context }); | ||
} | ||
const { token } = await login( | ||
app, | ||
initialData.User[0].email, | ||
initialData.User[0].password | ||
initialData.User[0].data.email, | ||
initialData.User[0].data.password | ||
); | ||
|
||
expect(token).toBeTruthy(); | ||
|
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
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,18 +1,22 @@ | ||
module.exports = { | ||
User: [ | ||
{ | ||
name: 'Admin User', | ||
email: '[email protected]', | ||
isAdmin: true, | ||
dob: '1990-01-01', | ||
password: 'password', | ||
data: { | ||
name: 'Admin User', | ||
email: '[email protected]', | ||
isAdmin: true, | ||
dob: '1990-01-01', | ||
password: 'password', | ||
}, | ||
}, | ||
{ | ||
name: 'Demo User', | ||
email: '[email protected]', | ||
isAdmin: false, | ||
dob: '1995-06-09', | ||
password: 'password', | ||
data: { | ||
name: 'Demo User', | ||
email: '[email protected]', | ||
isAdmin: false, | ||
dob: '1995-06-09', | ||
password: 'password', | ||
}, | ||
}, | ||
], | ||
}; |
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,4 +1,5 @@ | ||
require('dotenv').config(); | ||
const { createItems } = require('@keystonejs/server-side-graphql-client'); | ||
|
||
// Lets not hardcode password, even for test data | ||
const password = process.env.INITIAL_DATA_PASSWORD; | ||
|
@@ -27,14 +28,54 @@ module.exports = async keystone => { | |
// Ensure a valid initial password is available to be used | ||
validatePassword(); | ||
// Drop the connected database to ensure no existing collections remain | ||
Object.values(keystone.adapters).forEach(async adapter => { | ||
await adapter.dropDatabase(); | ||
}); | ||
await Promise.all(Object.values(keystone.adapters).map(adapter => adapter.dropDatabase())); | ||
console.log('💾 Creating initial data...'); | ||
await keystone.createItems(initialData); | ||
await seedData(initialData, keystone); | ||
} | ||
}; | ||
|
||
async function seedData(intitialData, keystone) { | ||
/* 1. Insert the data which has no associated relationships | ||
* 2. Insert the data with the required relationships using connect | ||
*/ | ||
|
||
const users = await createItems({ | ||
keystone, | ||
listKey: 'User', | ||
items: initialData['User'].map(x => ({ data: x })), | ||
// name field is required for connect query for setting up Organiser list | ||
returnFields: 'id, name', | ||
}); | ||
|
||
await Promise.all( | ||
['Event', 'Talk', 'Rsvp', 'Sponsor'].map(list => | ||
createItems({ keystone, listKey: list, items: intitialData[list].map(x => ({ data: x })) }) | ||
) | ||
); | ||
|
||
// Preparing the Organiser list with connect nested mutation | ||
const organisers = Array(3) | ||
.fill(true) | ||
.map(createOrganisers(users)); | ||
|
||
// Run the GraphQL query to insert all the organisers | ||
await createItems({ keystone, listKey: 'Organiser', items: organisers }); | ||
} | ||
|
||
function createOrganisers(users) { | ||
return (_, i) => { | ||
return { | ||
data: { | ||
order: i + 1, | ||
role: 'Organiser', | ||
user: { | ||
connect: { id: users.find(user => user.name === `Organiser ${i + 1}`).id }, | ||
}, | ||
}, | ||
}; | ||
}; | ||
} | ||
|
||
const initialData = { | ||
User: [ | ||
{ name: 'Admin User', email: '[email protected]', isAdmin: true, password }, | ||
|
@@ -93,11 +134,6 @@ const initialData = { | |
password, | ||
}, | ||
], | ||
Organiser: [ | ||
{ user: { where: { name: 'Organiser 1' } }, order: 1, role: 'Organiser' }, | ||
{ user: { where: { name: 'Organiser 2' } }, order: 2, role: 'Organiser' }, | ||
{ user: { where: { name: 'Organiser 3' } }, order: 3, role: 'Organiser' }, | ||
], | ||
Event: [ | ||
{ | ||
name: 'Keystone Launch', | ||
|
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
Oops, something went wrong.