- 
                Notifications
    
You must be signed in to change notification settings  - Fork 8
 
Add ui test for Oauth (AST-89331) #1080
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            cx-elchanan-arbiv
  wants to merge
  18
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
feature/elchanan/add-ui-for-oauth-authentication-test
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +196
        
        
          −33
        
        
          
        
      
    
  
  
     Open
                    Changes from 10 commits
      Commits
    
    
            Show all changes
          
          
            18 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      3ec9706
              
                Add ui test for Oauth (AST-89331)
              
              
                 5d73860
              
                revert oauth in comment
              
              
                 afd8fc5
              
                fix login error messages are not disappearing
              
              
                 f0d517a
              
                 close server immediately if user cancels OAuth login
              
              
                 a6376b8
              
                check
              
              
                 a57516d
              
                fix ui test
              
              
                 52f40c0
              
                fix ui v2
              
              
                 72f8494
              
                Update 12.oauthAuthentication.test.ts
              
              
                 8ebcbac
              
                after cr add checks in test
              
              
                 1145308
              
                If not logged in, logged-in to check logout  flow
              
              
                 8f187d9
              
                after cr Split into multiple tests
              
              
                 3270b83
              
                skip  oauth tests
              
              
                 3e96582
              
                fix
              
              
                 b55d2f6
              
                Merge branch 'main' into feature/elchanan/add-ui-for-oauth-authentica…
              
              
                cx-elchanan-arbiv 91e70f9
              
                revert oauth
              
              
                 4723652
              
                Merge branch 'main' into feature/elchanan/add-ui-for-oauth-authentica…
              
              
                cx-elchanan-arbiv bba25a3
              
                Add offline_access scope to OAuth request to maintain token validity …
              
              
                 817250a
              
                Update auth.js
              
              
                 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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 hidden or 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 hidden or 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,218 @@ | ||
| import { | ||
| By, | ||
| EditorView, | ||
| VSBrowser, | ||
| WebDriver, | ||
| WebView, | ||
| Workbench | ||
| } from "vscode-extension-tester"; | ||
| import { expect } from "chai"; | ||
| import { retryTest, sleep } from "./utils/utils"; | ||
| 
     | 
||
| const CX_AUTHENTICATION_COMMAND = "ast-results.showAuth"; | ||
| 
     | 
