Skip to content

Commit

Permalink
fix(api): Fix api context tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
SamTolmay committed Oct 27, 2021
1 parent 320c4a1 commit 8aa2642
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
18 changes: 17 additions & 1 deletion packages/api/src/context/createContext.test.js
Expand Up @@ -29,7 +29,10 @@ getSecrets.mockImplementation(() => ({ secret: true }));

createAuthorize.mockImplementation(({ authenticated, roles = [] }) => ({ authenticated, roles }));

createReadConfigFile.mockImplementation(({ configDirectory }) => () => ({ configDirectory }));
createReadConfigFile.mockImplementation(({ configDirectory }) => (path) => ({
configDirectory,
path,
}));

verifyAuthorizationHeader.mockImplementation(() => ({
authenticated: true,
Expand All @@ -42,6 +45,7 @@ test('createContext', async () => {
const context = contextFn({
headers: { header: 'header' },
host: 'host',
logger: 'logger',
protocol: 'https',
setHeader: 'setHeaderFunction',
});
Expand All @@ -56,11 +60,17 @@ test('createContext', async () => {
},
"config": Object {
"configDirectory": "configDirectory",
"path": "config.json",
},
"connectionTypes": Object {
"configDirectory": "configDirectory",
"path": "connectionTypes.json",
},
"headers": Object {
"header": "header",
},
"host": "host",
"logger": "logger",
"protocol": "https",
"readConfigFile": [Function],
"secrets": Object {
Expand All @@ -85,11 +95,17 @@ test('createContext', async () => {
},
"config": Object {
"configDirectory": "configDirectory",
"path": "config.json",
},
"connectionTypes": Object {
"configDirectory": "configDirectory",
"path": "connectionTypes.json",
},
"headers": Object {
"header": "header",
},
"host": "host",
"logger": "logger",
"protocol": "https",
"readConfigFile": [Function],
"secrets": Object {
Expand Down
36 changes: 17 additions & 19 deletions packages/api/src/routes/request/getConnectionPackage.js
Expand Up @@ -13,28 +13,26 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import { createRequire } from 'module';
import { ConfigurationError } from '../../context/errors';

async function getConnectionPackage({ connectionTypes, logger }, { connection, request }) {
const connectionTypeDefinition = connectionTypes[connection.type];
if (!connectionTypeDefinition) {
throw new ConfigurationError(
`Request "${request.requestId}" has undefined connection type "${connection.type}".`
);
}

const require = createRequire(import.meta.url);
const connectionPackage = require(connectionTypeDefinition.package);

logger.info(connectionPackage);

if (!connectionPackage) {
throw new ConfigurationError(
`Connection package "${connectionTypeDefinition.package}" could not be imported.`
);
}
return connectionPackage;
// const connectionTypeDefinition = connectionTypes[connection.type];
// if (!connectionTypeDefinition) {
// throw new ConfigurationError(
// `Request "${request.requestId}" has undefined connection type "${connection.type}".`
// );
// }

// const connectionPackage = await import(connectionTypeDefinition.package);

// logger.info(connectionPackage);

// if (!connectionPackage) {
// throw new ConfigurationError(
// `Connection package "${connectionTypeDefinition.package}" could not be imported.`
// );
// }
return null;
}

export default getConnectionPackage;

0 comments on commit 8aa2642

Please sign in to comment.