Skip to content
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

move test files #180

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions internal/bitwarden/embedded/password_manager_base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -22,33 +21,33 @@ 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,
KdfIterations: 3,
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)

Expand All @@ -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)

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
47 changes: 23 additions & 24 deletions internal/bitwarden/embedded/password_manager_webapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -111,7 +110,7 @@ func newMockedPasswordManager(client webapi.Client) (webAPIVault, func()) {
httpmock.Activate()

return webAPIVault{
serverURL: fixtures.ServerURL,
serverURL: ServerURL,
client: client,
}, httpmock.DeactivateAndReset
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fixtures
package embedded

import (
"context"
Expand All @@ -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"
)
Expand Down Expand Up @@ -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",
})

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fixtures
package embedded

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package fixtures
package embedded

import (
"fmt"
"net/http"
"os"
"path"
"path/filepath"
"runtime"
"strings"
Expand All @@ -19,19 +20,20 @@ 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)
if !ok {
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)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fixtures
package embedded

const (
ServerURL = "http://127.0.0.1:8080"
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/provider_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package test
package provider

import (
"context"
Expand Down
Loading