Skip to content

Commit

Permalink
fix AuthSession and example.test.ts types
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed May 18, 2023
1 parent fec8685 commit 4d1e075
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
37 changes: 19 additions & 18 deletions examples/testing/example.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ beforeEach(async () => {
await resetDatabase(dbUrl, prismaSchemaPath);
});

test('Create a Person using the Query API', async () => {
test('Create a User using the Query API', async () => {
// We can use the context argument provided by the test runner to access
// the full context API.
const person = await context.query.Person.createOne({
data: { name: 'Alice', email: '[email protected]', password: 'super-secret' },
query: 'id name email password { isSet }',
const person = await context.query.User.createOne({
data: { name: 'Alice', password: 'dont-use-me' },
query: 'id name password { isSet }',
});
expect(person.name).toEqual('Alice');
expect(person.email).toEqual('[email protected]');
expect(person.password.isSet).toEqual(true);
});

Expand All @@ -31,16 +30,16 @@ test('Check that trying to create user with no name (required field) fails', asy
// error from an operation.
const { data, errors } = (await context.graphql.raw({
query: `mutation {
createPerson(data: { email: "[email protected]", password: "super-secret" }) {
id name email password { isSet }
createUser(data: { password: "dont-use-me" }) {
id name password { isSet }
}
}`,
})) as any;
expect(data!.createPerson).toBe(null);
expect(data!.createUser).toBe(null);
expect(errors).toHaveLength(1);
expect(errors![0].path).toEqual(['createPerson']);
expect(errors![0].path).toEqual(['createUser']);
expect(errors![0].message).toEqual(
'You provided invalid data for this operation.\n - Person.name: Name must not be empty'
'You provided invalid data for this operation.\n - User.name: Name must not be empty'
);
});

Expand All @@ -50,10 +49,10 @@ test('Check access control by running updateTask as a specific user via context.
// are behaving as expected.

// Create some users
const [alice, bob] = await context.query.Person.createMany({
const [alice, bob] = await context.query.User.createMany({
data: [
{ name: 'Alice', email: '[email protected]', password: 'super-secret' },
{ name: 'Bob', email: '[email protected]', password: 'super-secret' },
{ name: 'Alice', password: 'dont-use-me' },
{ name: 'Bob', password: 'dont-use-me' },
],
query: 'id name',
});
Expand Down Expand Up @@ -96,7 +95,7 @@ test('Check access control by running updateTask as a specific user via context.
{
// Check that we can update the task when logged in as Alice
const { data, errors } = (await context
.withSession({ itemId: alice.id, data: {} })
.withSession({ listKey: 'User', itemId: alice.id, data: {} })
.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
Expand All @@ -111,14 +110,16 @@ test('Check access control by running updateTask as a specific user via context.

// Check that we can't update the task when logged in as Bob
{
const { data, errors } = (await context.withSession({ itemId: bob.id, data: {} }).graphql.raw({
query: `mutation update($id: ID!) {
const { data, errors } = (await context
.withSession({ listKey: 'User', itemId: bob.id, data: {} })
.graphql.raw({
query: `mutation update($id: ID!) {
updateTask(where: { id: $id }, data: { isComplete: true }) {
id
}
}`,
variables: { id: task.id },
})) as any;
variables: { id: task.id },
})) as any;
expect(data!.updateTask).toBe(null);
expect(errors).toHaveLength(1);
expect(errors![0].path).toEqual(['updateTask']);
Expand Down
1 change: 1 addition & 0 deletions examples/testing/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Lists } from '.keystone/types';
export type Session = {
listKey: string;
itemId: string;
data: {};
};

function isAssignedUserFilter({ session }: { session?: Session }) {
Expand Down
9 changes: 5 additions & 4 deletions packages/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import { signinTemplate } from './templates/signin';
import { initTemplate } from './templates/init';

export type AuthSession = {
listKey: string;
itemId: string;
data: unknown;
listKey: string; // TODO: use ListTypeInfo
itemId: string | number; // TODO: use ListTypeInfo
data: unknown; // TODO: use ListTypeInfo
};

// TODO: use TypeInfo and listKey for types
/**
* createAuth function
*
Expand Down Expand Up @@ -195,7 +196,7 @@ export function createAuth<ListTypeInfo extends BaseListTypeInfo>({

try {
const data = await sudoContext.query[listKey].findOne({
where: { id: session.itemId },
where: { id: session.itemId as any }, // TODO: fix this
query: sessionData,
});
if (!data) return;
Expand Down

0 comments on commit 4d1e075

Please sign in to comment.