-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure MIS model name mismatch cannot cause unexpected behavior (#…
…403)
- Loading branch information
Showing
8 changed files
with
208 additions
and
34 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,5 @@ | ||
--- | ||
'@aws-amplify/data-schema': patch | ||
--- | ||
|
||
fix prevent mis model mismatch |
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
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
155 changes: 155 additions & 0 deletions
155
packages/integration-tests/__tests__/defined-behavior/3-exhaustive/mis-manipulation.ts
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,155 @@ | ||
import { a, ClientSchema } from '@aws-amplify/data-schema'; | ||
import { Amplify } from 'aws-amplify'; | ||
import { | ||
buildAmplifyConfig, | ||
mockedGenerateClient, | ||
optionsAndHeaders, | ||
expectGraphqlMatches, | ||
} from '../../utils'; | ||
|
||
const sampleTodo = { | ||
__typename: 'Todo', | ||
id: 'some-id', | ||
content: 'some content', | ||
description: 'something something', | ||
owner: 'some-body', | ||
done: false, | ||
updatedAt: '2024-03-01T19:05:44.536Z', | ||
createdAt: '2024-03-01T18:05:44.536Z', | ||
}; | ||
|
||
function manipulateMis(config: Awaited<ReturnType<typeof buildAmplifyConfig>>) { | ||
const todoDef = { ...config.modelIntrospection.models['Todo'] }; | ||
const notTodoDef = { ...config.modelIntrospection.models['NotTodo'] }; | ||
|
||
// swap definitions | ||
config.modelIntrospection.models['Todo'] = notTodoDef; | ||
config.modelIntrospection.models['NotTodo'] = todoDef; | ||
|
||
return config; | ||
} | ||
|
||
describe('Manipulated MIS cannot be used to call the wrong model', () => { | ||
const schema = a | ||
.schema({ | ||
Todo: a.model({ | ||
content: a.string(), | ||
}), | ||
NotTodo: a.model({ | ||
content: a.string(), | ||
}), | ||
}) | ||
.authorization((allow) => [allow.publicApiKey()]); | ||
type Schema = ClientSchema<typeof schema>; | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test('create performs operation against intended model', async () => { | ||
// #region mocking | ||
const { spy, generateClient } = mockedGenerateClient([ | ||
{ | ||
data: { | ||
createTodo: sampleTodo, | ||
}, | ||
}, | ||
]); | ||
|
||
// simulated amplifyconfiguration.json | ||
const original = await buildAmplifyConfig(schema); | ||
const config = manipulateMis(original); | ||
// #endregion mocking | ||
|
||
// App.tsx | ||
Amplify.configure(config); | ||
const client = generateClient<Schema>(); | ||
const { errors, data: newTodo } = await client.models.Todo.create({ | ||
content: 'My new todo', | ||
}); | ||
// #endregion | ||
|
||
// #region assertions | ||
expectGraphqlMatches( | ||
optionsAndHeaders(spy)[0][0].query, | ||
`mutation($input: CreateTodoInput!) { | ||
createTodo(input: $input) { id content createdAt updatedAt } | ||
}`, | ||
); | ||
expect(errors).toBeUndefined(); | ||
expect(newTodo).toEqual(sampleTodo); | ||
// #endregion assertions | ||
}); | ||
|
||
test('update performs operation against intended model', async () => { | ||
// #region mocking | ||
const { spy, generateClient } = mockedGenerateClient([ | ||
{ | ||
data: { | ||
updateTodo: sampleTodo, | ||
}, | ||
}, | ||
]); | ||
|
||
// simulated amplifyconfiguration.json | ||
const original = await buildAmplifyConfig(schema); | ||
const config = manipulateMis(original); | ||
// #endregion mocking | ||
|
||
Amplify.configure(config); | ||
const client = generateClient<Schema>(); | ||
const { data: updatedTodo, errors } = await client.models.Todo.update({ | ||
id: 'some_id', | ||
content: 'Updated content', | ||
}); | ||
// #endregion | ||
|
||
// #region assertions | ||
expectGraphqlMatches( | ||
optionsAndHeaders(spy)[0][0].query, | ||
`mutation($input: UpdateTodoInput!) { | ||
updateTodo(input: $input) { id content createdAt updatedAt } | ||
}`, | ||
); | ||
expect(errors).toBeUndefined(); | ||
expect(updatedTodo).toEqual(sampleTodo); | ||
// #endregion assertions | ||
}); | ||
|
||
test('delete performs operation against intended model', async () => { | ||
// #region mocking | ||
const { spy, generateClient } = mockedGenerateClient([ | ||
{ | ||
data: { | ||
deleteTodo: sampleTodo, | ||
}, | ||
}, | ||
]); | ||
|
||
// simulated amplifyconfiguration.json | ||
const original = await buildAmplifyConfig(schema); | ||
const config = manipulateMis(original); | ||
// #endregion mocking | ||
|
||
// #region | ||
Amplify.configure(config); | ||
const client = generateClient<Schema>(); | ||
const toBeDeletedTodo = { | ||
id: '123123213', | ||
}; | ||
const { data: deletedTodo, errors } = | ||
await client.models.Todo.delete(toBeDeletedTodo); | ||
// #endregion | ||
|
||
// #region assertions | ||
expectGraphqlMatches( | ||
optionsAndHeaders(spy)[0][0].query, | ||
`mutation($input: DeleteTodoInput!) { | ||
deleteTodo(input: $input) { id content createdAt updatedAt } | ||
}`, | ||
); | ||
expect(errors).toBeUndefined(); | ||
expect(deletedTodo).toEqual(sampleTodo); | ||
// #endregion assertions | ||
}); | ||
}); |