This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Adding some MultiUser tests #334
Merged
Merged
Changes from all commits
Commits
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
106 changes: 106 additions & 0 deletions
106
test/GitHubExtension/DeveloperId/CredentialVaultTests.cs
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,106 @@ | ||
| // Copyright (c) Microsoft Corporation and Contributors | ||
| // Licensed under the MIT license. | ||
|
|
||
| using System.Net; | ||
| using GitHubExtension.DeveloperId; | ||
|
|
||
| namespace GitHubExtension.Test; | ||
|
|
||
| // Unit Tests for CredentialVault | ||
| public partial class DeveloperIdTests | ||
| { | ||
| [TestMethod] | ||
| [TestCategory("Unit")] | ||
| public void CredentialVault_CreateSingleton() | ||
| { | ||
| var credentialVault1 = new CredentialVault("DevHomeGitHubExtensionTest"); | ||
| Assert.IsNotNull(credentialVault1); | ||
|
|
||
| var credentialVault2 = new CredentialVault("DevHomeGitHubExtensionTest"); | ||
| Assert.IsNotNull(credentialVault2); | ||
|
|
||
| Assert.AreNotEqual(credentialVault1, credentialVault2); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The test name |
||
|
|
||
| credentialVault1.RemoveAllCredentials(); | ||
| Assert.AreEqual(0, credentialVault1.GetAllCredentials().Count()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("LiveData")] | ||
| [DataRow("testuser1")] | ||
| [DataRow("https://github.com/testuser2")] | ||
| [DataRow("https://RandomWebServer.example/testuser3")] | ||
| public void CredentialVault_SaveAndRetrieveCredential(string loginId) | ||
| { | ||
| var credentialVault = new CredentialVault("DevHomeGitHubExtensionTest"); | ||
| Assert.IsNotNull(credentialVault); | ||
| Assert.AreEqual(0, credentialVault.GetAllCredentials().Count()); | ||
|
|
||
| var nullCredential = credentialVault.GetCredentials(loginId); | ||
| Assert.IsNull(nullCredential); | ||
|
|
||
| var testCredential = "testcredential"; | ||
|
|
10BC0
||
| var credential = new NetworkCredential(null, testCredential).SecurePassword; | ||
| credentialVault.SaveCredentials(loginId, credential); | ||
|
|
||
| var retrievedCredential = credentialVault.GetCredentials(loginId); | ||
| Assert.IsNotNull(retrievedCredential); | ||
| Assert.AreEqual(testCredential, retrievedCredential.Password); | ||
|
|
||
| credentialVault.RemoveAllCredentials(); | ||
| Assert.AreEqual(0, credentialVault.GetAllCredentials().Count()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("LiveData")] | ||
| [DataRow("testuser1")] | ||
| [DataRow("https://github.com/testuser2")] | ||
| [DataRow("https://RandomWebServer.example/testuser3")] | ||
| public void CredentialVault_RemoveAndRetrieveCredential(string loginId) | ||
| { | ||
| var credentialVault = new CredentialVault("DevHomeGitHubExtensionTest"); | ||
| Assert.IsNotNull(credentialVault); | ||
|
|
||
| var testCredential = "testCredential"; | ||
|
|
||
| var credential = new NetworkCredential(null, testCredential).SecurePassword; | ||
| credentialVault.SaveCredentials(loginId, credential); | ||
|
|
||
| var retrievedCredential = credentialVault.GetCredentials(loginId); | ||
| Assert.IsNotNull(retrievedCredential); | ||
| Assert.AreEqual(testCredential, retrievedCredential.Password); | ||
|
|
||
| credentialVault.RemoveCredentials(loginId); | ||
|
|
||
| var nullCredential = credentialVault.GetCredentials(loginId); | ||
| Assert.IsNull(nullCredential); | ||
|
|
||
| Assert.AreEqual(0, credentialVault.GetAllCredentials().Count()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("LiveData")] | ||
| public void CredentialVault_GetAllCredentials() | ||
| { | ||
| var credentialVault = new CredentialVault("DevHomeGitHubExtensionTest"); | ||
| Assert.IsNotNull(credentialVault); | ||
|
|
||
| Assert.AreEqual(0, credentialVault.GetAllCredentials().Count()); | ||
|
|
||
| var testLoginId = "testuser1"; | ||
| var testCredential = "testCredential"; | ||
|
|
||
| var credential = new NetworkCredential(null, testCredential).SecurePassword; | ||
| credentialVault.SaveCredentials(testLoginId, credential); | ||
|
|
||
| Assert.AreEqual(1, credentialVault.GetAllCredentials().Count()); | ||
|
|
||
| credentialVault.RemoveCredentials(testLoginId); | ||
|
|
||
| Assert.AreEqual(0, credentialVault.GetAllCredentials().Count()); | ||
|
|
||
| var nullCredential = credentialVault.GetCredentials(testLoginId); | ||
| Assert.IsNull(nullCredential); | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -56,6 +56,32 @@ private CredentialVault SetupCredentialVaultWithGHESTestUser() | |
| return credentialVault; | ||
| } | ||
|
|
||
| /* Tests depend on the following environment variables: | ||
| * DEV_HOME_TEST_GITHUB_COM_USER : Url for a test user on github.com | ||
| * DEV_HOME_TEST_GITHUB_COM_PAT : Personal Access Token for the test user on github.com | ||
| * DEV_HOME_TEST_GITHUB_ENTERPRISE_SERVER_USER : Url for a test user on GHES | ||
| * DEV_HOME_TEST_GITHUB_ENTERPRISE_SERVER_PAT : Personal Access Token for the test user on the GHES | ||
| */ | ||
| private CredentialVault SetupCredentialVaultWithMultipleTestUsers() | ||
| { | ||
| var credentialVault = SetupCleanCredentialVaultClean(); | ||
|
|
||
| var testLoginId = Environment.GetEnvironmentVariable("DEV_HOME_TEST_GITHUB_COM_USER") ?? string.Empty; | ||
| var testPassword = Environment.GetEnvironmentVariable("DEV_HOME_TEST_GITHUB_COM_PAT"); | ||
|
|
||
| var testGHESLoginId = Environment.GetEnvironmentVariable("DEV_HOME_TEST_GITHUB_ENTERPRISE_SERVER_USER") ?? string.Empty; | ||
| var testGHESPassword = Environment.GetEnvironmentVariable("DEV_HOME_TEST_GITHUB_ENTERPRISE_SERVER_PAT"); | ||
|
|
||
| var password = new NetworkCredential(null, testPassword).SecurePassword; | ||
| credentialVault.SaveCredentials(testLoginId, password); | ||
|
|
||
| var ghesPassword = new NetworkCredential(null, testGHESPassword).SecurePassword; | ||
| credentialVault.SaveCredentials(testGHESLoginId, ghesPassword); | ||
|
|
||
| Assert.AreEqual(2, credentialVault.GetAllCredentials().Count()); | ||
| return credentialVault; | ||
| } | ||
|
|
||
| private CredentialVault SetupCredentialVaultWithInvalidTestUser() | ||
| { | ||
| var credentialVault = SetupCleanCredentialVaultClean(); | ||
|
|
@@ -129,6 +155,31 @@ public void DeveloperIdProvider_RestoreAndGetDeveloperIds() | |
| Assert.AreEqual(0, credentialVault.GetAllCredentials().Count()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("LiveData")] | ||
| public void DeveloperIdProvider_RestoreAndGetMultipleDeveloperIds() | ||
| { | ||
| // Setup CredentialVault with a dummy testuser and valid PAT for Github.com | ||
| var credentialVault = SetupCredentialVaultWithMultipleTestUsers(); | ||
|
|
||
| // Test whether the DeveloperIdProvider can restore the saved credentials | ||
| var devIdProvider = DeveloperIdProvider.GetInstance(); | ||
| Assert.IsNotNull(devIdProvider); | ||
| var result = devIdProvider.GetLoggedInDeveloperIds(); | ||
|
|
||
| Assert.IsNotNull(result); | ||
| Assert.AreEqual(ProviderOperationStatus.Success, result.Result.Status); | ||
| Assert.AreEqual(2, result.DeveloperIds.Count()); | ||
| Assert.AreNotEqual("dummytestuser1", result.DeveloperIds.First().LoginId); | ||
| Assert.AreNotEqual("dummytestuser1", result.DeveloperIds.Last().LoginId); | ||
| Assert.IsNotNull(new Uri(result.DeveloperIds.First().Url)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider using |
||
| Assert.IsNotNull(new Uri(result.DeveloperIds.Last().Url)); | ||
|
|
||
| // Cleanup | ||
| credentialVault.RemoveAllCredentials(); | ||
| Assert.AreEqual(0, credentialVault.GetAllCredentials().Count()); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [TestCategory("LiveData")] | ||
| public void DeveloperIdProvider_GetDeveloperIds_InvalidPAT() | ||
|
|
||
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.
nit: constructor won't return null
** all occurrences **