Skip to content

Commit

Permalink
fix organization-membership tests
Browse files Browse the repository at this point in the history
Change-type: patch
  • Loading branch information
myarmolinsky committed Jan 15, 2025
1 parent cf7122c commit 7b7f151
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 46 deletions.
6 changes: 3 additions & 3 deletions tests/integration/models/application-membership.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ describe('Application Membership Model', function () {
describe('balena.models.application.membership.remove()', function () {
keyAlternatives.forEach(([title, keyGetter]) => {
it(`should be able to remove a member by ${title}`, async function () {
const key = keyGetter(membership!);
const key = keyGetter(membership);

Check failure on line 159 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (windows-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.

Check failure on line 159 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (ubuntu-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.

Check failure on line 159 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (macos-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.
await balena.models.application.membership.remove(key);

const promise = balena.models.application.membership.get(
Expand Down Expand Up @@ -200,7 +200,7 @@ describe('Application Membership Model', function () {
[title, keyGetter]: (typeof keyAlternatives)[number],
) => {
it(`should be able to change an application membership to "${rolenName}" by ${title}`, async function () {
const key = keyGetter(membership!);
const key = keyGetter(membership);

Check failure on line 203 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (windows-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.

Check failure on line 203 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (ubuntu-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.

Check failure on line 203 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (macos-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.
await balena.models.application.membership.changeRole(key, rolenName);

membership = await balena.models.application.membership.get(
Expand Down Expand Up @@ -330,7 +330,7 @@ describe('Application Membership Model', function () {

keyAlternatives.forEach(([title, keyGetter]) => {
it(`should be able to retrieve a membership by ${title}`, async function () {
const key = keyGetter(membership!);
const key = keyGetter(membership);

Check failure on line 333 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (windows-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.

Check failure on line 333 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (ubuntu-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.

Check failure on line 333 in tests/integration/models/application-membership.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (macos-latest)

Argument of type 'ApplicationMembership | undefined' is not assignable to parameter of type 'ApplicationMembership'.
const result = await balena.models.application.membership.get(key, {
$select: 'id',
$expand: {
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/models/billing.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ describe('Billing Model', function () {
return balena.models.billing
.downloadInvoice(this.initialOrg.id, this.firstInvoiceNumber)
.then(function (result) {
const stream = result as Exclude<typeof result, Blob>;
const stream = result;
expect(stream.mime).to.equal('application/pdf');

Check failure on line 457 in tests/integration/models/billing.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (windows-latest)

Property 'mime' does not exist on type 'Blob | BalenaRequestStreamResult'.

Check failure on line 457 in tests/integration/models/billing.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (ubuntu-latest)

Property 'mime' does not exist on type 'Blob | BalenaRequestStreamResult'.

Check failure on line 457 in tests/integration/models/billing.spec.ts

View workflow job for this annotation

GitHub Actions / Flowzone / Test custom (macos-latest)

Property 'mime' does not exist on type 'Blob | BalenaRequestStreamResult'.

const tmpFile = tmp.tmpNameSync();
Expand Down
90 changes: 50 additions & 40 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,
TEST_ORGANIZATION_NAME,
} from '../setup';
import type * as BalenaSdk from '../../..';
import { assertDeepMatchAndLength, timeSuite } from '../../util';
Expand Down Expand Up @@ -64,18 +63,12 @@ describe('Organization Membership Model', function () {
});

describe('balena.models.organization.membership.getAllByOrganization()', function () {
it(`should return only the user's own membership`, async function () {
it(`should return the members of the organization`, async function () {
const memberships =
await balena.models.organization.membership.getAllByOrganization(
this.initialOrg.id,
);
assertDeepMatchAndLength(memberships, [
{
user: { __id: this.userId },
is_member_of__organization: { __id: this.initialOrg.id },
organization_membership_role: { __id: this.orgAdminRole.id },
},
]);
expect(memberships).to.be.an('array').with.length.greaterThan(0);
});
});

Expand All @@ -85,6 +78,16 @@ describe('Organization Membership Model', function () {
membership = (
await balena.models.organization.membership.getAllByOrganization(
this.initialOrg.id,
{
$filter: {
user: {
$any: {
$alias: 'u',
$expr: { u: { username: credentials.username } },
},
},
},
},
)
)[0];
});
Expand Down Expand Up @@ -126,22 +129,6 @@ describe('Organization Membership Model', function () {
});
});

describe('balena.models.organization.membership.getAllByOrganization()', function () {
it(`should return only the user's own membership`, async function () {
const memberships =
await balena.models.organization.membership.getAllByOrganization(
this.initialOrg.id,
);
assertDeepMatchAndLength(memberships, [
{
user: { __id: this.userId },
is_member_of__organization: { __id: this.initialOrg.id },
organization_membership_role: { __id: this.orgAdminRole.id },
},
]);
});
});

parallel(
'balena.models.organization.membership.getAllByUser()',
function () {
Expand Down Expand Up @@ -172,7 +159,23 @@ describe('Organization Membership Model', function () {
resource: 'organization_membership',
options: {
$filter: {
user: { $ne: this.userId },
$not: {
user: {
$any: {
$alias: 'u',
$expr: {
u: {
username: {
$in: [
credentials.username,
credentials.member.username,
],
},
},
},
},
},
},
is_member_of__organization: this.organization.id,
},
},
Expand Down Expand Up @@ -202,22 +205,28 @@ describe('Organization Membership Model', 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;
describe('given an organization with an administrator organization membership [contained scenario]', function () {
let membership:
| BalenaSdk.OrganizationMembership
| BalenaSdk.PinePostResult<BalenaSdk.OrganizationMembership>
| undefined;
before(async function () {
testOrg = await balena.models.organization.create({
name: testOrg1Name,
});
});

after(async function () {
await balena.models.organization.remove(testOrg!.id);
before(async function () {
membership = (
await balena.models.organization.membership.getAllByOrganization(
this.initialOrg.id,
{
$filter: {
user: {
$any: {
$alias: 'u',
$expr: { u: { username: credentials.member.username } },
},
},
},
},
)
)[0];
});

describe('balena.models.organization.membership.changeRole()', function () {
Expand Down Expand Up @@ -266,7 +275,7 @@ describe('Organization Membership Model', function () {
);
expect(membership).to.have.nested.property(
'is_member_of__organization[0].id',
testOrg!.id,
this.initialOrg.id,
);
expect(membership).to.have.nested.property(
'organization_membership_role[0].name',
Expand All @@ -281,7 +290,8 @@ describe('Organization Membership Model', function () {
});
});

describe('balena.models.organization.membership.remove()', 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('balena.models.organization.membership.remove()', function () {
it(`should be able to remove an administrator`, async function () {
await balena.models.organization.membership.remove(membership!.id);

Expand All @@ -297,7 +307,7 @@ describe('Organization Membership Model', function () {
it(`should not be able to remove the last membership of the organization`, async function () {
const [lastAdminMembership] =
await balena.models.organization.membership.getAllByOrganization(
testOrg!.id,
this.initialOrg.id,
{
$select: 'id',
$filter: { user: this.userId },
Expand Down
16 changes: 14 additions & 2 deletions tests/integration/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,25 @@ export async function loginPaidUser() {
}

async function resetInitialOrganization() {
const { id: userId } = await balena.auth.getUserInfo();
const initialOrg = await getInitialOrganization();
await balena.pine.delete({
resource: 'organization_membership',
options: {
$filter: {
user: { $ne: userId },
$not: {
user: {
$any: {
$alias: 'u',
$expr: {
u: {
username: {
$in: [credentials.username, credentials.member.username],
},
},
},
},
},
},
is_member_of__organization: initialOrg.id,
},
},
Expand Down

0 comments on commit 7b7f151

Please sign in to comment.