Skip to content

Commit

Permalink
Drop deprecated method organization-membership.create
Browse files Browse the repository at this point in the history
Change-type: major
  • Loading branch information
myarmolinsky committed Dec 10, 2024
1 parent 5c2d34a commit cc753fb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 189 deletions.
30 changes: 0 additions & 30 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ const sdk = fromSharedOptions();
* [.get(membershipId, [options])](#balena.models.organization.membership.get) ⇒ <code>Promise</code>
* [.getAllByOrganization(handleOrId, [options])](#balena.models.organization.membership.getAllByOrganization) ⇒ <code>Promise</code>
* [.getAllByUser(usernameOrId, [options])](#balena.models.organization.membership.getAllByUser) ⇒ <code>Promise</code>
* ~~[.create(options)](#balena.models.organization.membership.create) ⇒ <code>Promise</code>~~
* [.changeRole(idOrUniqueKey, roleName)](#balena.models.organization.membership.changeRole) ⇒ <code>Promise</code>
* [.remove(id)](#balena.models.organization.membership.remove) ⇒ <code>Promise</code>
* [.invite](#balena.models.organization.invite) : <code>object</code>
Expand Down Expand Up @@ -742,7 +741,6 @@ balena.models.device.get(123).catch(function (error) {
* [.get(membershipId, [options])](#balena.models.organization.membership.get) ⇒ <code>Promise</code>
* [.getAllByOrganization(handleOrId, [options])](#balena.models.organization.membership.getAllByOrganization) ⇒ <code>Promise</code>
* [.getAllByUser(usernameOrId, [options])](#balena.models.organization.membership.getAllByUser) ⇒ <code>Promise</code>
* ~~[.create(options)](#balena.models.organization.membership.create) ⇒ <code>Promise</code>~~
* [.changeRole(idOrUniqueKey, roleName)](#balena.models.organization.membership.changeRole) ⇒ <code>Promise</code>
* [.remove(id)](#balena.models.organization.membership.remove) ⇒ <code>Promise</code>
* [.invite](#balena.models.organization.invite) : <code>object</code>
Expand Down Expand Up @@ -4704,7 +4702,6 @@ balena.models.key.create('Main', 'ssh-rsa AAAAB....').then(function(key) {
* [.get(membershipId, [options])](#balena.models.organization.membership.get) ⇒ <code>Promise</code>
* [.getAllByOrganization(handleOrId, [options])](#balena.models.organization.membership.getAllByOrganization) ⇒ <code>Promise</code>
* [.getAllByUser(usernameOrId, [options])](#balena.models.organization.membership.getAllByUser) ⇒ <code>Promise</code>
* ~~[.create(options)](#balena.models.organization.membership.create) ⇒ <code>Promise</code>~~
* [.changeRole(idOrUniqueKey, roleName)](#balena.models.organization.membership.changeRole) ⇒ <code>Promise</code>
* [.remove(id)](#balena.models.organization.membership.remove) ⇒ <code>Promise</code>
* [.invite](#balena.models.organization.invite) : <code>object</code>
Expand All @@ -4727,7 +4724,6 @@ balena.models.key.create('Main', 'ssh-rsa AAAAB....').then(function(key) {
* [.get(membershipId, [options])](#balena.models.organization.membership.get) ⇒ <code>Promise</code>
* [.getAllByOrganization(handleOrId, [options])](#balena.models.organization.membership.getAllByOrganization) ⇒ <code>Promise</code>
* [.getAllByUser(usernameOrId, [options])](#balena.models.organization.membership.getAllByUser) ⇒ <code>Promise</code>
* ~~[.create(options)](#balena.models.organization.membership.create) ⇒ <code>Promise</code>~~
* [.changeRole(idOrUniqueKey, roleName)](#balena.models.organization.membership.changeRole) ⇒ <code>Promise</code>
* [.remove(id)](#balena.models.organization.membership.remove) ⇒ <code>Promise</code>

Expand Down Expand Up @@ -4806,32 +4802,6 @@ balena.models.organization.membership.getAllByUser(123).then(function(membership
console.log(memberships);
});
```
<a name="balena.models.organization.membership.create"></a>

###### ~~membership.create(options) ⇒ <code>Promise</code>~~
***use balena.models.organization.invite.create instead***

This method adds a user to an organization by their usename.
WARNING: This method is deprecated, use balena.models.organization.invite.create instead.

**Kind**: static method of [<code>membership</code>](#balena.models.organization.membership)
**Summary**: Creates a new membership for an organization
**Access**: public
**Fulfil**: <code>Object</code> - organization membership

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | <code>Object</code> | | membership creation parameters |
| options.organization | <code>String</code> \| <code>Number</code> | | organization handle (string), or id (number) |
| options.username | <code>String</code> | | the username of the balena user that will become a member |
| [options.roleName] | <code>String</code> | <code>&quot;member&quot;</code> | the role name to be granted to the membership |

**Example**
```js
balena.models.organization.membership.create({ organization: "myorg", username: "user123", roleName: "member" }).then(function(membership) {
console.log(membership);
});
```
<a name="balena.models.organization.membership.changeRole"></a>

###### membership.changeRole(idOrUniqueKey, roleName) ⇒ <code>Promise</code>
Expand Down
54 changes: 0 additions & 54 deletions src/models/organization-membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ import type {
OrganizationMembershipRoles,
OrganizationMembershipTag,
PineOptions,
PineSubmitBody,
InjectedDependenciesParam,
PinePostResult,
} from '..';
import { mergePineOptions } from '../util';

Expand Down Expand Up @@ -241,58 +239,6 @@ const getOrganizationMembershipModel = function (
});
},

/**
* @summary Creates a new membership for an organization
* @name create
* @public
* @function
* @memberof balena.models.organization.membership
*
* @deprecated use balena.models.organization.invite.create instead
* @description This method adds a user to an organization by their usename.
* WARNING: This method is deprecated, use balena.models.organization.invite.create instead.
*
* @param {Object} options - membership creation parameters
* @param {String|Number} options.organization - organization handle (string), or id (number)
* @param {String} options.username - the username of the balena user that will become a member
* @param {String} [options.roleName="member"] - the role name to be granted to the membership
*
* @fulfil {Object} - organization membership
* @returns {Promise}
*
* @example
* balena.models.organization.membership.create({ organization: "myorg", username: "user123", roleName: "member" }).then(function(membership) {
* console.log(membership);
* });
*/
async create({
organization,
username,
roleName,
}: OrganizationMembershipCreationOptions): Promise<
PinePostResult<OrganizationMembership>
> {
const [{ id }, roleId] = await Promise.all([
getOrganization(organization, { $select: 'id' }),
roleName ? getRoleId(roleName) : undefined,
]);
type OrganizationMembershipBase = Omit<OrganizationMembership, 'user'>;
type OrganizationMembershipPostBody = OrganizationMembershipBase & {
username: string;
};
const body: PineSubmitBody<OrganizationMembershipPostBody> = {
username,
is_member_of__organization: id,
};
if (roleName) {
body.organization_membership_role = roleId;
}
return (await pine.post<OrganizationMembershipBase>({
resource: RESOURCE,
body,
})) as PinePostResult<OrganizationMembership>;
},

/**
* @summary Changes the role of an organization member
* @name changeRole
Expand Down
104 changes: 2 additions & 102 deletions tests/integration/models/organization-membership.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
credentials,
givenInitialOrganization,
givenLoggedInUser,
organizationRetrievalFields,
TEST_ORGANIZATION_NAME,
} from '../setup';
import type * as BalenaSdk from '../../..';
Expand Down Expand Up @@ -180,105 +179,10 @@ describe('Organization Membership Model', function () {
});
});

describe('balena.models.organization.membership.create()', function () {
before(function () {
ctx = this;
});

parallel('[read operations]', function () {
it(`should not be able to add a new member to the organization usign a wrong role name`, async function () {
const promise = balena.models.organization.membership.create({
organization: ctx.organization.id,
username: credentials.member.username,
// @ts-expect-error invalid value
roleName: 'unknown role',
});
await expect(promise).to.be.rejected.and.eventually.have.property(
'code',
'BalenaOrganizationMembershipRoleNotFound',
);
});

const randomOrdInfo = {
id: Math.floor(Date.now() / 1000),
handle: `random_sdk_test_org_handle_${Math.floor(Date.now() / 1000)}`,
};

organizationRetrievalFields.forEach((field) => {
it(`should not be able to add a new member when using an not existing organization ${field}`, async function () {
const promise = balena.models.organization.membership.create({
organization: randomOrdInfo[field],
username: credentials.member.username,
roleName: 'member',
});
await expect(promise).to.be.rejected.and.eventually.have.property(
'code',
'BalenaOrganizationNotFound',
);
});
});
});

describe('[mutating operations]', function () {
let membership:
| BalenaSdk.PinePostResult<BalenaSdk.OrganizationMembership>
| undefined;
afterEach(async function () {
await balena.models.organization.membership.remove(membership!.id);
});
organizationRetrievalFields.forEach(function (field) {
it(`should be able to add a new member to the organization by ${field}`, async function () {
membership = await balena.models.organization.membership.create({
organization: this.organization[field],
username: credentials.member.username,
});

expect(membership)
.to.be.an('object')
.that.has.nested.property('organization_membership_role.__id')
.that.equals(this.orgMemberRole.id);
});
});

it(`should be able to add a new member to the organization without providing a role`, async function () {
membership = await balena.models.organization.membership.create({
organization: this.organization.id,
username: credentials.member.username,
});

expect(membership)
.to.be.an('object')
.that.has.nested.property('organization_membership_role.__id')
.that.equals(this.orgMemberRole.id);
});

(['member', 'administrator'] as const).forEach(function (roleName) {
it(`should be able to add a new member to the organization with a given role [${roleName}]`, async function () {
membership = await balena.models.organization.membership.create({
organization: this.organization.id,
username: credentials.member.username,
roleName,
});

expect(membership)
.to.be.an('object')
.that.has.nested.property('organization_membership_role.__id')
.that.equals(this.orgRoleMap[roleName].id);
});
});
});
});

describe('given a member organization membership [contained scenario]', function () {
let membership:
| BalenaSdk.PinePostResult<BalenaSdk.OrganizationMembership>
| undefined;
beforeEach(async function () {
membership = await balena.models.organization.membership.create({
organization: this.organization.id,
username: credentials.member.username,
});
});

describe('balena.models.organization.membership.remove()', function () {
keyAlternatives.forEach(([title, keyGetter]) => {
Expand All @@ -297,7 +201,8 @@ describe('Organization Membership Model', function () {
});
});

describe('given an organization with an administrator organization membership [contained scenario]', function () {
// TODO: re-add this test in the future, we need to add a second user that is a member of the organization from the start
describe.skip('given an organization with an administrator organization membership [contained scenario]', function () {
const testOrg1Name = `${TEST_ORGANIZATION_NAME}_org_member_tests_${Date.now()}`;
let testOrg: BalenaSdk.PinePostResult<BalenaSdk.Organization> | undefined;
let membership:
Expand All @@ -308,11 +213,6 @@ describe('Organization Membership Model', function () {
testOrg = await balena.models.organization.create({
name: testOrg1Name,
});
membership = await balena.models.organization.membership.create({
organization: testOrg.id,
username: credentials.member.username,
roleName: 'administrator',
});
});

after(async function () {
Expand Down
90 changes: 87 additions & 3 deletions tests/integration/models/organizationInvite.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { expect } from 'chai';
import parallel from 'mocha.parallel';
import { balena, givenInitialOrganization, givenLoggedInUser } from '../setup';
import { timeSuite } from '../../util';
import { assertDeepMatchAndLength } from '../../util';
import {
balena,
givenInitialOrganization,
givenLoggedInUser,
credentials,
organizationRetrievalFields,
} from '../setup';
import { timeSuite, assertDeepMatchAndLength } from '../../util';
import type * as BalenaSdk from '../../..';
const TEST_EMAIL = '[email protected]';
const TEST_MESSAGE = 'Hey!, Join my org on balenaCloud';
const TEST_ROLE = 'member';
Expand Down Expand Up @@ -119,6 +125,84 @@ describe('Organization Invite Model', function () {
});
});
});

parallel('[read operations]', function () {
const randomOrdInfo = {
id: Math.floor(Date.now() / 1000),
handle: `random_sdk_test_org_handle_${Math.floor(Date.now() / 1000)}`,
};

organizationRetrievalFields.forEach((field) => {
it(`should not be able to invite a new member when using an not existing organization ${field}`, async function () {
const promise = balena.models.organization.invite.create(
randomOrdInfo[field],
{
invitee: credentials.member.email,
roleName: 'member',
},
);
await expect(promise).to.be.rejected.and.eventually.have.property(
'code',
'BalenaOrganizationNotFound',
);
});
});
});

describe('[mutating operations]', function () {
let membership:
| BalenaSdk.PinePostResult<BalenaSdk.OrganizationInvite>
| undefined;
afterEach(async function () {
await balena.models.organization.membership.remove(membership!.id);
});
organizationRetrievalFields.forEach(function (field) {
it(`should be able to invite a new member to the organization by ${field}`, async function () {
membership = await balena.models.organization.invite.create(
this.organization[field],
{
invitee: credentials.member.email,
},
);

expect(membership)
.to.be.an('object')
.that.has.nested.property('organization_membership_role.__id')
.that.equals(this.orgMemberRole.id);
});
});

it(`should be able to invite a new member to the organization without providing a role`, async function () {
membership = await balena.models.organization.invite.create(
this.organization.id,
{
invitee: credentials.member.email,
},
);

expect(membership)
.to.be.an('object')
.that.has.nested.property('organization_membership_role.__id')
.that.equals(this.orgMemberRole.id);
});

(['member', 'administrator'] as const).forEach(function (roleName) {
it(`should be able to invite a new member to the organization with a given role [${roleName}]`, async function () {
membership = await balena.models.organization.invite.create(
this.organization.id,
{
invitee: credentials.member.email,
roleName,
},
);

expect(membership)
.to.be.an('object')
.that.has.nested.property('organization_membership_role.__id')
.that.equals(this.orgRoleMap[roleName].id);
});
});
});
});

describe('given a single organization invite [contained scenario]', function () {
Expand Down

0 comments on commit cc753fb

Please sign in to comment.