forked from gardener/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ServiceAccount Member Dashboard Access (gardener#871)
* allow all kind of members to access the dashboard (including ServiceAccount members) * keep old logic to allow serviceaccountusers to login * added tests * rm roles * Apply suggestions from code review Co-authored-by: Holger Koser <[email protected]> Co-authored-by: Holger Koser <[email protected]>
- Loading branch information
1 parent
3e634a3
commit fec8875
Showing
2 changed files
with
139 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// | ||
// SPDX-FileCopyrightText: 2020 SAP SE or an SAP affiliate company and Gardener contributors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
|
||
'use strict' | ||
|
||
const projects = require('../lib/services/projects') | ||
const cache = require('../lib/cache') | ||
const authorization = require('../lib/services/authorization') | ||
const nextTick = () => new Promise(process.nextTick) | ||
|
||
describe('services', function () { | ||
/* eslint no-unused-expressions: 0 */ | ||
const sandbox = sinon.createSandbox() | ||
|
||
afterEach(function () { | ||
sandbox.restore() | ||
}) | ||
|
||
describe('projects', function () { | ||
const projectList = [ | ||
{ | ||
spec: { | ||
members: [ | ||
{ | ||
kind: 'User', | ||
name: '[email protected]' | ||
}, | ||
{ | ||
kind: 'User', | ||
name: 'system:serviceaccount:garden-foo:robot-user' | ||
}, | ||
{ | ||
kind: 'ServiceAccount', | ||
name: 'robot-sa', | ||
namespace: 'garden-foo' | ||
} | ||
] | ||
}, | ||
metadata: { | ||
name: 'foo' | ||
}, | ||
status: { | ||
phase: 'Ready' | ||
} | ||
}, | ||
{ | ||
spec: { | ||
members: [ | ||
{ | ||
kind: 'User', | ||
name: '[email protected]' | ||
} | ||
] | ||
}, | ||
metadata: { | ||
name: 'bar' | ||
}, | ||
status: { | ||
phase: 'Ready' | ||
} | ||
}, | ||
{ | ||
spec: { | ||
members: [ | ||
{ | ||
apiGroup: 'rbac.authorization.k8s.io', | ||
kind: 'User', | ||
name: '[email protected]' | ||
} | ||
] | ||
}, | ||
metadata: { | ||
name: 'notReady' | ||
} | ||
} | ||
] | ||
|
||
const createUser = (username) => { | ||
return { | ||
id: username | ||
} | ||
} | ||
|
||
beforeEach(function () { | ||
sandbox.stub(cache, 'getProjects').returns(projectList) | ||
sandbox.stub(authorization, 'isAdmin').callsFake(async (user) => { | ||
await nextTick() | ||
if (user.id === '[email protected]') { | ||
return true | ||
} | ||
return false | ||
}) | ||
}) | ||
|
||
describe('#list', function () { | ||
it('should return all ready projects if user is admin', async function () { | ||
const userProjects = await projects.list({ user: createUser('[email protected]') }) | ||
expect(userProjects).to.have.length(2) | ||
}) | ||
|
||
it('should return project for user member', async function () { | ||
const userProjects = await projects.list({ user: createUser('system:serviceaccount:garden-foo:robot-user') }) | ||
expect(userProjects).to.have.length(1) | ||
expect(userProjects[0].metadata.name).to.eql('foo') | ||
}) | ||
|
||
it('should return project for serviceaccount user member', async function () { | ||
const userProjects = await projects.list({ user: createUser('system:serviceaccount:garden-foo:robot-user') }) | ||
expect(userProjects).to.have.length(1) | ||
expect(userProjects[0].metadata.name).to.eql('foo') | ||
}) | ||
|
||
it('should return project for serviceaccount member', async function () { | ||
const userProjects = await projects.list({ user: createUser('system:serviceaccount:garden-foo:robot-sa') }) | ||
expect(userProjects).to.have.length(1) | ||
expect(userProjects[0].metadata.name).to.eql('foo') | ||
}) | ||
|
||
it('should not return project if not in member list', async function () { | ||
const userProjects = await projects.list({ user: createUser('[email protected]') }) | ||
expect(userProjects).to.have.length(0) | ||
}) | ||
}) | ||
}) | ||
}) |