pubnub
Version:
Publish & Subscribe Real-time Messaging with PubNub
197 lines (150 loc) • 6.1 kB
text/typescript
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import { binding, given, then, when } from 'cucumber-tsflow';
import { expect } from 'chai';
import { AccessManagerKeyset } from '../shared/keysets';
import { PubNub, PubNubManager } from '../shared/pubnub';
import {
tokenWithKnownAuthorizedUUID,
tokenWithUUIDPatternPermissions,
tokenWithUUIDResourcePermissions,
} from '../shared/fixtures';
import { ResourceType, AccessPermission } from '../shared/enums';
import { exists } from '../shared/helpers';
import { PAM } from '../../../lib/types';
class GrantTokenSteps {
private pubnub?: PubNub;
private token?: string;
private parsedToken?: PAM.Token;
private grantParams: Partial<PAM.GrantTokenParameters> = {};
private resourceName?: string;
private resourceType?: ResourceType;
constructor(
private manager: PubNubManager,
private keyset: AccessManagerKeyset,
) {}
public givenAuthorizedUUID(authorizedUUID: string) {
this.grantParams.authorized_uuid = authorizedUUID;
}
public givenTTL(ttl: number) {
this.grantParams.ttl = ttl;
}
public givenResourceAccess(name: string, type: ResourceType) {
this.resourceType = type;
this.resourceName = name;
this.grantParams.resources = {
...(this.grantParams.resources ?? {}),
[type]: {
...(this.grantParams.resources?.[type] ?? {}),
[name]: {},
},
};
}
public givenPatternAccess(name: string, type: ResourceType) {
this.resourceType = type;
this.resourceName = name;
this.grantParams.patterns = {
...(this.grantParams.patterns ?? {}),
[type]: {
...(this.grantParams.patterns?.[type] ?? {}),
[name]: {},
},
};
}
public givenGrantResourceAccessPermissions(permission: AccessPermission) {
exists(this.resourceType);
exists(this.resourceName);
exists(this.grantParams.resources?.[this.resourceType]?.[this.resourceName]);
this.grantParams.resources[this.resourceType]![this.resourceName][permission] = true;
}
public givenDenyResourceAccessPermissions(permission: AccessPermission) {
exists(this.resourceType);
exists(this.resourceName);
exists(this.grantParams.resources?.[this.resourceType]?.[this.resourceName]);
this.grantParams.resources[this.resourceType]![this.resourceName][permission] = false;
}
public givenGrantPatternAccessPermissions(permission: AccessPermission) {
exists(this.resourceType);
exists(this.resourceName);
exists(this.grantParams.patterns?.[this.resourceType]?.[this.resourceName]);
this.grantParams.patterns[this.resourceType]![this.resourceName][permission] = true;
}
public useAccessManagerKeyset(): void {
this.pubnub = this.manager.getInstance(this.keyset);
}
public useTokenWithKnownAuthorizedUUID() {
this.token = tokenWithKnownAuthorizedUUID;
}
public useTokenWithUUIDResourcePermissions() {
this.token = tokenWithUUIDResourcePermissions;
}
public useTokenWithUUIDPatternPermissions() {
this.token = tokenWithUUIDPatternPermissions;
}
public parseToken() {
exists(this.token);
exists(this.pubnub);
this.parsedToken = this.pubnub.parseToken(this.token);
expect(this.parsedToken).to.not.be.undefined;
}
public async grantToken() {
exists(this.grantParams);
const params = this.grantParams as PAM.GrantTokenParameters;
const token = await this.pubnub?.grantToken(params);
exists(token);
this.token = token;
this.parsedToken = this.pubnub?.parseToken(token);
}
public withPatternAccessPermissions(name: string, type: ResourceType) {
this.resourceName = name;
this.resourceType = type;
exists(this.parsedToken?.patterns?.[type]);
exists(this.parsedToken?.patterns?.[type]?.[name]);
}
public hasPatternAccessPermission(permission: AccessPermission) {
exists(this.resourceType);
exists(this.resourceName);
expect(this.parsedToken?.patterns?.[this.resourceType]?.[this.resourceName]?.[permission]).to.be.true;
}
public withResourceAccessPermissions(name: string, type: ResourceType) {
this.resourceName = name;
this.resourceType = type;
exists(this.parsedToken?.resources?.[type]);
exists(this.parsedToken?.resources?.[type]?.[name]);
}
public hasResourceAccessPermission(permission: AccessPermission) {
exists(this.resourceType);
exists(this.resourceName);
expect(this.parsedToken?.resources?.[this.resourceType]?.[this.resourceName]?.[permission]).to.be.true;
}
public hasTTL(ttl: number) {
expect(this.parsedToken?.ttl).to.equal(ttl);
}
public hasAuthorizedUUID(authorizedUUID: string) {
expect(this.parsedToken?.authorized_uuid).to.equal(authorizedUUID);
}
public doesntHaveAuthorizedUUID() {
expect(this.parsedToken?.authorized_uuid).to.be.undefined;
}
}
export = GrantTokenSteps;