(t *testing.T)
| 1573 | } |
| 1574 | |
| 1575 | func TestUserSecrets(t *testing.T) { |
| 1576 | t.Parallel() |
| 1577 | ctx := context.Background() |
| 1578 | |
| 1579 | const ( |
| 1580 | //nolint:gosec // test credentials |
| 1581 | initialValue = "super-secret-value-initial" |
| 1582 | //nolint:gosec // test credentials |
| 1583 | updatedValue = "super-secret-value-updated" |
| 1584 | ) |
| 1585 | |
| 1586 | insertUserSecret := func( |
| 1587 | t *testing.T, |
| 1588 | crypt *dbCrypt, |
| 1589 | ciphers []Cipher, |
| 1590 | ) database.UserSecret { |
| 1591 | t.Helper() |
| 1592 | user := dbgen.User(t, crypt, database.User{}) |
| 1593 | secret, err := crypt.CreateUserSecret(ctx, database.CreateUserSecretParams{ |
| 1594 | ID: uuid.New(), |
| 1595 | UserID: user.ID, |
| 1596 | Name: "test-secret-" + uuid.NewString()[:8], |
| 1597 | Value: initialValue, |
| 1598 | }) |
| 1599 | require.NoError(t, err) |
| 1600 | require.Equal(t, initialValue, secret.Value) |
| 1601 | if len(ciphers) > 0 { |
| 1602 | require.Equal(t, ciphers[0].HexDigest(), secret.ValueKeyID.String) |
| 1603 | } |
| 1604 | return secret |
| 1605 | } |
| 1606 | |
| 1607 | t.Run("CreateUserSecretEncryptsValue", func(t *testing.T) { |
| 1608 | t.Parallel() |
| 1609 | db, crypt, ciphers := setup(t) |
| 1610 | secret := insertUserSecret(t, crypt, ciphers) |
| 1611 | |
| 1612 | // Reading through crypt should return plaintext. |
| 1613 | got, err := crypt.GetUserSecretByUserIDAndName(ctx, database.GetUserSecretByUserIDAndNameParams{ |
| 1614 | UserID: secret.UserID, |
| 1615 | Name: secret.Name, |
| 1616 | }) |
| 1617 | require.NoError(t, err) |
| 1618 | require.Equal(t, initialValue, got.Value) |
| 1619 | |
| 1620 | // Reading through raw DB should return encrypted value. |
| 1621 | raw, err := db.GetUserSecretByUserIDAndName(ctx, database.GetUserSecretByUserIDAndNameParams{ |
| 1622 | UserID: secret.UserID, |
| 1623 | Name: secret.Name, |
| 1624 | }) |
| 1625 | require.NoError(t, err) |
| 1626 | require.NotEqual(t, initialValue, raw.Value) |
| 1627 | requireEncryptedEquals(t, ciphers[0], raw.Value, initialValue) |
| 1628 | }) |
| 1629 | |
| 1630 | t.Run("ListUserSecretsWithValuesDecrypts", func(t *testing.T) { |
| 1631 | t.Parallel() |
| 1632 | _, crypt, ciphers := setup(t) |
nothing calls this directly
no test coverage detected