login.spec.ts 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { test, expect } from '@playwright/test';
  20. import { AuthPage } from '../../pages/AuthPage';
  21. import { URL } from '../../utils/urls';
  22. test.describe('Login view', () => {
  23. let authPage: AuthPage;
  24. test.beforeEach(async ({ page }: any) => {
  25. authPage = new AuthPage(page);
  26. await authPage.goto();
  27. await authPage.waitForLoginForm();
  28. });
  29. test('should redirect to login with incorrect username and password', async ({
  30. page,
  31. }: any) => {
  32. // Setup request interception before login attempt
  33. const loginRequestPromise = authPage.waitForLoginRequest();
  34. // Attempt login with incorrect credentials
  35. await authPage.loginWithCredentials('admin', 'wrongpassword');
  36. // Wait for login request and verify response
  37. const loginResponse = await loginRequestPromise;
  38. // Failed login returns 401 Unauthorized or 302 redirect to login
  39. expect([401, 302]).toContain(loginResponse.status());
  40. // Wait for redirect to complete before checking URL
  41. await page.waitForURL((url: any) => url.pathname.endsWith('login/'), {
  42. timeout: 10000,
  43. });
  44. // Verify we stay on login page
  45. const currentUrl = await authPage.getCurrentUrl();
  46. expect(currentUrl).toContain(URL.LOGIN);
  47. // Verify error message is shown
  48. const hasError = await authPage.hasLoginError();
  49. expect(hasError).toBe(true);
  50. });
  51. test('should login with correct username and password', async ({
  52. page,
  53. }: any) => {
  54. // Setup request interception before login attempt
  55. const loginRequestPromise = authPage.waitForLoginRequest();
  56. // Login with correct credentials
  57. await authPage.loginWithCredentials('admin', 'general');
  58. // Wait for login request and verify response
  59. const loginResponse = await loginRequestPromise;
  60. // Successful login returns 302 redirect
  61. expect(loginResponse.status()).toBe(302);
  62. // Wait for successful redirect to welcome page
  63. await page.waitForURL(
  64. (url: any) => url.pathname.endsWith('superset/welcome/'),
  65. {
  66. timeout: 10000,
  67. },
  68. );
  69. // Verify specific session cookie exists
  70. const sessionCookie = await authPage.getSessionCookie();
  71. expect(sessionCookie).not.toBeNull();
  72. expect(sessionCookie?.value).toBeTruthy();
  73. });
  74. });