Skip to content

Commit

Permalink
fixed backend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
holgerkoser committed Dec 11, 2024
1 parent d9fc675 commit 55cb23e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 32 deletions.
3 changes: 0 additions & 3 deletions backend/lib/io/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ async function userProfiles (req, res, next) {
try {
const [
canListProjects,
canGetSecrets,
] = await Promise.all([
authorization.canListProjects(req.user),
authorization.canListSecrets(req.user),
])
const profiles = Object.freeze({
canListProjects,
canGetSecrets,
})
Object.defineProperty(req.user, 'profiles', {
value: profiles,
Expand Down
1 change: 1 addition & 0 deletions backend/lib/io/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ function init (httpServer, cache) {

// handle 'subscribe' events
socket.on('subscribe', async (key, ...args) => {
logger.debug('Socket %s subscribed to %s', socket.id, key)
const done = args.pop()
try {
await dispatcher.subscribe(socket, key, ...args)
Expand Down
2 changes: 1 addition & 1 deletion backend/lib/watches/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const { get } = require('lodash')
const ioHelper = require('../io/helper')
const { isMemberOf } = require('../utils')

module.exports = (io, informer, options) => {
module.exports = (io, informer) => {
const nsp = io.of('/')

const handleEvent = async (type, newObject, oldObject) => {
Expand Down
4 changes: 2 additions & 2 deletions backend/test/acceptance/__snapshots__/io.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ exports[`api events when user is "admin" should subscribe shoots for a single cl
"code": 404,
"details": {
"group": "core.gardener.cloud",
"kind": "shoots",
"kind": "Shoot",
"uid": 2,
},
"kind": "Status",
Expand Down Expand Up @@ -150,7 +150,7 @@ exports[`api events when user is "admin" should subscribe shoots for a single na
"code": 404,
"details": {
"group": "core.gardener.cloud",
"kind": "shoots",
"kind": "Shoot",
"uid": 4,
},
"kind": "Status",
Expand Down
46 changes: 34 additions & 12 deletions backend/test/acceptance/io.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,25 @@ describe('api', function () {

describe('events', function () {
describe('when user is "foo"', () => {
const username = '[email protected]'
const user = fixtures.auth.createUser({
id: '[email protected]',
id: username,
})
let defaultRooms
let args

beforeEach(async () => {
// authorization check for `canListProjects`
mockRequest.mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess())
socket = await agent.connect({
cookie: await user.cookie,
})
defaultRooms = [
socket.id,
ioHelper.sha256(username),
]
expect(mockRequest).toHaveBeenCalledTimes(1)
mockRequest.mockClear()
})

it('should subscribe shoots for a single cluster', async function () {
Expand All @@ -140,7 +150,7 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

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

Expand Down Expand Up @@ -170,13 +180,13 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

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

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

Expand All @@ -191,7 +201,7 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

expect(getRooms(socket, nsp)).toEqual(new Set([
socket.id,
...defaultRooms,
'shoots;garden-foo',
'shoots;garden-bar',
]))
Expand All @@ -208,7 +218,7 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

expect(getRooms(socket, nsp)).toEqual(new Set([
socket.id,
...defaultRooms,
'shoots:unhealthy;garden-foo',
'shoots:unhealthy;garden-bar',
]))
Expand Down Expand Up @@ -254,14 +264,23 @@ describe('api', function () {
})

describe('when user is "admin"', () => {
const username = '[email protected]'
const user = fixtures.auth.createUser({
id: '[email protected]',
id: username,
})
let defaultRooms

beforeEach(async () => {
mockRequest.mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess())
socket = await agent.connect({
cookie: await user.cookie,
})
defaultRooms = [
socket.id,
ioHelper.sha256(username),
]
expect(mockRequest).toHaveBeenCalledTimes(1)
mockRequest.mockClear()
})

it('should subscribe shoots for a single cluster', async function () {
Expand All @@ -273,7 +292,7 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

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

Expand All @@ -290,7 +309,7 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

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

Expand All @@ -307,7 +326,7 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

expect(getRooms(socket, nsp)).toEqual(new Set([
socket.id,
...defaultRooms,
'shoots:admin',
]))

Expand All @@ -324,7 +343,7 @@ describe('api', function () {
expect(mockRequest.mock.calls).toMatchSnapshot()

expect(getRooms(socket, nsp)).toEqual(new Set([
socket.id,
...defaultRooms,
'shoots:unhealthy:admin',
]))
})
Expand Down Expand Up @@ -399,15 +418,18 @@ describe('api', function () {
})

it('should close the underlying connection', async function () {
const username = '[email protected]'
const options = {
id: '[email protected]',
id: username,
rti: 'abcdefg',
refresh_at: Math.ceil(Date.now() / 1000) + 4,
}
const user = fixtures.auth.createUser(options)
mockRequest.mockImplementationOnce(fixtures.auth.mocks.reviewSelfSubjectAccess())
socket = await agent.connect({
cookie: await user.cookie,
})
expect(mockRequest).toHaveBeenCalledTimes(1)
expect(mockSetDisconnectTimeout).toHaveBeenCalledTimes(1)
expect(mockSetDisconnectTimeout.mock.calls[0]).toEqual([
expect.objectContaining({
Expand Down
25 changes: 11 additions & 14 deletions backend/test/hooks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,32 +88,29 @@ describe('hooks', () => {
const server = {}
const ticketCache = {}
const ioInstance = {}
const keys = ['leases', 'shoots', 'projects']
let informers
let mockCreateInformers

beforeEach(() => {
informers = {
leases: {
run: jest.fn(),
store: {
untilHasSynced: Promise.resolve('leases'),
informers = keys.reduce((acc, key) => {
return Object.assign(acc, {
[key]: {
run: jest.fn(),
store: {
untilHasSynced: Promise.resolve(key),
},
},
},
shoots: {
run: jest.fn(),
store: {
untilHasSynced: Promise.resolve('shoots'),
},
},
}
})
}, {})
hooks.constructor.createInformers = mockCreateInformers = jest.fn(() => informers)
cache.initialize = jest.fn()
cache.getTicketCache = jest.fn(() => ticketCache)
io.mockReturnValue(ioInstance)
})

it('should create and run informers, create io instance and initialize cache and watches', async function () {
await expect(hooks.beforeListen(server)).resolves.toEqual(['leases', 'shoots'])
await expect(hooks.beforeListen(server)).resolves.toEqual(keys)

expect(mockCreateInformers).toHaveBeenCalledTimes(1)
expect(mockCreateInformers.mock.calls[0]).toHaveLength(1)
Expand Down

0 comments on commit 55cb23e

Please sign in to comment.