Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update all internal uses of createItems #3301

Merged
merged 12 commits into from
Jul 29, 2020
13 changes: 13 additions & 0 deletions .changeset/grumpy-coats-shake.md
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.
14 changes: 13 additions & 1 deletion api-tests/access-control/authed.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { multiAdapterRunners, authedGraphqlRequest } = require('@keystonejs/test-utils');
const { createItems } = require('@keystonejs/server-side-graphql-client');
const {
FAKE_ID,
FAKE_ID_2,
Expand Down Expand Up @@ -43,7 +44,18 @@ multiAdapterRunners().map(({ before, after, adapterName }) =>
}),
{}
);
items = await keystone.createItems(initialData);

items = {};
const context = keystone.createContext({ schemaName: 'internal' });
for (const [listKey, _items] of Object.entries(initialData)) {
items[listKey] = await createItems({
keystone,
listKey,
items: _items.map(x => ({ data: x })),
returnFields: 'id, name',
context,
});
}
});
afterAll(async () => {
await after(keystone);
Expand Down
42 changes: 29 additions & 13 deletions api-tests/auth-header.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
},
],
};
Expand Down Expand Up @@ -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 } }',
Expand All @@ -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();
Expand All @@ -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();
Expand Down
16 changes: 11 additions & 5 deletions api-tests/fields.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const globby = require('globby');
const path = require('path');
const { multiAdapterRunners, setupServer } = require('@keystonejs/test-utils');
const { createItems } = require('@keystonejs/server-side-graphql-client');

describe('Fields', () => {
const testModules = globby.sync(`packages/fields/src/types/**/test-fixtures.js`, {
Expand All @@ -11,23 +12,28 @@ describe('Fields', () => {
multiAdapterRunners().map(({ runner, adapterName }) =>
describe(`${adapterName} adapter`, () => {
testModules.map(require).forEach(mod => {
const listName = 'test';
const listKey = 'test';
const keystoneTestWrapper = (testFn = () => {}) =>
runner(
() => {
const createLists = keystone => {
// Create a list with all the fields required for testing
const fields = mod.getTestFields();

keystone.createList(listName, { fields });
keystone.createList(listKey, { fields });
};
return setupServer({ adapterName, createLists });
},
async ({ keystone, ...rest }) => {
// Populate the database before running the tests
await keystone.createItems({ [listName]: mod.initItems() });

return testFn({ keystone, listName, adapterName, ...rest });
const context = keystone.createContext({ schemaName: 'testing' });
await createItems({
keystone,
listKey,
items: mod.initItems().map(x => ({ data: x })),
context,
});
return testFn({ keystone, listKey, adapterName, ...rest });
}
);

Expand Down
1 change: 1 addition & 0 deletions api-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@keystonejs/adapter-mongoose": "*",
"@keystonejs/app-graphql": "*",
"@keystonejs/keystone": "*",
"@keystonejs/server-side-graphql-client": "*",
"@keystonejs/session": "*",
"body-parser": "^1.18.2",
"cookie-signature": "^1.1.0",
Expand Down
6 changes: 3 additions & 3 deletions api-tests/required.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ describe('Test isRequired flag for all field types', () => {
testModules.map(require).forEach(mod => {
describe(`Test isRequired flag for module: ${mod.name}`, () => {
const type = mod.type;
const listName = 'Test';
const listKey = 'Test';
const keystoneTestWrapper = (testFn = () => {}) =>
runner(
() =>
setupServer({
adapterName,
createLists: keystone => {
if (type.type === 'Select') {
keystone.createList(listName, {
keystone.createList(listKey, {
fields: {
name: { type: Text },
testField: {
Expand All @@ -33,7 +33,7 @@ describe('Test isRequired flag for all field types', () => {
},
});
} else {
keystone.createList(listName, {
keystone.createList(listKey, {
fields: {
name: { type: Text },
testField: { type, isRequired: true },
Expand Down
3 changes: 2 additions & 1 deletion demo-projects/blog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { GraphQLApp } = require('@keystonejs/app-graphql');
const { AdminUIApp } = require('@keystonejs/app-admin-ui');
const { NextApp } = require('@keystonejs/app-next');
const { StaticApp } = require('@keystonejs/app-static');
const { createItems } = require('@keystonejs/server-side-graphql-client');

const { staticRoute, staticPath, distDir } = require('./config');
const { User, Post, PostCategory, Comment } = require('./schema');
Expand All @@ -18,7 +19,7 @@ const keystone = new Keystone({
const users = await keystone.lists.User.adapter.findAll();
if (!users.length) {
const initialData = require('./initialData');
await keystone.createItems(initialData);
await createItems({ keystone, listKey: 'User', items: initialData.User });
}
},
});
Expand Down
24 changes: 14 additions & 10 deletions demo-projects/blog/initialData.js
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',
},
},
],
};
1 change: 1 addition & 0 deletions demo-projects/blog/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@keystonejs/file-adapters": "^7.0.2",
"@keystonejs/keystone": "^12.0.0",
"@keystonejs/oembed-adapters": "^5.1.4",
"@keystonejs/server-side-graphql-client": "^0.0.0",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
"apollo-upload-client": "^13.0.0",
Expand Down
54 changes: 45 additions & 9 deletions demo-projects/meetup/initialData.js
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;
Expand Down Expand Up @@ -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 },
Expand Down Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions demo-projects/meetup/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@keystonejs/fields-wysiwyg-tinymce": "^5.3.3",
"@keystonejs/file-adapters": "^7.0.2",
"@keystonejs/keystone": "^12.0.0",
"@keystonejs/server-side-graphql-client": "^0.0.0",
"@keystonejs/session": "^8.0.0",
"apollo-cache-inmemory": "^1.6.6",
"apollo-client": "^2.6.10",
Expand Down
6 changes: 3 additions & 3 deletions packages/fields/src/types/Decimal/test-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export const filterTests = withKeystone => {

export const crudTests = withKeystone => {
const withHelpers = wrappedFn => {
return async ({ keystone, listName, ...rest }) => {
const list = keystone.getListByKey(listName);
return async ({ keystone, listKey, ...rest }) => {
const list = keystone.getListByKey(listKey);
const { listQueryName } = list.gqlNames;
const {
data: { [listQueryName]: items },
Expand All @@ -132,7 +132,7 @@ export const crudTests = withKeystone => {
}`,
});
expect(errors).toBe(undefined);
return wrappedFn({ keystone, listName, ...rest, list, items, ...list.gqlNames });
return wrappedFn({ keystone, listKey, ...rest, list, items, ...list.gqlNames });
};
};

Expand Down
1 change: 1 addition & 0 deletions test-projects/access-control/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@keystonejs/auth-password": "^5.1.11",
"@keystonejs/fields": "^14.0.0",
"@keystonejs/keystone": "^12.0.0",
"@keystonejs/server-side-graphql-client": "^0.0.0",
"@keystonejs/utils": "^5.4.1",
"cross-env": "^7.0.0",
"express": "^4.17.1"
Expand Down
Loading