Skip to content

Commit

Permalink
tests: Add e2e for Harbor static admin login
Browse files Browse the repository at this point in the history
  • Loading branch information
crssnd committed Oct 17, 2023
1 parent bd3eff7 commit 865ab20
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tests/cypress.support.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,25 @@ Cypress.Commands.add("dexStaticUserLogin", function(username, loginUrl, cookieNa
)
})

// Available as cy.staticLogin("username", "password expression", "url for login", "path to test after login", "session cookie name")
Cypress.Commands.add('staticLogin', (username, passwordExpression, loginUrl, landingPath, cookieName) => {
cy.session([username, passwordExpression, loginUrl, landingPath, cookieName], () => {
// Available as cy.staticLogin("username", "password expression", "url for login", "username field name", "password field name", "path to test after login", "session cookie name")
Cypress.Commands.add('staticLogin', (username, passwordExpression, userField, passField, loginUrl, landingPath, cookieName) => {
cy.session([username, passwordExpression, userField, passField, loginUrl, landingPath, cookieName], () => {
cy.visit(loginUrl)

// this is needed for Harbor login via local db
cy.title().then(($title) => {
if ($title === 'Harbor') {
cy.get('[id="login-db"]').should('exist').click()
}
})
// ToDo the username label should be configurable
cy.get('input[name=user]').type(username)
cy.get(`input[name=${userField}]`).type(username)

cy.yqSecrets(passwordExpression)
.then(password => {
// {enter} causes the form to submit
// ToDo the password label should be configurable
cy.get('input[name=password]').type(`${password}{enter}`, { log: false })
cy.get(`input[name=${passField}]`).type(`${password}{enter}`, { log: false })
})
cy.url().should('include', landingPath)
},
Expand Down
17 changes: 17 additions & 0 deletions tests/end-to-end/harbor-admin-static-user.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe("harbor admin static user", function() {
before(function() {
cy.yq("sc", "\"https://\" + .harbor.subdomain + \".\" + .global.baseDomain")
.should("not.be.empty")
.as('baseUrl')
})
beforeEach(function() {
cy.staticLogin("admin", ".harbor.password", "login_username", "login_password", this.baseUrl + '/account/sign-in', '/harbor/projects', 'sid')
})

it('Harbor landing page is visible', function () {
cy.visit(this.baseUrl)
cy.get('[class="header-title"]').should('exist').and('contain', 'Projects')
cy.contains('button', 'admin').click()
})

})
83 changes: 83 additions & 0 deletions tests/end-to-end/harbor-create-user-project.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
describe("harbor create user project and robot account", function() {
before(function() {
cy.yq("sc", "\"https://\" + .harbor.subdomain + \".\" + .global.baseDomain")
.should("not.be.empty")
.as('baseUrl')
})

beforeEach(function() {
cy.viewport(2560, 2160)
})

it('Harbor login and create the dex static user', function () {
cy.dexStaticUserLogin("[email protected]", this.baseUrl, 'sid')
cy.visit(this.baseUrl)
cy.get('[class="header-title"]').should('exist').and('contain', 'Projects')
})

it('Harbor login with admin and promote the dex static user to Harbor admin', function () {
cy.staticLogin("admin", ".harbor.password", "login_username", "login_password", this.baseUrl + '/account/sign-in', '/harbor/projects', 'sid')
cy.visit(this.baseUrl)
cy.get('[class="header-title"]').should('exist').and('contain', 'Projects')
cy.contains('a', 'Users').click()
cy.contains('admin_static_user')
.parents('[class="datagrid-row ng-star-inserted"]')
.within(($row) => {
const userIsHarborAdmin = $row[0].querySelectorAll('clr-dg-cell')[1].innerText
cy.log(userIsHarborAdmin)
if (userIsHarborAdmin === 'Yes') {
cy.log("The admin_static_user is already Harbor admin")
} else {
cy.get('[type="checkbox"]').check({force: true})
cy.document().its('body').find('button').filter(':contains("SET AS ADMIN")').click()
cy.get('clr-dg-cell').eq(1).contains('Yes')
}
})
})

it('Harbor create dex static user project', function () {
cy.dexStaticUserLogin("[email protected]", this.baseUrl, 'sid')
cy.visit(this.baseUrl + '/harbor/projects')
cy.intercept("**/api/**").as("api")
cy.get('[class="header-title"]').should('exist').and('contain', 'Projects')
cy.wait(Array(7).fill('@api'))
cy.get('[class="datagrid-table"]')
.then(($table) => {
//this will match if other project names contain the demo_project name
const projectExists = $table.text().includes('cypress_demo_project')
if (projectExists) {
cy.log("The project was already created")
} else {
cy.contains('button', 'New Project').click()
cy.get('input[name="create_project_name"]').type('cypress_demo_project')
cy.contains('button', 'OK').click()
cy.contains('cypress_demo_project')
}
})
})

it('Harbor create robot account for dex static user', function () {
cy.dexStaticUserLogin("[email protected]", this.baseUrl, 'sid')
cy.visit(this.baseUrl + '/harbor/projects')
cy.intercept("**/api/**").as("api")
cy.get('[class="header-title"]').should('exist').and('contain', 'Projects')
cy.wait(Array(2).fill('@api'))
cy.contains('cypress_demo_project').click()
cy.contains('button', 'Robot Accounts').click()
cy.wait(Array(13).fill('@api'))
cy.get('[class="datagrid-table"]')
.then(($table) => {
//this will match if other robot account names contain this name
const robotAccountExists = $table.text().includes('cypress_robot')
if (robotAccountExists) {
cy.log("The robot account was already created")
} else {
cy.contains('button', 'NEW ROBOT ACCOUNT').click()
cy.get('input[name="name"]').type('cypress_robot')
cy.get('input[name="expiration"]').type('30')
cy.contains('button', 'ADD').click()
cy.contains('button', 'export to file')
}
})
})
})

0 comments on commit 865ab20

Please sign in to comment.