2024-04-01 08:58:04 +00:00
|
|
|
|
|
|
|
const LoopBackContext = require('loopback-context');
|
2024-06-14 06:39:57 +00:00
|
|
|
const getAccessToken = (userId = 9) => {
|
|
|
|
return {accessToken: {userId}};
|
|
|
|
};
|
2024-04-01 08:58:04 +00:00
|
|
|
const DEFAULT_HEADERS = {headers: {origin: 'http://localhost'}};
|
2024-06-14 06:39:57 +00:00
|
|
|
const default_before_all = userId => {
|
|
|
|
return {
|
2024-04-01 08:58:04 +00:00
|
|
|
req: {
|
2024-06-14 06:39:57 +00:00
|
|
|
...getAccessToken(userId),
|
|
|
|
...DEFAULT_HEADERS,
|
|
|
|
...{__: value => value}
|
2024-04-01 08:58:04 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
args: {}
|
2024-06-14 06:39:57 +00:00
|
|
|
};
|
2024-04-01 08:58:04 +00:00
|
|
|
};
|
2024-06-14 06:39:57 +00:00
|
|
|
const default_loopback_ctx = userId => {
|
|
|
|
return {
|
|
|
|
...getAccessToken(userId),
|
|
|
|
...default_before_all(userId),
|
|
|
|
http: {
|
|
|
|
...default_before_all(userId)
|
|
|
|
},
|
|
|
|
args: {}
|
|
|
|
};
|
2024-04-01 08:58:04 +00:00
|
|
|
};
|
|
|
|
|
2024-06-14 06:39:57 +00:00
|
|
|
function vnBeforeAll() {
|
|
|
|
Object.assign(beforeAll, {getCtx: default_before_all});
|
|
|
|
Object.assign(beforeAll, {mockLoopBackContext});
|
2024-04-01 08:58:04 +00:00
|
|
|
}
|
2024-06-14 06:39:57 +00:00
|
|
|
function mockBeforeAll(value = default_before_all) {
|
2024-04-01 08:58:04 +00:00
|
|
|
const origin = beforeAll.ctx;
|
|
|
|
Object.assign(origin, value);
|
|
|
|
return origin;
|
|
|
|
}
|
|
|
|
|
2024-06-14 06:39:57 +00:00
|
|
|
const mockLoopBackContext = userId => {
|
|
|
|
const activeCtx = default_loopback_ctx(userId);
|
|
|
|
beforeAll(() => {
|
|
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
|
|
active: activeCtx
|
|
|
|
});
|
2024-04-01 08:58:04 +00:00
|
|
|
});
|
|
|
|
return activeCtx;
|
|
|
|
};
|
|
|
|
module.exports = {
|
|
|
|
mockBeforeAll, mockLoopBackContext
|
|
|
|
};
|
|
|
|
|
|
|
|
(function init() {
|
|
|
|
vnBeforeAll();
|
|
|
|
})();
|