-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: enable tenant check on AMT operations
- Loading branch information
Showing
3 changed files
with
74 additions
and
4 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,66 @@ | ||
/********************************************************************* | ||
* Copyright (c) Intel Corporation 2022 | ||
* SPDX-License-Identifier: Apache-2.0 | ||
**********************************************************************/ | ||
|
||
import { createSpyObj } from '../test/helper/jest' | ||
import { ErrorResponse } from '../utils/amtHelper' | ||
import ciraMiddleware from './cira' | ||
import { devices } from '../server/mpsserver' | ||
|
||
describe('CIRA Middleware', () => { | ||
let req, next, resSpy | ||
|
||
beforeEach(() => { | ||
req = { | ||
params: { guid: 'some-guid' }, | ||
tenantId: null, | ||
deviceAction: null | ||
} | ||
next = jest.fn() | ||
resSpy = createSpyObj('Response', ['status', 'json', 'end', 'send']) | ||
resSpy.status.mockReturnThis() | ||
resSpy.json.mockReturnThis() | ||
resSpy.send.mockReturnThis() | ||
}) | ||
|
||
it('Should handle unauthorized tenant', async () => { | ||
// mock device | ||
const device: any = { ciraSocket: { readyState: 'open' }, tenantId: 'another-tenant' } | ||
req.tenantId = 'some-tenant' | ||
devices['some-guid'] = device | ||
|
||
await ciraMiddleware(req, resSpy, next) | ||
|
||
expect(resSpy.status).toHaveBeenCalledWith(401) | ||
expect(resSpy.json).toHaveBeenCalledWith(ErrorResponse(401, 'Unauthorized')) | ||
expect(resSpy.end).toHaveBeenCalled() | ||
expect(next).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('Should handle missing device', async () => { | ||
// no device for the provided guid | ||
delete devices['some-guid'] | ||
|
||
await ciraMiddleware(req, resSpy, next) | ||
|
||
expect(resSpy.status).toHaveBeenCalledWith(404) | ||
expect(resSpy.json).toHaveBeenCalledWith(ErrorResponse(404, 'guid : some-guid', 'device')) | ||
expect(resSpy.end).toHaveBeenCalled() | ||
expect(next).not.toHaveBeenCalled() | ||
}) | ||
|
||
// Similarly, add other test cases based on the conditions mentioned above... | ||
|
||
// For example: | ||
it('Should pass the middleware if multitenancy isnt used and ciraSocket is open', async () => { | ||
// mock device | ||
const device: any = { ciraSocket: { readyState: 'open' }, tenantId: 'some-tenant' } | ||
devices['some-guid'] = device | ||
|
||
await ciraMiddleware(req, resSpy, next) | ||
|
||
expect(req.deviceAction).toBeDefined() | ||
expect(next).toHaveBeenCalled() | ||
}) | ||
}) |
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