oso-cloud
Version:
Oso Cloud Node.js Client SDK
63 lines • 2.06 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ParityHandle = void 0;
/**
* A testing utility for comparing expected authorization
* decisions with actual results in Oso Migrate.
*/
class ParityHandle {
constructor() {
this.expected = null;
}
/**
* @internal
* Internal method called by the API class after authorize.
* @param {string | null}requestId The ID of the authorization request
* @param api Reference to the API instance
* @throws Error If request_id is set twice
*/
set(requestId, api) {
if (this.requestId) {
throw new Error("Attempted to set requestId twice. Only one request is allowed per ParityHandle instance.");
}
this.requestId = requestId;
this.api = api;
if (this.expected !== null) {
this.send();
}
}
/**
* Sets the expected result for this authorization check.
* This can be called either before or after the authorization check,
* but can only be called once per handle.
*
* @param expected The expected authorization result
* @throws @TypeError If expected is not a boolean
* @throws @Error If expected result is set twice
*/
expect(expected) {
if (this.expected != null) {
throw new Error(`Attempted to set expected result twice. (Original request ID: ${this.requestId})`);
}
this.expected = expected;
if (this.requestId != null) {
this.send();
}
}
/**
* Internal method to send the expectation to the API.
* @private
*/
send() {
if (!this.api || !this.requestId) {
throw new Error("API or requestId is not set. Cannot send expected result.");
}
const payload = {
request_id: this.requestId,
expected: this.expected,
};
this.api.postExpectedResult(payload);
}
}
exports.ParityHandle = ParityHandle;
//# sourceMappingURL=parity-handle.js.map