Skip to content

Commit

Permalink
fixed tests and renamed test folder
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerkoser committed Dec 13, 2024
1 parent 78bb742 commit 2373018
Show file tree
Hide file tree
Showing 52 changed files with 197 additions and 4 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
!backend/jest.setup.js
!backend/server.js
!backend/lib
!backend/test
!backend/__tests__
!backend/__fixtures__
!backend/__mocks__
Expand Down
2 changes: 1 addition & 1 deletion backend/__fixtures__/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const mocks = {
(id === '[email protected]' && namespace === 'garden-foo')
break
case 'projects':
allowed = id === '[email protected]'
allowed = id === '[email protected]' || id === '[email protected]'
break
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,36 @@ exports[`api events when user is "admin" should subscribe unhealthy shoots for a
]
`;

exports[`api events when user is "admin" should synchronize a project 1`] = `
[
{
"metadata": {
"name": "bar",
"resourceVersion": "42",
"uid": 2,
},
"spec": {
"createdBy": {
"apiGroup": "rbac.authorization.k8s.io",
"kind": "User",
"name": "[email protected]",
},
"description": "bar-description",
"namespace": "garden-bar",
"owner": {
"apiGroup": "rbac.authorization.k8s.io",
"kind": "User",
"name": "[email protected]",
},
"purpose": "bar-purpose",
},
"status": {
"phase": "Ready",
},
},
]
`;

exports[`api events when user is "foo" should subscribe shoots for a single cluster 1`] = `
[
[
Expand Down Expand Up @@ -343,6 +373,33 @@ exports[`api events when user is "foo" should subscribe shoots for a single name
]
`;

exports[`api events when user is "foo" should subscribe shoots for a single namespace 2`] = `
[
[
{
":authority": "kubernetes:6443",
":method": "post",
":path": "/apis/authorization.k8s.io/v1/selfsubjectaccessreviews",
":scheme": "https",
"authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImZvb0BleGFtcGxlLm9yZyIsImlhdCI6MTU3NzgzNjgwMCwiYXVkIjpbImdhcmRlbmVyIl0sImV4cCI6MzE1NTcxNjgwMCwianRpIjoianRpIn0.k3kGjF6AgugJLdwERXEWZPaibFAPFPOnmpT3YM9H0xU",
},
{
"apiVersion": "authorization.k8s.io/v1",
"kind": "SelfSubjectAccessReview",
"spec": {
"nonResourceAttributes": undefined,
"resourceAttributes": {
"group": "core.gardener.cloud",
"namespace": "garden-foo",
"resource": "shoots",
"verb": "list",
},
},
},
],
]
`;

exports[`api events when user is "foo" should subscribe shoots for all namespace 1`] = `
[
[
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,42 @@ describe('api', function () {
message: 'Invalid subscription type - baz',
}))
})

it('should subscribe shoots for a single namespace', async function () {
mockRequest.mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess())

await subscribe(socket, 'shoots', { namespace: 'garden-foo' })

expect(mockRequest).toHaveBeenCalledTimes(1)
expect(mockRequest.mock.calls).toMatchSnapshot()

expect(getRooms(socket, nsp)).toEqual(new Set([
...defaultRooms,
'shoots;garden-foo',
]))

await unsubscribe(socket, 'shoots')
expect(getRooms(socket, nsp)).toEqual(new Set([
...defaultRooms,
]))
})

it('should fail to synchronize a secret project', async function () {
const items = await synchronize(socket, 'projects', [6])
expect(items).toEqual([{
apiVersion: 'v1',
code: 404,
details: {
group: 'core.gardener.cloud',
kind: 'Project',
uid: 6,
},
kind: 'Status',
message: 'Project with uid 6 does not exist',
reason: 'NotFound',
status: 'Failure',
}])
})
})

describe('when user is "admin"', () => {
Expand Down Expand Up @@ -348,13 +384,18 @@ describe('api', function () {
]))
})

it('should fail to syncronize cats', async function () {
it('should fail to synchronize cats', async function () {
mockRequest.mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess())

await expect(synchronize(socket, 'cats', [42]))
.rejects
.toThrow('Invalid synchronization type - cats')
})

it('should synchronize a project', async function () {
const items = await synchronize(socket, 'projects', [2])
expect(items).toMatchSnapshot()
})
})
})

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
97 changes: 97 additions & 0 deletions backend/test/watches.spec.js → backend/__tests__/watches.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const watches = require('../lib/watches')
const cache = require('../lib/cache')
const tickets = require('../lib/services/tickets')
const SyncManager = require('../lib/github/SyncManager')
const { sha256 } = require('../lib/io/helper')

const { cloneDeep } = require('lodash')

const flushPromises = () => new Promise(setImmediate)

const rooms = new Map()

Expand Down Expand Up @@ -48,8 +53,45 @@ const nsp = {
emit: jest.fn(),
}

const sockets = [
{
id: 1,
data: {
user: {
id: '[email protected]',
profiles: {
canListProjects: true,
},
},
},
},
{
id: 2,
data: {
user: {
id: '[email protected]',
profiles: {
canListProjects: false,
},
},
},
},
{
id: 3,
data: {
user: {
id: '[email protected]',
profiles: {
canListProjects: false,
},
},
},
},
]

const io = {
of: jest.fn().mockReturnValue(nsp),
fetchSockets: jest.fn().mockResolvedValue(sockets),
}

describe('watches', function () {
Expand Down Expand Up @@ -163,6 +205,61 @@ describe('watches', function () {
})
})

describe('projects', function () {
it('should watch projects', async function () {
watches.projects(io, informer)

expect(io.of).toHaveBeenCalledTimes(1)
expect(io.of.mock.calls).toEqual([['/']])

const uid = 4
const bar = {
metadata: {
name: 'bar',
uid,
},
spec: {
members: [{
kind: 'User',
name: '[email protected]',
}],
},
}

informer.emit('add', bar)
const modifiedBar = cloneDeep(bar)
modifiedBar.spec.members = []
informer.emit('update', modifiedBar, bar)
informer.emit('delete', modifiedBar)

await flushPromises()

expect(logger.error).not.toHaveBeenCalled()

const ids = [
'[email protected]',
'[email protected]',
].map(sha256)
expect(Array.from(rooms.keys())).toEqual(ids)
expect(nsp.to).toHaveBeenCalledTimes(5)

const adminRoom = rooms.get(ids[0])
expect(adminRoom.emit).toHaveBeenCalledTimes(3)
expect(adminRoom.emit.mock.calls).toEqual([
['projects', { type: 'ADDED', uid }],
['projects', { type: 'MODIFIED', uid }],
['projects', { type: 'DELETED', uid }],
])

const fooRoom = rooms.get(ids[1])
expect(fooRoom.emit).toHaveBeenCalledTimes(2)
expect(fooRoom.emit.mock.calls).toEqual([
['projects', { type: 'ADDED', uid }],
['projects', { type: 'DELETED', uid }],
])
})
})

describe('leases', function () {
const metadata = {
projectName: 'foo',
Expand Down
1 change: 0 additions & 1 deletion backend/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ module.exports = [
'**/__fixtures__/**',
'**/__mocks__/**',
'**/__tests__/**',
'**/test/**',
'**/jest.setup.js',
],
plugins: {
Expand Down

0 comments on commit 2373018

Please sign in to comment.