From a25082526eed74cd38344208c5b943bf907be0f3 Mon Sep 17 00:00:00 2001 From: Maxime Lagresle Date: Tue, 5 Nov 2024 07:31:19 +0100 Subject: [PATCH] move test files --- .../embedded/password_manager_base_test.go | 27 +++++------ .../embedded/password_manager_webapi_test.go | 47 +++++++++---------- ....go => testhelper_create_accounts_test.go} | 7 ++- ...t.go => testhelper_disk_transport_test.go} | 2 +- ...nt.go => testhelper_mocked_client_test.go} | 10 ++-- ...ts.go => testhelper_test_accounts_test.go} | 2 +- internal/provider/provider_utils_test.go | 3 +- .../testhelper_secrets_manager_test.go} | 2 +- 8 files changed, 49 insertions(+), 51 deletions(-) rename internal/bitwarden/embedded/{fixtures/create_accounts_test.go => testhelper_create_accounts_test.go} (93%) rename internal/bitwarden/embedded/{fixtures/disk_transport.go => testhelper_disk_transport_test.go} (98%) rename internal/bitwarden/embedded/{fixtures/mocked_client.go => testhelper_mocked_client_test.go} (87%) rename internal/bitwarden/embedded/{fixtures/test_accounts.go => testhelper_test_accounts_test.go} (99%) rename internal/{bitwarden/test/test_secrets_manager.go => provider/testhelper_secrets_manager_test.go} (99%) diff --git a/internal/bitwarden/embedded/password_manager_base_test.go b/internal/bitwarden/embedded/password_manager_base_test.go index fb3e75d..7360398 100644 --- a/internal/bitwarden/embedded/password_manager_base_test.go +++ b/internal/bitwarden/embedded/password_manager_base_test.go @@ -9,7 +9,6 @@ import ( "time" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/crypto/symmetrickey" - "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/embedded/fixtures" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models" "github.com/stretchr/testify/assert" ) @@ -22,19 +21,19 @@ const ( var ( testAccountPbkdf2 = Account{ AccountUUID: "e8dababd-242e-4900-becf-e88bc021dda8", - Email: fixtures.Pdkdf2Email, + Email: Pdkdf2Email, VaultFormat: "API", KdfConfig: models.KdfConfiguration{ KdfType: models.KdfTypePBKDF2_SHA256, KdfIterations: 600000, }, - ProtectedSymmetricKey: fixtures.Pdkdf2ProtectedSymmetricKey, - ProtectedRSAPrivateKey: fixtures.Pdkdf2ProtectedRSAPrivateKey, + ProtectedSymmetricKey: Pdkdf2ProtectedSymmetricKey, + ProtectedRSAPrivateKey: Pdkdf2ProtectedRSAPrivateKey, } testAccountArgon2 = Account{ AccountUUID: "e8dababd-242e-4900-becf-e88bc021dda8", - Email: fixtures.Argon2Email, + Email: Argon2Email, VaultFormat: "API", KdfConfig: models.KdfConfiguration{ KdfType: models.KdfTypeArgon2, @@ -42,13 +41,13 @@ var ( KdfMemory: 64, KdfParallelism: 4, }, - ProtectedSymmetricKey: fixtures.Argon2ProtectedSymmetricKey, - ProtectedRSAPrivateKey: fixtures.Argon2ProtectedRSAPrivateKey, + ProtectedSymmetricKey: Argon2ProtectedSymmetricKey, + ProtectedRSAPrivateKey: Argon2ProtectedRSAPrivateKey, } ) func TestDecryptAccountSecretPbkdf2(t *testing.T) { - accountSecrets, err := decryptAccountSecrets(testAccountPbkdf2, fixtures.TestPassword) + accountSecrets, err := decryptAccountSecrets(testAccountPbkdf2, TestPassword) assert.NoError(t, err) assert.Equal(t, "jHZYmFOOr0KGorSsmWeuGMHWJDqRrz7uwZNBJkZaupM=", accountSecrets.MasterPasswordHash) @@ -59,12 +58,12 @@ func TestDecryptAccountSecretPbkdf2(t *testing.T) { }, ) - assert.Equal(t, fixtures.RsaPrivateKey, strings.Replace(string(pemdata), "\\n", "\n", -1)) - assert.Contains(t, accountSecrets.MainKey.Summary(), fixtures.EncryptionKey) + assert.Equal(t, RsaPrivateKey, strings.Replace(string(pemdata), "\\n", "\n", -1)) + assert.Contains(t, accountSecrets.MainKey.Summary(), EncryptionKey) } func TestDecryptAccountSecretArgon2(t *testing.T) { - accountSecrets, err := decryptAccountSecrets(testAccountArgon2, fixtures.TestPassword) + accountSecrets, err := decryptAccountSecrets(testAccountArgon2, TestPassword) assert.NoError(t, err) assert.Equal(t, "3fBImY0XFvRrUSP/fe6mqUc1bjhWBuvHYJvlwnxS0i4=", accountSecrets.MasterPasswordHash) @@ -75,8 +74,8 @@ func TestDecryptAccountSecretArgon2(t *testing.T) { }, ) - assert.Equal(t, fixtures.RsaPrivateKey, strings.Replace(string(pemdata), "\\n", "\n", -1)) - assert.Contains(t, accountSecrets.MainKey.Summary(), fixtures.EncryptionKey) + assert.Equal(t, RsaPrivateKey, strings.Replace(string(pemdata), "\\n", "\n", -1)) + assert.Contains(t, accountSecrets.MainKey.Summary(), EncryptionKey) } func TestDecryptAccountSecretWrongPassword(t *testing.T) { @@ -337,7 +336,7 @@ func testFullyFilledLogin() models.Login { } func computeTestAccountSecrets(t *testing.T) *AccountSecrets { - accountSecrets, err := decryptAccountSecrets(testAccountPbkdf2, fixtures.TestPassword) + accountSecrets, err := decryptAccountSecrets(testAccountPbkdf2, TestPassword) if err != nil { t.Fatal(err) } diff --git a/internal/bitwarden/embedded/password_manager_webapi_test.go b/internal/bitwarden/embedded/password_manager_webapi_test.go index 40e7c44..7692ad4 100644 --- a/internal/bitwarden/embedded/password_manager_webapi_test.go +++ b/internal/bitwarden/embedded/password_manager_webapi_test.go @@ -5,90 +5,89 @@ import ( "testing" "github.com/jarcoal/httpmock" - "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/embedded/fixtures" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/webapi" "github.com/stretchr/testify/assert" ) func TestLoginAsPasswordLoadsAccountInformationForPbkdf2(t *testing.T) { - vault, reset := newMockedPasswordManager(fixtures.MockedClient(t, fixtures.Pdkdf2Mocks)) + vault, reset := newMockedPasswordManager(MockedClient(t, Pdkdf2Mocks)) defer reset() ctx := context.Background() - err := vault.LoginWithPassword(ctx, fixtures.Pdkdf2Email, fixtures.TestPassword) + err := vault.LoginWithPassword(ctx, Pdkdf2Email, TestPassword) if err != nil { t.Fatalf("vault unlock failed: %v", err) } assert.Equal(t, "API", vault.loginAccount.VaultFormat) - assert.Equal(t, fixtures.Pdkdf2Email, vault.loginAccount.Email) + assert.Equal(t, Pdkdf2Email, vault.loginAccount.Email) assert.Equal(t, models.KdfTypePBKDF2_SHA256, vault.loginAccount.KdfConfig.KdfType) assert.Equal(t, 600000, vault.loginAccount.KdfConfig.KdfIterations) - assert.Equal(t, fixtures.Pdkdf2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) - assert.Equal(t, fixtures.Pdkdf2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) + assert.Equal(t, Pdkdf2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) + assert.Equal(t, Pdkdf2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) } func TestLoginAsAPILoadsAccountInformationForPbkdf2(t *testing.T) { - vault, reset := newMockedPasswordManager(fixtures.MockedClient(t, fixtures.Pdkdf2Mocks)) + vault, reset := newMockedPasswordManager(MockedClient(t, Pdkdf2Mocks)) defer reset() ctx := context.Background() - err := vault.LoginWithAPIKey(ctx, fixtures.TestPassword, "user.aaf15bd1-4f51-4ba0-ade8-9dc2ec0fd2c3", "ZTXHHyPY6bNlNq1diDA2nM1GROboP3") + err := vault.LoginWithAPIKey(ctx, TestPassword, "user.aaf15bd1-4f51-4ba0-ade8-9dc2ec0fd2c3", "ZTXHHyPY6bNlNq1diDA2nM1GROboP3") if err != nil { t.Fatalf("vault unlock failed: %v", err) } assert.Equal(t, "API", vault.loginAccount.VaultFormat) - assert.Equal(t, fixtures.Pdkdf2Email, vault.loginAccount.Email) + assert.Equal(t, Pdkdf2Email, vault.loginAccount.Email) assert.Equal(t, models.KdfTypePBKDF2_SHA256, vault.loginAccount.KdfConfig.KdfType) assert.Equal(t, 600000, vault.loginAccount.KdfConfig.KdfIterations) - assert.Equal(t, fixtures.Pdkdf2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) - assert.Equal(t, fixtures.Pdkdf2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) + assert.Equal(t, Pdkdf2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) + assert.Equal(t, Pdkdf2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) } func TestLoginAsPasswordLoadsAccountInformationForArgon2(t *testing.T) { - vault, reset := newMockedPasswordManager(fixtures.MockedClient(t, fixtures.Argon2Mocks)) + vault, reset := newMockedPasswordManager(MockedClient(t, Argon2Mocks)) defer reset() ctx := context.Background() - err := vault.LoginWithPassword(ctx, fixtures.Argon2Email, fixtures.TestPassword) + err := vault.LoginWithPassword(ctx, Argon2Email, TestPassword) if err != nil { t.Fatalf("vault unlock failed: %v", err) } assert.Equal(t, "API", vault.loginAccount.VaultFormat) - assert.Equal(t, fixtures.Argon2Email, vault.loginAccount.Email) + assert.Equal(t, Argon2Email, vault.loginAccount.Email) assert.Equal(t, models.KdfTypeArgon2, vault.loginAccount.KdfConfig.KdfType) assert.Equal(t, 3, vault.loginAccount.KdfConfig.KdfIterations) - assert.Equal(t, fixtures.Argon2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) - assert.Equal(t, fixtures.Argon2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) + assert.Equal(t, Argon2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) + assert.Equal(t, Argon2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) } func TestLoginAsAPILoadsAccountInformationForArgon2(t *testing.T) { - vault, reset := newMockedPasswordManager(fixtures.MockedClient(t, fixtures.Argon2Mocks)) + vault, reset := newMockedPasswordManager(MockedClient(t, Argon2Mocks)) defer reset() ctx := context.Background() - err := vault.LoginWithAPIKey(ctx, fixtures.TestPassword, "user.3f0abf17-e779-4312-a3dd-9c6266e95a9e", "oQAvXGx5h3iw0wzzgRwySsGxn3PvvA") + err := vault.LoginWithAPIKey(ctx, TestPassword, "user.3f0abf17-e779-4312-a3dd-9c6266e95a9e", "oQAvXGx5h3iw0wzzgRwySsGxn3PvvA") if err != nil { t.Fatalf("vault unlock failed: %v", err) } assert.Equal(t, "API", vault.loginAccount.VaultFormat) - assert.Equal(t, fixtures.Argon2Email, vault.loginAccount.Email) + assert.Equal(t, Argon2Email, vault.loginAccount.Email) assert.Equal(t, models.KdfTypeArgon2, vault.loginAccount.KdfConfig.KdfType) assert.Equal(t, 3, vault.loginAccount.KdfConfig.KdfIterations) - assert.Equal(t, fixtures.Argon2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) - assert.Equal(t, fixtures.Argon2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) + assert.Equal(t, Argon2ProtectedRSAPrivateKey, vault.loginAccount.ProtectedRSAPrivateKey) + assert.Equal(t, Argon2ProtectedSymmetricKey, vault.loginAccount.ProtectedSymmetricKey) } func TestObjectCreation(t *testing.T) { - vault, reset := newMockedPasswordManager(fixtures.MockedClient(t, fixtures.Pdkdf2Mocks)) + vault, reset := newMockedPasswordManager(MockedClient(t, Pdkdf2Mocks)) defer reset() ctx := context.Background() - err := vault.LoginWithPassword(ctx, fixtures.Pdkdf2Email, fixtures.TestPassword) + err := vault.LoginWithPassword(ctx, Pdkdf2Email, TestPassword) if err != nil { t.Fatalf("vault unlock failed: %v", err) } @@ -111,7 +110,7 @@ func newMockedPasswordManager(client webapi.Client) (webAPIVault, func()) { httpmock.Activate() return webAPIVault{ - serverURL: fixtures.ServerURL, + serverURL: ServerURL, client: client, }, httpmock.DeactivateAndReset } diff --git a/internal/bitwarden/embedded/fixtures/create_accounts_test.go b/internal/bitwarden/embedded/testhelper_create_accounts_test.go similarity index 93% rename from internal/bitwarden/embedded/fixtures/create_accounts_test.go rename to internal/bitwarden/embedded/testhelper_create_accounts_test.go index 3d54aa7..0db48ba 100644 --- a/internal/bitwarden/embedded/fixtures/create_accounts_test.go +++ b/internal/bitwarden/embedded/testhelper_create_accounts_test.go @@ -1,4 +1,4 @@ -package fixtures +package embedded import ( "context" @@ -18,7 +18,6 @@ import ( "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/crypto/helpers" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/crypto/keybuilder" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/crypto/symmetrickey" - "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/embedded" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/webapi" ) @@ -61,7 +60,7 @@ func TestCreateAccessTokenLoginMock(t *testing.T) { t.Fatal(err) } - jwtToken := jwt.NewWithClaims(jwt.SigningMethodNone, &embedded.MachineAccountClaims{ + jwtToken := jwt.NewWithClaims(jwt.SigningMethodNone, &MachineAccountClaims{ Organization: "b1a4b97f-c75e-4901-b831-00912f3549a7", }) @@ -152,7 +151,7 @@ func createTestAccount(t *testing.T, accountEmail string, kdfConfig models.KdfCo Prefix: mockName, }, } - vault := embedded.NewPasswordManagerClient(ServerURL, testDeviceIdentifer, "dev", embedded.WithPasswordManagerHttpOptions(webapi.WithCustomClient(httpClient))) + vault := NewPasswordManagerClient(ServerURL, testDeviceIdentifer, "dev", WithPasswordManagerHttpOptions(webapi.WithCustomClient(httpClient))) err = vault.LoginWithPassword(ctx, accountEmail, TestPassword) if err != nil { diff --git a/internal/bitwarden/embedded/fixtures/disk_transport.go b/internal/bitwarden/embedded/testhelper_disk_transport_test.go similarity index 98% rename from internal/bitwarden/embedded/fixtures/disk_transport.go rename to internal/bitwarden/embedded/testhelper_disk_transport_test.go index ff49dc2..d34c790 100644 --- a/internal/bitwarden/embedded/fixtures/disk_transport.go +++ b/internal/bitwarden/embedded/testhelper_disk_transport_test.go @@ -1,4 +1,4 @@ -package fixtures +package embedded import ( "bytes" diff --git a/internal/bitwarden/embedded/fixtures/mocked_client.go b/internal/bitwarden/embedded/testhelper_mocked_client_test.go similarity index 87% rename from internal/bitwarden/embedded/fixtures/mocked_client.go rename to internal/bitwarden/embedded/testhelper_mocked_client_test.go index 84444a5..44c321a 100644 --- a/internal/bitwarden/embedded/fixtures/mocked_client.go +++ b/internal/bitwarden/embedded/testhelper_mocked_client_test.go @@ -1,9 +1,10 @@ -package fixtures +package embedded import ( "fmt" "net/http" "os" + "path" "path/filepath" "runtime" "strings" @@ -19,11 +20,12 @@ const ( testDeviceVersion = "dev" ) -func MockedClient(t *testing.T, name string) webapi.Client { +func MockedClient(t testing.TB, name string) webapi.Client { return webapi.NewClient(mockedServerUrl, testDeviceIdentifer, testDeviceVersion, webapi.WithCustomClient(MockedHTTPClient(t, mockedServerUrl, name)), webapi.DisableRetries()) } -func MockedHTTPClient(t *testing.T, serverUrl string, name string) http.Client { +func MockedHTTPClient(t testing.TB, serverUrl string, name string) http.Client { + t.Helper() client := http.Client{Transport: httpmock.DefaultTransport} _, file, _, ok := runtime.Caller(0) @@ -31,7 +33,7 @@ func MockedHTTPClient(t *testing.T, serverUrl string, name string) http.Client { t.Fatal("unable to get caller information") } dir := filepath.Dir(file) - + dir = path.Join(dir, "fixtures") files, err := os.ReadDir(dir) if err != nil { t.Fatal(err) diff --git a/internal/bitwarden/embedded/fixtures/test_accounts.go b/internal/bitwarden/embedded/testhelper_test_accounts_test.go similarity index 99% rename from internal/bitwarden/embedded/fixtures/test_accounts.go rename to internal/bitwarden/embedded/testhelper_test_accounts_test.go index 4a13f78..cbcc886 100644 --- a/internal/bitwarden/embedded/fixtures/test_accounts.go +++ b/internal/bitwarden/embedded/testhelper_test_accounts_test.go @@ -1,4 +1,4 @@ -package fixtures +package embedded const ( ServerURL = "http://127.0.0.1:8080" diff --git a/internal/provider/provider_utils_test.go b/internal/provider/provider_utils_test.go index 7f574c8..4d085a2 100644 --- a/internal/provider/provider_utils_test.go +++ b/internal/provider/provider_utils_test.go @@ -18,7 +18,6 @@ import ( "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/bwcli" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/embedded" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/models" - "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/test" "github.com/maxlaverse/terraform-provider-bitwarden/internal/bitwarden/webapi" ) @@ -266,7 +265,7 @@ func testOrRealSecretsManagerProvider(t *testing.T) (string, string, func()) { } func spawnTestSecretsManager(t *testing.T) (string, string, func()) { - testSecretsManager := test.NewTestSecretsManager() + testSecretsManager := NewTestSecretsManager() ctx, stop := context.WithCancel(context.Background()) go testSecretsManager.Run(ctx, 8081) diff --git a/internal/bitwarden/test/test_secrets_manager.go b/internal/provider/testhelper_secrets_manager_test.go similarity index 99% rename from internal/bitwarden/test/test_secrets_manager.go rename to internal/provider/testhelper_secrets_manager_test.go index 80b1812..5a7ba47 100644 --- a/internal/bitwarden/test/test_secrets_manager.go +++ b/internal/provider/testhelper_secrets_manager_test.go @@ -1,4 +1,4 @@ -package test +package provider import ( "context"