|
| 1 | +/// <reference types="cypress" /> |
| 2 | + |
| 3 | +const contextPath = process.env.QDB_HTTP_CONTEXT_WEB_CONSOLE || "" |
| 4 | +const baseUrl = `http://localhost:9999${contextPath}`; |
| 5 | +const settingsUrl = `${baseUrl}/settings`; |
| 6 | + |
| 7 | +const oidcProviderUrl = "http://localhost:9032"; |
| 8 | +const oidcAuthorizationCodeUrl = `${oidcProviderUrl}/authorization`; |
| 9 | +const oidcTokenUrl = `${oidcProviderUrl}/token`; |
| 10 | + |
| 11 | +const interceptSettings = (payload) => { |
| 12 | + cy.intercept({ method: "GET", url: settingsUrl }, payload).as( |
| 13 | + "settings" |
| 14 | + ); |
| 15 | +}; |
| 16 | + |
| 17 | +const interceptAuthorizationCodeRequest = (redirectUrl) => { |
| 18 | + cy.intercept("GET", `${oidcAuthorizationCodeUrl}?**`, (req) => { |
| 19 | + req.redirect(redirectUrl); |
| 20 | + }).as('authorizationCode'); |
| 21 | +}; |
| 22 | + |
| 23 | +const interceptTokenRequest = (payload) => { |
| 24 | + cy.intercept({ method: "POST", url: oidcTokenUrl }, payload).as( |
| 25 | + "tokens" |
| 26 | + ); |
| 27 | +}; |
| 28 | + |
| 29 | +describe("OIDC authentication", () => { |
| 30 | + before(() => { |
| 31 | + // setup SSO group mappings |
| 32 | + cy.loadConsoleAsAdminAndCreateSSOGroup("group1"); |
| 33 | + }); |
| 34 | + |
| 35 | + beforeEach(() => { |
| 36 | + // load login page |
| 37 | + interceptSettings({ |
| 38 | + "release.type": "EE", |
| 39 | + "release.version": "1.2.3", |
| 40 | + "acl.enabled": true, |
| 41 | + "acl.basic.auth.realm.enabled": false, |
| 42 | + "acl.oidc.enabled": true, |
| 43 | + "acl.oidc.client.id": "client1", |
| 44 | + "acl.oidc.authorization.endpoint": oidcAuthorizationCodeUrl, |
| 45 | + "acl.oidc.token.endpoint": oidcTokenUrl, |
| 46 | + "acl.oidc.pkce.required": true, |
| 47 | + "acl.oidc.state.required": false, |
| 48 | + "acl.oidc.groups.encoded.in.token": false, |
| 49 | + }); |
| 50 | + cy.visit(baseUrl); |
| 51 | + |
| 52 | + cy.wait("@settings"); |
| 53 | + cy.getByDataHook("auth-login").should("be.visible"); |
| 54 | + cy.getByDataHook("button-sso-login").should("be.visible"); |
| 55 | + cy.getEditor().should("not.exist"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should login via OIDC", () => { |
| 59 | + interceptAuthorizationCodeRequest(`${baseUrl}?code=abcdefgh`); |
| 60 | + cy.getByDataHook("button-sso-login").click(); |
| 61 | + cy.wait("@authorizationCode"); |
| 62 | + |
| 63 | + interceptTokenRequest({ |
| 64 | + "access_token": "gslpJtzmmi6RwaPSx0dYGD4tEkom", |
| 65 | + "refresh_token": "FUuAAqMp6LSTKmkUd5uZuodhiE4Kr6M7Eyv", |
| 66 | + "id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6I", |
| 67 | + "token_type": "Bearer", |
| 68 | + "expires_in": 300 |
| 69 | + }); |
| 70 | + cy.wait("@tokens"); |
| 71 | + cy.getEditor().should("be.visible"); |
| 72 | + |
| 73 | + cy.executeSQL("select current_user();"); |
| 74 | + cy.getGridRow(0).should("contain", "user1"); |
| 75 | + |
| 76 | + cy.logout(); |
| 77 | + }); |
| 78 | + |
| 79 | + it("should force authentication if token expired, and there is no refresh token", () => { |
| 80 | + interceptAuthorizationCodeRequest(`${baseUrl}?code=abcdefgh`); |
| 81 | + cy.getByDataHook("button-sso-login").click(); |
| 82 | + cy.wait("@authorizationCode"); |
| 83 | + |
| 84 | + interceptTokenRequest({ |
| 85 | + "access_token": "gslpJtzmmi6RwaPSx0dYGD4tEkom", |
| 86 | + "id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6I", |
| 87 | + "token_type": "Bearer", |
| 88 | + "expires_in": 0 |
| 89 | + }); |
| 90 | + cy.wait("@tokens"); |
| 91 | + cy.getEditor().should("be.visible"); |
| 92 | + |
| 93 | + cy.reload(); |
| 94 | + cy.getByDataHook("button-log-in").should("be.visible"); |
| 95 | + |
| 96 | + cy.getByDataHook("button-log-in").click() |
| 97 | + cy.getEditor().should("be.visible"); |
| 98 | + }); |
| 99 | +}); |
0 commit comments