Skip to content

Commit

Permalink
Merge branch 'staging' into fix/send-duplicated-notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
pdcp1 committed Dec 19, 2023
2 parents 8135efd + 80ec03d commit 261274b
Show file tree
Hide file tree
Showing 31 changed files with 480 additions and 137 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Artificial Intelligence Incident Database (AIID)

[![Netlify Status](https://api.netlify.com/api/v1/badges/9eb0dda2-916c-46f9-a0bd-9ddab3879c6e/deploy-status)](https://app.netlify.com/sites/aiid/deploys)
[![Slack Link](https://img.shields.io/badge/Join%20the%20RAIC%20Slack!-purple?logo=slack)](https://forms.gle/v7UHJvEkYSJQ7jHj7)

Information about the goals and organization of the AI Incident Database can be found on the [production website](https://incidentdatabase.ai/). This page concentrates on onboarding for the following types of contributions to the database,

Expand Down
149 changes: 68 additions & 81 deletions site/gatsby-site/cypress/e2e/integration/apps/checklistsForm.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ const { gql } = require('@apollo/client');
describe('Checklists App Form', () => {
const url = '/apps/checklists?id=testChecklist';

const defaultChecklist = {
__typename: 'Checklist',
about: '',
id: 'testChecklist',
name: 'Test Checklist',
owner_id: 'a-fake-user-id-that-does-not-exist',
risks: [],
tags_goals: [],
tags_methods: [],
tags_other: [],
};

const usersQuery = {
query: gql`
{
Expand All @@ -19,27 +31,36 @@ describe('Checklists App Form', () => {
timeout: 120000, // mongodb admin api is extremely slow
};

it('Should have read-only access for non-logged-in users', () => {
const withLogin = (callback) => {
cy.login(Cypress.env('e2eUsername'), Cypress.env('e2ePassword'));

cy.query(usersQuery).then(({ data: { users } }) => {
const user = users.find((user) => user.adminData.email == Cypress.env('e2eUsername'));

callback({ user });
});
};

const interceptFindChecklist = (checklist) => {
cy.conditionalIntercept(
'**/graphql',
(req) => req.body.operationName == 'findChecklist',
'findChecklist',
{
data: {
checklist: {
__typename: 'Checklist',
about: '',
id: 'testChecklist',
name: 'Test Checklist',
owner_id: 'a-fake-user-id-that-does-not-exist',
risks: [],
tags_goals: [],
tags_methods: [],
tags_other: [],
},
},
}
{ data: { checklist } }
);
};

const interceptUpsertChecklist = (checklist) => {
cy.conditionalIntercept(
'**/graphql',
(req) => req.body.operationName == 'upsertChecklist',
'upsertChecklist',
{ data: { checklist } }
);
};

it('Should have read-only access for non-logged-in users', () => {
interceptFindChecklist(defaultChecklist);

cy.visit(url);

Expand All @@ -55,26 +76,7 @@ describe('Checklists App Form', () => {
maybeIt('Should have read-only access for logged-in non-owners', () => {
cy.login(Cypress.env('e2eUsername'), Cypress.env('e2ePassword'));

cy.conditionalIntercept(
'**/graphql',
(req) => req.body.operationName == 'findChecklist',
'findChecklist',
{
data: {
checklist: {
__typename: 'Checklist',
about: '',
id: 'testChecklist',
name: 'Test Checklist',
owner_id: 'a-fake-user-id-that-does-not-exist',
risks: [],
tags_goals: [],
tags_methods: [],
tags_other: [],
},
},
}
);
interceptFindChecklist(defaultChecklist);

cy.visit(url);

Expand All @@ -88,51 +90,13 @@ describe('Checklists App Form', () => {
});

maybeIt('Should allow editing for owner', () => {
cy.login(Cypress.env('e2eUsername'), Cypress.env('e2ePassword'));

cy.query(usersQuery).then(({ data: { users } }) => {
const user = users.find((user) => user.adminData.email == Cypress.env('e2eUsername'));

cy.conditionalIntercept(
'**/graphql',
(req) => req.body.operationName == 'findChecklist',
'findChecklist',
{
data: {
checklist: {
__typename: 'Checklist',
about: '',
id: 'testChecklist',
name: 'Test Checklist',
owner_id: user.userId,
risks: [],
tags_goals: [],
tags_methods: [],
tags_other: [],
},
},
}
);
cy.conditionalIntercept(
'**/graphql',
(req) => req.body.operationName == 'upsertChecklist',
'upsertChecklist',
{
data: {
checklist: {
__typename: 'Checklist',
about: "It's a system that does something probably.",
id: 'testChecklist',
name: 'Test Checklist',
owner_id: user.userId,
risks: [],
tags_goals: [],
tags_methods: [],
tags_other: [],
},
},
}
);
withLogin(({ user }) => {
interceptFindChecklist({ ...defaultChecklist, owner_id: user.userId });
interceptUpsertChecklist({
...defaultChecklist,
owner_id: user.userId,
about: "It's a system that does something probably.",
});

cy.visit(url);

Expand All @@ -145,4 +109,27 @@ describe('Checklists App Form', () => {
cy.wait(['@upsertChecklist']);
});
});

maybeIt('Should trigger GraphQL update on removing tag', () => {
withLogin(({ user }) => {
interceptFindChecklist({
...defaultChecklist,
owner_id: user.userId,
tags_goals: ['GMF:Known AI Goal:Code Generation'],
});
interceptUpsertChecklist({});

cy.visit(url);

cy.get('[option="GMF:Known AI Goal:Code Generation"] .close').click();

cy.wait(['@upsertChecklist']).then((xhr) => {
expect(xhr.request.body.variables.checklist).to.deep.nested.include({
tags_goals: [],
});
});

cy.visit(url);
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
const { ObjectID } = require('bson');

const {
up,
} = require('../../../../migrations/2023.11.29T14.56.25.delete-duplicated-subscriptions');

describe('Migration Script - Remove Duplicated Subscriptions', () => {
it('Should remove duplicated subscriptions correctly', () => {
// Mocked data for all three cases
const testSubscriptions = {
incident: [
{
_id: {
type: 'incident',
userId: '642188372947d07020c1319d',
incident_id: 600,
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a3'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a4'),
],
},
{
_id: {
type: 'submission-promoted',
userId: '642188372947d07020c1319d',
incident_id: 600,
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a0'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a1'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a2'),
],
},
],
entity: [
{
_id: {
type: 'entity',
userId: '642188372947d07020c1319d',
entity_id: 'trans-women',
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a5'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a6'),
],
},
],
'new-incidents': [
{
_id: {
type: 'new-incidents',
userId: '642188372947d07020c1319d',
},
uniqueIds: [
new ObjectID('5f9f6b9b5f9c4c0001a3b3a7'),
new ObjectID('5f9f6b9b5f9c4c0001a3b3a8'),
],
},
],
};

// Mocked collection with stubbed methods
const subscriptionsCollection = {
aggregate: cy.stub().callsFake((query) => {
const type = query[0].$match.type.$in[0];

return {
toArray: cy.stub().resolves(testSubscriptions[type]),
};
}),
deleteMany: cy.stub().callsFake((query) => {
console.log('deleteMany called with:', query._id.$in[0].toString()); // Log the query
return Promise.resolve({ deletedCount: 1 });
}),
};

// Mocked context with database client
const context = {
client: {
db: cy.stub().returns({
collection: cy.stub().withArgs('subscriptions').returns(subscriptionsCollection),
}),
},
};

// Execute the migration script
cy.wrap(up({ context })).then(() => {
// Assertions for each case
const args = subscriptionsCollection.deleteMany.getCall(0).args[0];

const argsSubmissionPromoted = subscriptionsCollection.deleteMany.getCall(1).args[0];

const argsEntity = subscriptionsCollection.deleteMany.getCall(2).args[0];

const argsNewIncidents = subscriptionsCollection.deleteMany.getCall(3).args[0];

let modifiedObjectIncident = {
_id: {
$in: args._id.$in.map((id) => id.toString()),
},
};

let modifiedObjectSubmissionPromoted = {
_id: {
$in: argsSubmissionPromoted._id.$in.map((id) => id.toString()),
},
};

let modifiedObjectEntity = {
_id: {
$in: argsEntity._id.$in.map((id) => id.toString()),
},
};

let modifiedObjectNewIncidents = {
_id: {
$in: argsNewIncidents._id.$in.map((id) => id.toString()),
},
};

expect(modifiedObjectIncident).to.be.deep.equal({
_id: {
$in: testSubscriptions['incident'][0].uniqueIds.slice(0, 1).map((id) => id.toString()),
},
});

expect(modifiedObjectSubmissionPromoted).to.be.deep.equal({
_id: {
$in: testSubscriptions['incident'][1].uniqueIds.slice(0, 2).map((id) => id.toString()),
},
});

expect(modifiedObjectEntity).to.be.deep.equal({
_id: {
$in: testSubscriptions['entity'][0].uniqueIds.slice(0, 1).map((id) => id.toString()),
},
});

expect(modifiedObjectNewIncidents).to.be.deep.equal({
_id: {
$in: testSubscriptions['new-incidents'][0].uniqueIds
.slice(0, 1)
.map((id) => id.toString()),
},
});
});
});
});
Loading

0 comments on commit 261274b

Please sign in to comment.