Skip to content

Commit

Permalink
fix: unit test error
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <suzhou@amazon.com>
  • Loading branch information
SuZhou-Joe committed Mar 4, 2024
1 parent 59cfdd6 commit fd19eb1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,6 @@ function defaultMapping(): IndexMapping {
},
},
},
workspaces: {
type: 'keyword',
},
permissions: {
properties: {
read: principals,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ describe('IndexMigrator', () => {
type: '2f4316de49999235636386fe51dc06c1',
updated_at: '00da57df13e94e9d98437d13ace4bfe0',
workspaces: '2f4316de49999235636386fe51dc06c1',
permissions: '07c04cdd060494956fdddaa7ef86e8ac',
},
},
properties: {
Expand All @@ -106,6 +107,40 @@ describe('IndexMigrator', () => {
},
},
workspaces: { type: 'keyword' },
permissions: {
properties: {
library_read: {
properties: {
users: { type: 'keyword' },
groups: { type: 'keyword' },
},
},
library_write: {
properties: {
users: { type: 'keyword' },
groups: { type: 'keyword' },
},
},
management: {
properties: {
users: { type: 'keyword' },
groups: { type: 'keyword' },
},
},
read: {
properties: {
users: { type: 'keyword' },
groups: { type: 'keyword' },
},
},
write: {
properties: {
users: { type: 'keyword' },
groups: { type: 'keyword' },
},
},
},
},
},
},
settings: { number_of_shards: 1, auto_expand_replicas: '0-1' },
Expand Down Expand Up @@ -138,7 +173,6 @@ describe('IndexMigrator', () => {
references: '7997cf5a56cc02bdc9c93361bde732b0',
type: '2f4316de49999235636386fe51dc06c1',
updated_at: '00da57df13e94e9d98437d13ace4bfe0',
workspaces: '2f4316de49999235636386fe51dc06c1',
},
},
properties: {
Expand All @@ -149,9 +183,6 @@ describe('IndexMigrator', () => {
originId: { type: 'keyword' },
type: { type: 'keyword' },
updated_at: { type: 'date' },
workspaces: {
type: 'keyword',
},
permissions: {
properties: {
library_read: {
Expand Down Expand Up @@ -294,7 +325,6 @@ describe('IndexMigrator', () => {
references: '7997cf5a56cc02bdc9c93361bde732b0',
type: '2f4316de49999235636386fe51dc06c1',
updated_at: '00da57df13e94e9d98437d13ace4bfe0',
workspaces: '2f4316de49999235636386fe51dc06c1',
},
},
properties: {
Expand All @@ -306,9 +336,6 @@ describe('IndexMigrator', () => {
originId: { type: 'keyword' },
type: { type: 'keyword' },
updated_at: { type: 'date' },
workspaces: {
type: 'keyword',
},
permissions: {
properties: {
library_read: {
Expand Down Expand Up @@ -394,7 +421,6 @@ describe('IndexMigrator', () => {
references: '7997cf5a56cc02bdc9c93361bde732b0',
type: '2f4316de49999235636386fe51dc06c1',
updated_at: '00da57df13e94e9d98437d13ace4bfe0',
workspaces: '2f4316de49999235636386fe51dc06c1',
},
},
properties: {
Expand All @@ -406,9 +432,6 @@ describe('IndexMigrator', () => {
originId: { type: 'keyword' },
type: { type: 'keyword' },
updated_at: { type: 'date' },
workspaces: {
type: 'keyword',
},
permissions: {
properties: {
library_read: {
Expand Down
106 changes: 0 additions & 106 deletions src/core/server/saved_objects/service/lib/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,74 +921,6 @@ describe('SavedObjectsRepository', () => {
const expectedError = expectErrorResult(obj3, { message: JSON.stringify(opensearchError) });
await bulkCreateError(obj3, opensearchError, expectedError);
});

it(`returns error when there is a conflict with an existing saved object according to workspaces`, async () => {
const obj = { ...obj3, workspaces: ['foo'] };
const response1 = {
status: 200,
docs: [
{
found: true,
_id: `${obj1.type}:${obj1.id}`,
_source: {
type: obj1.type,
workspaces: ['bar'],
},
},
{
found: true,
_id: `${obj.type}:${obj.id}`,
_source: {
type: obj.type,
workspaces: obj.workspaces,
},
},
{
found: true,
_id: `${obj2.type}:${obj2.id}`,
_source: {
type: obj2.type,
},
},
],
};
client.mget.mockResolvedValueOnce(
opensearchClientMock.createSuccessTransportRequestPromise(response1)
);
const response2 = getMockBulkCreateResponse([obj1, obj, obj2]);
client.bulk.mockResolvedValueOnce(
opensearchClientMock.createSuccessTransportRequestPromise(response2)
);

const options = { overwrite: true, workspaces: ['bar'] };
const result = await savedObjectsRepository.bulkCreate([obj1, obj, obj2], options);
expect(client.bulk).toHaveBeenCalled();
expect(client.mget).toHaveBeenCalled();

const body1 = {
docs: [
expect.objectContaining({ _id: `${obj1.type}:${obj1.id}` }),
expect.objectContaining({ _id: `${obj.type}:${obj.id}` }),
expect.objectContaining({ _id: `${obj2.type}:${obj2.id}` }),
],
};
expect(client.mget).toHaveBeenCalledWith(
expect.objectContaining({ body: body1 }),
expect.anything()
);
const body2 = [...expectObjArgs(obj1)];
expect(client.bulk).toHaveBeenCalledWith(
expect.objectContaining({ body: body2 }),
expect.anything()
);
expect(result).toEqual({
saved_objects: [
expectSuccess(obj1),
expectErrorConflict(obj, { metadata: { isNotOverwritable: true } }),
expectErrorConflict(obj2, { metadata: { isNotOverwritable: true } }),
],
});
});
});

describe('migration', () => {
Expand Down Expand Up @@ -2260,29 +2192,6 @@ describe('SavedObjectsRepository', () => {
);
});

it(`doesn't modify workspaces when overwrite without target workspaces`, async () => {
const response = getMockGetResponse({ workspaces: ['foo'], id });
client.get.mockResolvedValueOnce(
opensearchClientMock.createSuccessTransportRequestPromise(response)
);

await savedObjectsRepository.create('dashboard', attributes, {
id,
overwrite: true,
workspaces: [],
});

expect(client.index).toHaveBeenCalledWith(
expect.objectContaining({
id: `dashboard:${id}`,
body: expect.objectContaining({
workspaces: ['foo'],
}),
}),
expect.anything()
);
});

it(`accepts permissions property`, async () => {
await createSuccess(type, attributes, { id, permissions });
expect(client.create).toHaveBeenCalledWith(
Expand Down Expand Up @@ -2352,21 +2261,6 @@ describe('SavedObjectsRepository', () => {
expect(client.get).toHaveBeenCalled();
});

it(`throws error when there is a conflict with an existing workspaces saved object`, async () => {
const response = getMockGetResponse({ workspaces: ['foo'], id });
client.get.mockResolvedValueOnce(
opensearchClientMock.createSuccessTransportRequestPromise(response)
);
await expect(
savedObjectsRepository.create('dashboard', attributes, {
id,
overwrite: true,
workspaces: ['bar'],
})
).rejects.toThrowError(createConflictError('dashboard', id));
expect(client.get).toHaveBeenCalled();
});

it.todo(`throws when automatic index creation fails`);

it.todo(`throws when an unexpected failure occurs`);
Expand Down

0 comments on commit fd19eb1

Please sign in to comment.