Skip to content

Commit

Permalink
more test fixes??
Browse files Browse the repository at this point in the history
  • Loading branch information
fabis94 committed Nov 15, 2024
1 parent 9938475 commit ea63fde
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 167 deletions.
4 changes: 3 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -623,8 +623,10 @@ jobs:
S3_REGION: '' # optional, defaults to 'us-east-1'
AUTOMATE_ENCRYPTION_KEYS_PATH: 'test/assets/automate/encryptionKeys.json'
FF_BILLING_INTEGRATION_ENABLED: 'true'
# These are the only 2 different env keys:
# These are the only different env keys:
MULTI_REGION_CONFIG_PATH: '../../.circleci/multiregion.test-ci.json'
FF_WORKSPACES_MODULE_ENABLED: 'true'
FF_WORKSPACES_MULTI_REGION_ENABLED: 'true'
RUN_TESTS_IN_MULTIREGION_MODE: true

test-frontend-2:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,188 +13,194 @@ import {
SetWorkspaceDefaultRegionDocument
} from '@/test/graphql/generated/graphql'
import { testApolloServer, TestApolloServer } from '@/test/graphqlHelper'
import { beforeEachContext } from '@/test/hooks'
import { beforeEachContext, getRegionKeys } from '@/test/hooks'
import { MultiRegionDbSelectorMock } from '@/test/mocks/global'
import { truncateRegionsSafely } from '@/test/speckle-helpers/regions'
import {
isMultiRegionTestMode,
truncateRegionsSafely
} from '@/test/speckle-helpers/regions'
import { Roles } from '@speckle/shared'
import { expect } from 'chai'

const storeRegion = storeRegionFactory({ db })
const isEnabled = isMultiRegionTestMode()

