From 9be9d85d5d48df586398c4efcdc1ce42756e6af9 Mon Sep 17 00:00:00 2001 From: RMidhunSuresh Date: Tue, 6 Sep 2022 01:39:17 +0530 Subject: [PATCH] Write test for SSO --- cypress/e2e/login.cy.ts | 52 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/cypress/e2e/login.cy.ts b/cypress/e2e/login.cy.ts index 36badebf..5ec34749 100644 --- a/cypress/e2e/login.cy.ts +++ b/cypress/e2e/login.cy.ts @@ -13,17 +13,27 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ +import type { DexInstance } from "../plugins/dex"; import type { SynapseInstance } from "../plugins/synapsedocker"; describe("Login", () => { let synapse: SynapseInstance; + let dex: DexInstance; beforeEach(() => { - cy.startSynapse("consent").then((data) => { - synapse = data; + cy.startDex().then((data) => { + dex = data; + cy.startSynapse("sso").then((data) => { + synapse = data; + }); }); }); + afterEach(() => { + cy.stopSynapse(synapse); + cy.stopDex(dex); + }) + it("Login using username/password", () => { const username = "foobaraccount"; const password = "password123"; @@ -35,5 +45,43 @@ describe("Login", () => { cy.contains("Log In").click(); cy.get(".SessionView"); }); + + it.only("Login using SSO", () => { + /** + * Add the homeserver to the localStorage manually; clicking on the start sso button would normally do this but we can't + * use two different origins in a single cypress test! + */ + cy.visit("/"); + cy.window().then(win => win.localStorage.setItem("hydrogen_setting_v1_sso_ongoing_login_homeserver", synapse.baseUrl)); + // Perform the SSO login manually using requests + const synapseAddress = synapse.baseUrl; + const dexAddress = dex.baseUrl; + // const dexAddress = `${Cypress.env("DEX_IP_ADDRESS")}:${Cypress.env("DEX_PORT")}`; + const redirectAddress = Cypress.config().baseUrl; + const ssoLoginUrl = `${synapseAddress}/_matrix/client/r0/login/sso/redirect?redirectUrl=${encodeURIComponent(redirectAddress)}`; + cy.request(ssoLoginUrl).then(response => { + // Request the Dex page + const dexPageHtml = response.body; + const loginWithExampleLink = Cypress.$(dexPageHtml).find(`a:contains("Log in with Example")`).attr("href"); + cy.log("Login with example link", loginWithExampleLink); + + // Proceed to next page + cy.request(`${dexAddress}${loginWithExampleLink}`).then(response => { + const secondDexPageHtml = response.body; + // This req token is used to approve this login in Dex + const req = Cypress.$(secondDexPageHtml).find(`input[name=req]`).attr("value"); + cy.log("req for sso login", req); + + // Next request will redirect us back to Synapse page with "Continue" link + cy.request("POST", `${dexAddress}/dex/approval?req=${req}&approval=approve`).then(response => { + const synapseHtml = response.body; + const hydrogenLinkWithToken = Cypress.$(synapseHtml).find(`a:contains("Continue")`).attr("href"); + cy.log("SSO redirect link", hydrogenLinkWithToken); + cy.visit(hydrogenLinkWithToken); + cy.get(".SessionView"); + }); + }); + }); + }) });