Skip to content

Commit

Permalink
Fix staging issues (#3026)
Browse files Browse the repository at this point in the history
* Add fixes to relationship handling from #1072

* Return link validation errors instead of throwing so they are shown in graphql response

* Update outdated query variables
  • Loading branch information
cesarvarela committed Aug 16, 2024
1 parent 592092f commit beb7ca6
Show file tree
Hide file tree
Showing 6 changed files with 352 additions and 74 deletions.
200 changes: 200 additions & 0 deletions site/gatsby-site/server/tests/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import { expect, jest, it } from '@jest/globals';
import { ApolloServer } from "@apollo/server";
import { makeRequest, seedFixture, startTestServer } from "./utils";
import * as context from '../context';

describe(`Utils`, () => {
let server: ApolloServer, url: string;

beforeAll(async () => {
({ server, url } = await startTestServer());
});

afterAll(async () => {
await server?.stop();
});

it(`Should return invalid linked id error - insertOne`, async () => {

const mutationData = {
query: `
mutation($data: IncidentInsertType!) {
insertOneIncident(data: $data) {
incident_id
reports {
report_number
}
}
}
`,
variables: {
"data": {
"incident_id": 9999,
"AllegedDeployerOfAISystem": {
"link": [
"entity-1"
]
},
"reports": {
"link": [
1
]
},
"date": "2029-01-01",
"title": "Test"
}
}

};

await seedFixture({
customData: {
users: [
{
userId: "123",
roles: ['admin'],
}
],
},
});


jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })

const response = await makeRequest(url, mutationData);

expect(response.body.data).toMatchObject({
insertOneIncident: null,
});
expect(response.body.errors).toMatchObject([{
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
},
"message": "Invalid linked ids: AllegedDeployerOfAISystem -> [entity-1]",
"path": [
"insertOneIncident",
],
}]);
});

// TODO: add insertMany tests once classifications are migrated to the new schema

it(`Should return invalid linked id error - updateMany`, async () => {

const mutationData = {
query: `
mutation($filter: IncidentFilterType!, $update: IncidentUpdateType!) {
updateManyIncidents(filter: $filter, update: $update) {
matchedCount
modifiedCount
}
}
`,
variables: {
"update": {
"set": {
"incident_id": 9999,
"reports": {
"link": [
45
]
},
"date": "2029-01-01",
"title": "Test"
}
},
"filter": {
"reports": {
"EQ": 1
}
}
}
};

await seedFixture({
customData: {
users: [
{
userId: "123",
roles: ['admin'],
}
],
},
});


jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })

const response = await makeRequest(url, mutationData);

expect(response.body.data).toMatchObject({
updateManyIncidents: null,
});
expect(response.body.errors).toMatchObject([{
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
},
"message": "Invalid linked ids: reports -> [45]",
"path": [
"updateManyIncidents",
],
}]);
});

it(`Should return invalid linked id error - updateOne`, async () => {

const mutationData = {
query: `
mutation($filter: SubmissionFilterType!, $update: SubmissionUpdateType!) {
updateOneSubmission(filter: $filter, update: $update) {
_id
user {
userId
}
}
}
`,
variables: {
"filter": { "_id": { "EQ": "66be58a854065d58fd99dbdb" } },
"update": {
"set": {
"user": {
"link": "2"
}
}
}
}
};

await seedFixture({
customData: {
users: [
{
userId: "123",
roles: ['admin'],
}
],
},
});


jest.spyOn(context, 'verifyToken').mockResolvedValue({ sub: "123" })

const response = await makeRequest(url, mutationData);

expect(response.body.data).toMatchObject({
updateOneSubmission: null,
});
expect(response.body.errors).toMatchObject([{
"extensions": {
"code": "INTERNAL_SERVER_ERROR",
},
"message": "Invalid linked id: user -> 2",
"path": [
"updateOneSubmission",
],
}]);
});

// TODO: add upsertOne tests once classifications are migrated to the new schema
});
Loading

0 comments on commit beb7ca6

Please sign in to comment.