isEnabled
? describe('Workspace regions GQL', () => {
const region1Key = 'us-west-1'
const region2Key = 'eu-west-2'

let me: BasicTestUser
let otherGuy: BasicTestUser

const myFirstWorkspace: BasicTestWorkspace = {
id: '',
ownerId: '',
slug: '',
name: 'My first workspace'
}

let apollo: TestApolloServer

before(async () => {
MultiRegionDbSelectorMock.mockFunction('getDb', async () => db)
MultiRegionDbSelectorMock.mockFunction('getRegionDb', async () => db)

await beforeEachContext()

me = await createTestUser({ role: Roles.Server.Admin })
otherGuy = await createTestUser({ role: Roles.Server.User })

await Promise.all([
// Create first test workspace
createTestWorkspace(myFirstWorkspace, me),
// Create a couple of test regions
storeRegion({
region: {
key: region1Key,
name: 'US West 1'
}
}),
storeRegion({
region: {
key: region2Key,
name: 'EU West 2'
}
})
])

await Promise.all([
// Make otherGuy member of my workspace
assignToWorkspace(myFirstWorkspace, otherGuy)
])

apollo = await testApolloServer({ authUserId: me.id })
})

describe('Workspace regions GQL', () => {
const region1Key = 'us-west-1'
const region2Key = 'eu-west-2'

let me: BasicTestUser
let otherGuy: BasicTestUser

const myFirstWorkspace: BasicTestWorkspace = {
id: '',
ownerId: '',
slug: '',
name: 'My first workspace'
}
after(async () => {
MultiRegionDbSelectorMock.resetMockedFunctions()
await truncateRegionsSafely()
})

let apollo: TestApolloServer
describe('when listing', () => {
it("can't list if not workspace admin", async () => {
const res = await apollo.execute(
GetWorkspaceAvailableRegionsDocument,
{ workspaceId: myFirstWorkspace.id },
{ authUserId: otherGuy.id }
)

before(async () => {
MultiRegionDbSelectorMock.mockFunction('getDb', async () => db)
MultiRegionDbSelectorMock.mockFunction('getRegionDb', async () => db)
expect(res.data?.workspace.availableRegions).to.be.not.ok
expect(res).to.haveGraphQLErrors('You are not authorized')
})

await beforeEachContext()
it('can list if workspace admin', async () => {
const res = await apollo.execute(GetWorkspaceAvailableRegionsDocument, {
workspaceId: myFirstWorkspace.id
})

me = await createTestUser({ role: Roles.Server.Admin })
otherGuy = await createTestUser({ role: Roles.Server.User })
expect(res).to.not.haveGraphQLErrors()
expect(
res.data?.workspace.availableRegions.map((r) => r.key)
).to.deep.equalInAnyOrder([region1Key, region2Key, ...getRegionKeys()])
})
})

await Promise.all([
// Create first test workspace
createTestWorkspace(myFirstWorkspace, me),
// Create a couple of test regions
storeRegion({
region: {
key: region1Key,
name: 'US West 1'
}
}),
storeRegion({
region: {
key: region2Key,
name: 'EU West 2'
describe('when setting default region', () => {
const mySecondWorkspace: BasicTestWorkspace = {
id: '',
ownerId: '',
slug: '',
name: 'My second workspace'
}
})
])

await Promise.all([
// Make otherGuy member of my workspace
assignToWorkspace(myFirstWorkspace, otherGuy)
])

apollo = await testApolloServer({ authUserId: me.id })
})

after(async () => {
MultiRegionDbSelectorMock.resetMockedFunctions()
await truncateRegionsSafely()
})

describe('when listing', () => {
it("can't list if not workspace admin", async () => {
const res = await apollo.execute(
GetWorkspaceAvailableRegionsDocument,
{ workspaceId: myFirstWorkspace.id },
{ authUserId: otherGuy.id }
)

expect(res.data?.workspace.availableRegions).to.be.not.ok
expect(res).to.haveGraphQLErrors('You are not authorized')
})

it('can list if workspace admin', async () => {
const res = await apollo.execute(GetWorkspaceAvailableRegionsDocument, {
workspaceId: myFirstWorkspace.id
})
before(async () => {
await createTestWorkspace(mySecondWorkspace, me)
})

expect(res).to.not.haveGraphQLErrors()
beforeEach(async () => {
await db
.from(WorkspaceRegions.name)
.where({
[WorkspaceRegions.col.workspaceId]: mySecondWorkspace.id
})
.delete()
})

const regionKeys = res.data?.workspace.availableRegions.map((r) => r.key) || {}
expect(regionKeys).to.include(region1Key)
expect(regionKeys).to.include(region2Key)
})
})

describe('when setting default region', () => {
const mySecondWorkspace: BasicTestWorkspace = {
id: '',
ownerId: '',
slug: '',
name: 'My second workspace'
}

before(async () => {
await createTestWorkspace(mySecondWorkspace, me)
})
it("can't set default region if not workspace admin", async () => {
const res = await apollo.execute(
SetWorkspaceDefaultRegionDocument,
{ workspaceId: mySecondWorkspace.id, regionKey: region1Key },
{ authUserId: otherGuy.id }
)

beforeEach(async () => {
await db
.from(WorkspaceRegions.name)
.where({
[WorkspaceRegions.col.workspaceId]: mySecondWorkspace.id
expect(res).to.haveGraphQLErrors('You are not authorized')
expect(res.data?.workspaceMutations.setDefaultRegion).to.be.not.ok
})
.delete()
})

it("can't set default region if not workspace admin", async () => {
const res = await apollo.execute(
SetWorkspaceDefaultRegionDocument,
{ workspaceId: mySecondWorkspace.id, regionKey: region1Key },
{ authUserId: otherGuy.id }
)
it('can set default region if workspace admin', async () => {
const res = await apollo.execute(SetWorkspaceDefaultRegionDocument, {
workspaceId: mySecondWorkspace.id,
regionKey: region1Key
})

expect(res).to.haveGraphQLErrors('You are not authorized')
expect(res.data?.workspaceMutations.setDefaultRegion).to.be.not.ok
})

it('can set default region if workspace admin', async () => {
const res = await apollo.execute(SetWorkspaceDefaultRegionDocument, {
workspaceId: mySecondWorkspace.id,
regionKey: region1Key
expect(res).to.not.haveGraphQLErrors()
expect(
res.data?.workspaceMutations.setDefaultRegion.defaultRegion?.key
).to.equal(region1Key)
})
})

expect(res).to.not.haveGraphQLErrors()
expect(res.data?.workspaceMutations.setDefaultRegion.defaultRegion?.key).to.equal(
region1Key
)
})
})

describe('with existing default region', () => {
const myThirdWorkspace: BasicTestWorkspace = {
id: '',
ownerId: '',
slug: '',
name: 'My third workspace'
}

before(async () => {
await createTestWorkspace(myThirdWorkspace, me)
await apollo.execute(
SetWorkspaceDefaultRegionDocument,
{
workspaceId: myThirdWorkspace.id,
regionKey: region1Key
},
{ assertNoErrors: true }
)
})
describe('with existing default region', () => {
const myThirdWorkspace: BasicTestWorkspace = {
id: '',
ownerId: '',
slug: '',
name: 'My third workspace'
}

it("can't override default region", async () => {
const res = await apollo.execute(SetWorkspaceDefaultRegionDocument, {
workspaceId: myThirdWorkspace.id,
regionKey: region2Key
})
before(async () => {
await createTestWorkspace(myThirdWorkspace, me)
await apollo.execute(
SetWorkspaceDefaultRegionDocument,
{
workspaceId: myThirdWorkspace.id,
regionKey: region1Key
},
{ assertNoErrors: true }
)
})

expect(res).to.haveGraphQLErrors('Workspace already has a region assigned')
expect(res.data?.workspaceMutations.setDefaultRegion.defaultRegion).to.be.not.ok
})
it("can't override default region", async () => {
const res = await apollo.execute(SetWorkspaceDefaultRegionDocument, {
workspaceId: myThirdWorkspace.id,
regionKey: region2Key
})

it('can list default region if workspace admin', async () => {
const res = await apollo.execute(GetWorkspaceDefaultRegionDocument, {
workspaceId: myThirdWorkspace.id
})
expect(res).to.haveGraphQLErrors('Workspace already has a region assigned')
expect(res.data?.workspaceMutations.setDefaultRegion.defaultRegion).to.be.not
.ok
})

expect(res).to.not.haveGraphQLErrors()
expect(res.data?.workspace.defaultRegion?.key).to.equal(region1Key)
})
it('can list default region if workspace admin', async () => {
const res = await apollo.execute(GetWorkspaceDefaultRegionDocument, {
workspaceId: myThirdWorkspace.id
})

it("can't list default region if not workspace admin", async () => {
const res = await apollo.execute(
GetWorkspaceDefaultRegionDocument,
{ workspaceId: myThirdWorkspace.id },
{ authUserId: otherGuy.id }
)
expect(res).to.not.haveGraphQLErrors()
expect(res.data?.workspace.defaultRegion?.key).to.equal(region1Key)
})

it("can't list default region if not workspace admin", async () => {
const res = await apollo.execute(
GetWorkspaceDefaultRegionDocument,
{ workspaceId: myThirdWorkspace.id },
{ authUserId: otherGuy.id }
)

expect(res).to.haveGraphQLErrors('You are not authorized')
expect(res.data?.workspace.defaultRegion).to.be.not.ok
expect(res).to.haveGraphQLErrors('You are not authorized')
expect(res.data?.workspace.defaultRegion).to.be.not.ok
})
})
})
})
})
: void 0
Loading

0 comments on commit ea63fde

Please sign in to comment.