||
| describe("Checkmarx OAuth Authentication Tests", () => { | ||
| let bench: Workbench; | ||
| let driver: WebDriver; | ||
| 
     | 
||
| before(async function () { | ||
| this.timeout(15000); | ||
| bench = new Workbench(); | ||
| driver = VSBrowser.instance.driver; | ||
| }); | ||
| 
     | 
||
| after(async () => { | ||
| // Inject a mock token into secrets before running tests using the new command | ||
| await bench.executeCommand("ast-results.mockTokenTest"); | ||
| // Short delay to allow the extension state to update | ||
| await new Promise((res) => setTimeout(res, 3000)); | ||
| await new EditorView().closeAllEditors(); | ||
| }); | ||
| 
     | 
||
| it("should open OAuth authentication panel and verify UI elements", retryTest(async function () { | ||
| console.log("========== Starting OAuth authentication test =========="); | ||
| 
     | 
||
| // Execute the authentication command | ||
| await bench.executeCommand(CX_AUTHENTICATION_COMMAND); | ||
| console.log("Authentication command executed"); | ||
| await sleep(5000); | ||
| 
     | 
||
| const editorView = new EditorView(); | ||
| await editorView.openEditor("Checkmarx One Authentication"); | ||
| console.log("Authentication editor opened"); | ||
| 
     | 
||
| // Switch to the WebView frame | ||
| const webView = new WebView(); | ||
| await webView.switchToFrame(10000); | ||
| console.log("Switched to WebView iframe"); | ||
| 
     | 
||
| try { | ||
| // Check if user is logged in | ||
| console.log("Checking if logout button exists..."); | ||
| let logoutElements = await webView.findWebElements(By.id("logoutButton")); | ||
| const isLoggedIn = logoutElements.length > 0 && await logoutElements[0].isDisplayed(); | ||
| 
     | 
||
| // If not logged in, inject a mock token to create a logged-in state | ||
| if (!isLoggedIn) { | ||
| console.log("User is not logged in - injecting mock token and reopening panel"); | ||
| await webView.switchBack(); | ||
| await bench.executeCommand("ast-results.mockTokenTest"); | ||
| await sleep(3000); | ||
| await new EditorView().closeAllEditors(); | ||
| await bench.executeCommand(CX_AUTHENTICATION_COMMAND); | ||
| await sleep(3000); | ||
| await new EditorView().openEditor("Checkmarx One Authentication"); | ||
| await webView.switchToFrame(5000); | ||
| logoutElements = await webView.findWebElements(By.id("logoutButton")); | ||
| 
     | 
||
| // Verify that we now have a logout button | ||
| const isLoggedInNow = logoutElements.length > 0 && await logoutElements[0].isDisplayed(); | ||
| console.log(`User logged in after token injection: ${isLoggedInNow}`); | ||
| if (!isLoggedInNow) { | ||
| console.log("Warning: Failed to log in user with mock token"); | ||
| } | ||
| } else { | ||
| console.log("User is already logged in"); | ||
| } | ||
| 
     | 
||
| // Now proceed with logout (should work whether we were already logged in or just injected a token) | ||
| console.log("Proceeding with logout test..."); | ||
| logoutElements = await webView.findWebElements(By.id("logoutButton")); | ||
| if (logoutElements.length > 0 && await logoutElements[0].isDisplayed()) { | ||
| console.log("Logging out user..."); | ||
| 
     | 
||
| // Switch back to main frame before clicking logout | ||
| await webView.switchBack(); | ||
| 
     | 
||
| // Switch back to WebView frame to click the logout button | ||
| await webView.switchToFrame(5000); | ||
| 
     | 
||
| await logoutElements[0].click(); | ||
| console.log("Clicked logout button"); | ||
| 
     | 
||
| // Switch back to main frame to handle the dialog | ||
| await webView.switchBack(); | ||
| await sleep(3000); // Longer wait time | ||
| 
     | 
||
| // Try all possible confirmation methods and log detailed information | ||
| await handleLogoutConfirmation(driver); | ||
| 
     | 
||
| // Switch back to WebView | ||
| await webView.switchToFrame(5000); | ||
| } else { | ||
| console.log("Error: Logout button not found even after token injection"); | ||
| } | ||
| 
     | 
||
| // Verify we're now logged out by checking the state of the form | ||
| console.log("Verifying logged out state..."); | ||
| const loginForm = await webView.findWebElements(By.id("loginForm")); | ||
| expect(loginForm.length).to.be.greaterThan(0, "Login form should be visible when logged out"); | ||
| 
     | 
||
| // Verify radio buttons | ||
| console.log("Finding radio buttons for authentication methods..."); | ||
| const radioButtons = await webView.findWebElements(By.css("input[type='radio']")); | ||
| console.log(`Found ${radioButtons.length} radio buttons`); | ||
| expect(radioButtons.length).to.be.at.least(2, "Should have at least 2 radio buttons (OAuth and API Key)"); | ||
| 
     | 
||
| // First, verify OAuth form fields | ||
| console.log("Selecting OAuth radio button..."); | ||
| const oauthRadio = await webView.findWebElement(By.css("input[name='authMethod'][value='oauth']")); | ||
| await driver.executeScript("arguments[0].click();", oauthRadio); | ||
| await sleep(1000); | ||
| 
     | 
||
| console.log("Verifying OAuth form fields..."); | ||
| const oauthForm = await webView.findWebElement(By.id("oauthForm")); | ||
| expect(oauthForm).to.not.be.undefined; | ||
| 
     | 
||
| const oauthFormText = await oauthForm.getText(); | ||
| console.log(`OAuth form text: ${oauthFormText}`); | ||
| 
     | 
||
| expect(oauthFormText).to.include("Checkmarx One Base URL:", "Base URL label should be present in OAuth form"); | ||
| expect(oauthFormText).to.include("Tenant Name:", "Tenant Name label should be present in OAuth form"); | ||
| 
     | 
||
| // Verify OAuth form input fields | ||
| const baseUriInput = await oauthForm.findElement(By.id("baseUri")); | ||
| expect(baseUriInput).to.not.be.undefined; | ||
| 
     | 
||
| const tenantInput = await oauthForm.findElement(By.id("tenant")); | ||
| expect(tenantInput).to.not.be.undefined; | ||
| 
     | 
||
| // Verify button state for OAuth (should be disabled when empty) | ||
| const authButton = await webView.findWebElement(By.id("authButton")); | ||
| let disabledAttr = await authButton.getAttribute("disabled"); | ||
| expect(disabledAttr).to.equal("true", "Auth button should be disabled when OAuth fields are empty"); | ||
| 
     | 
||
| // Now, verify API Key form fields | ||
| console.log("Selecting API Key radio button..."); | ||
| const apiKeyRadio = await webView.findWebElement(By.css("input[name='authMethod'][value='apiKey']")); | ||
| await driver.executeScript("arguments[0].click();", apiKeyRadio); | ||
| await sleep(1000); | ||
| 
     | 
||
| console.log("Verifying API Key form fields..."); | ||
| const apiKeyForm = await webView.findWebElement(By.id("apiKeyForm")); | ||
| expect(apiKeyForm).to.not.be.undefined; | ||
| 
     | 
||
| const apiKeyFormText = await apiKeyForm.getText(); | ||
| console.log(`API Key form text: ${apiKeyFormText}`); | ||
| 
     | 
||
| expect(apiKeyFormText).to.include("Checkmarx One API Key:", "API Key label should be present in API Key form"); | ||
| 
     | 
||
| // Verify API Key input field | ||
| const apiKeyInput = await apiKeyForm.findElement(By.id("apiKey")); | ||
| expect(apiKeyInput).to.not.be.undefined; | ||
| 
     | 
||
| // Verify button state for API Key (should be disabled when empty) | ||
| disabledAttr = await authButton.getAttribute("disabled"); | ||
| expect(disabledAttr).to.equal("true", "Auth button should be disabled when API Key field is empty"); | ||
| 
     | 
||
| console.log("OAuth UI verification completed successfully"); | ||
| } finally { | ||
| try { | ||
| await webView.switchBack(); | ||
| } catch (switchError) { | ||
| console.log("Error switching back:", switchError); | ||
| } | ||
| 
     | 
||
| try { | ||
| await new EditorView().closeAllEditors(); | ||
| } catch (closeError) { | ||
| console.log("Error closing editors:", closeError); | ||
| } | ||
| } | ||
| }, 3)); | ||
| 
     | 
||
| // Optimized function for handling logout confirmation | ||
| async function handleLogoutConfirmation(driver) { | ||
| try { | ||
| // Search directly for notifications - this is the approach that works | ||
| const notifications = await driver.findElements(By.className("notification-toast")); | ||
| console.log(`Found ${notifications.length} notifications`); | ||
| 
     | 
||
| for (const notification of notifications) { | ||
| // Check if this is the logout confirmation notification | ||
| const notificationText = await notification.getText(); | ||
| 
     | 
||
| if (notificationText.includes("Are you sure you want to log out?")) { | ||
| console.log("Found logout confirmation notification"); | ||
| 
     | 
||
| // Find the Yes button within this notification | ||
| const yesButton = await notification.findElement(By.css(".monaco-button")); | ||
| const buttonText = await yesButton.getText(); | ||
| 
     | 
||
| if (buttonText === "Yes") { | ||
| console.log("Clicking 'Yes' button in logout confirmation"); | ||
| await yesButton.click(); | ||
| await sleep(2000); | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| 
     | 
||
| console.log("Could not find logout confirmation notification"); | ||
| } catch (error) { | ||
| console.log("Error handling logout confirmation:", error); | ||
| } | ||
| 
     | 
||
| return false; | ||
| } | ||
| }); | ||
  
    
      This file contains hidden or 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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactor this test and extract code to separate function in order to make it shorter and more readble.
You also have some duplicate code that you can extract into separate function.
example for duplicated code:
await new EditorView().closeAllEditors();
await bench.executeCommand(CX_AUTHENTICATION_COMMAND);
await sleep(3000);
await new EditorView().openEditor("Checkmarx One Authentication");
await webView.switchToFrame(5000);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i Split into multiple tests and manage the duplicate code