mirror of
https://github.com/ProtonMail/go-proton-api.git
synced 2025-12-23 15:47:52 -05:00
feat(BRIDGE-348): add utility and tests functions for BYOE
This commit is contained in:
committed by
Atanas Janeshliev
parent
1829a04dd4
commit
92de6e7c85
@@ -16,11 +16,11 @@ func TestAddress_DisplayName(t *testing.T) {
|
||||
// Create a user on the server.
|
||||
userID, _, err := s.CreateUser("user", []byte("pass"))
|
||||
require.NoError(t, err)
|
||||
addr1, err := s.CreateAddress(userID, "user1@pm.me", []byte("pass"))
|
||||
addr1, err := s.CreateAddress(userID, "user1@pm.me", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.ChangeAddressDisplayName(userID, addr1, "User 1"))
|
||||
|
||||
addr2, err := s.CreateAddress(userID, "user2@pm.me", []byte("pass"))
|
||||
addr2, err := s.CreateAddress(userID, "user2@pm.me", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.ChangeAddressDisplayName(userID, addr2, "User 2"))
|
||||
|
||||
@@ -60,16 +60,16 @@ func TestAddress_Types(t *testing.T) {
|
||||
// Create a user on the server.
|
||||
userID, _, err := s.CreateUser("user", []byte("pass"))
|
||||
require.NoError(t, err)
|
||||
id2, err := s.CreateAddress(userID, "user@alias.com", []byte("pass"))
|
||||
id2, err := s.CreateAddress(userID, "user@alias.com", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.ChangeAddressType(userID, id2, proton.AddressTypeAlias))
|
||||
id3, err := s.CreateAddress(userID, "user@custom.com", []byte("pass"))
|
||||
id3, err := s.CreateAddress(userID, "user@custom.com", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.ChangeAddressType(userID, id3, proton.AddressTypeCustom))
|
||||
id4, err := s.CreateAddress(userID, "user@premium.com", []byte("pass"))
|
||||
id4, err := s.CreateAddress(userID, "user@premium.com", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.ChangeAddressType(userID, id4, proton.AddressTypePremium))
|
||||
id5, err := s.CreateAddress(userID, "user@external.com", []byte("pass"))
|
||||
id5, err := s.CreateAddress(userID, "user@external.com", []byte("pass"), false)
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, s.ChangeAddressType(userID, id5, proton.AddressTypeExternal))
|
||||
|
||||
|
||||
@@ -36,3 +36,9 @@ const (
|
||||
AddressTypePremium
|
||||
AddressTypeExternal
|
||||
)
|
||||
|
||||
// IsBYOEAddress - return a bool corresponding to whether an address is a BYOE address.
|
||||
// BYOE addresses have sending enabled and are of type `external`.
|
||||
func (a Address) IsBYOEAddress() bool {
|
||||
return bool(a.Send) && a.Type == AddressTypeExternal
|
||||
}
|
||||
|
||||
@@ -1131,6 +1131,21 @@ func (b *Backend) AddAddressCreatedUpdate(userID, addrID string) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) AddAddressUpdatedEvent(userID, addrID string) error {
|
||||
return writeBackendRet(b, func(b *unsafeBackend) error {
|
||||
return b.withAcc(userID, func(acc *account) error {
|
||||
updateID, err := b.newUpdate(&addressUpdated{addressID: addrID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
acc.updateIDs = append(acc.updateIDs, updateID)
|
||||
|
||||
return nil
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) AddLabelCreatedUpdate(userID, labelID string) error {
|
||||
return writeBackendRet(b, func(b *unsafeBackend) error {
|
||||
return b.withAcc(userID, func(acc *account) error {
|
||||
|
||||
@@ -254,9 +254,9 @@ func (b *Backend) RemoveUserKey(userID, keyID string) error {
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Backend) CreateAddress(userID, email string, password []byte, withKey bool, status proton.AddressStatus, addrType proton.AddressType) (string, error) {
|
||||
func (b *Backend) CreateAddress(userID, email string, password []byte, withKey bool, status proton.AddressStatus, addrType proton.AddressType, withSending bool) (string, error) {
|
||||
return writeBackendRetErr(b, func(b *unsafeBackend) (string, error) {
|
||||
return b.createAddress(userID, email, password, withKey, status, addrType, false, true)
|
||||
return b.createAddress(userID, email, password, withKey, status, addrType, false, withSending)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ func (s *Backend) quarkUserCreate(args ...string) (proton.User, error) {
|
||||
|
||||
// TODO: Create keys of different types (we always use RSA2048).
|
||||
if *newAddr || *genKeys != "" {
|
||||
if _, err := s.CreateAddress(userID, *name+"@"+s.domain, []byte(*pass), *genKeys != "", addressStatus, proton.AddressTypeOriginal); err != nil {
|
||||
if _, err := s.CreateAddress(userID, *name+"@"+s.domain, []byte(*pass), *genKeys != "", addressStatus, proton.AddressTypeOriginal, true); err != nil {
|
||||
return proton.User{}, fmt.Errorf("failed to create address with keys: %w", err)
|
||||
}
|
||||
}
|
||||
@@ -104,7 +104,7 @@ func (s *Backend) quarkUserCreateAddress(args ...string) (proton.Address, error)
|
||||
}
|
||||
|
||||
// TODO: Create keys of different types (we always use RSA2048).
|
||||
addrID, err := s.CreateAddress(fs.Arg(0), fs.Arg(2), []byte(fs.Arg(1)), *genKeys != "", addressStatus, proton.AddressTypeOriginal)
|
||||
addrID, err := s.CreateAddress(fs.Arg(0), fs.Arg(2), []byte(fs.Arg(1)), *genKeys != "", addressStatus, proton.AddressTypeOriginal, true)
|
||||
if err != nil {
|
||||
return proton.Address{}, fmt.Errorf("failed to create address with keys: %w", err)
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ func (s *service) RevokeUser(ctx context.Context, req *proto.RevokeUserRequest)
|
||||
}
|
||||
|
||||
func (s *service) CreateAddress(ctx context.Context, req *proto.CreateAddressRequest) (*proto.CreateAddressResponse, error) {
|
||||
addrID, err := s.server.CreateAddress(req.UserID, req.Email, req.Password)
|
||||
addrID, err := s.server.CreateAddress(req.UserID, req.Email, req.Password, true)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -112,7 +112,7 @@ func (s *Server) CreateUser(username string, password []byte) (string, string, e
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
addrID, err := s.b.CreateAddress(userID, username+"@"+s.domain, password, true, proton.AddressStatusEnabled, proton.AddressTypeOriginal)
|
||||
addrID, err := s.b.CreateAddress(userID, username+"@"+s.domain, password, true, proton.AddressStatusEnabled, proton.AddressTypeOriginal, true)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@@ -147,8 +147,12 @@ func (s *Server) RemoveUserKey(userID, keyID string) error {
|
||||
return s.b.RemoveUserKey(userID, keyID)
|
||||
}
|
||||
|
||||
func (s *Server) CreateAddress(userID, email string, password []byte) (string, error) {
|
||||
return s.b.CreateAddress(userID, email, password, true, proton.AddressStatusEnabled, proton.AddressTypeOriginal)
|
||||
func (s *Server) CreateAddress(userID, email string, password []byte, withSend bool) (string, error) {
|
||||
return s.b.CreateAddress(userID, email, password, true, proton.AddressStatusEnabled, proton.AddressTypeOriginal, withSend)
|
||||
}
|
||||
|
||||
func (s *Server) CreateExternalAddress(userID, email string, password []byte, withSend bool) (string, error) {
|
||||
return s.b.CreateAddress(userID, email, password, true, proton.AddressStatusEnabled, proton.AddressTypeExternal, withSend)
|
||||
}
|
||||
|
||||
func (s *Server) CreateAddressAsUpdate(userID, email string, password []byte) (string, error) {
|
||||
@@ -208,6 +212,10 @@ func (s *Server) AddAddressCreatedEvent(userID, addrID string) error {
|
||||
return s.b.AddAddressCreatedUpdate(userID, addrID)
|
||||
}
|
||||
|
||||
func (s *Server) AddAddressUpdatedEvent(userID, addrID string) error {
|
||||
return s.b.AddAddressUpdatedEvent(userID, addrID)
|
||||
}
|
||||
|
||||
func (s *Server) AddLabelCreatedEvent(userID, labelID string) error {
|
||||
return s.b.AddLabelCreatedUpdate(userID, labelID)
|
||||
}
|
||||
|
||||
@@ -1934,7 +1934,7 @@ func TestServer_AddressCreateDelete(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create an address.
|
||||
alias, err := s.CreateAddress(user.ID, "alias@example.com", []byte("pass"))
|
||||
alias, err := s.CreateAddress(user.ID, "alias@example.com", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
|
||||
// The user should have two addresses, both enabled.
|
||||
@@ -1982,13 +1982,13 @@ func TestServer_AddressOrder(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Create 3 additional addresses.
|
||||
addr1, err := s.CreateAddress(user.ID, "addr1@example.com", []byte("pass"))
|
||||
addr1, err := s.CreateAddress(user.ID, "addr1@example.com", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr2, err := s.CreateAddress(user.ID, "addr2@example.com", []byte("pass"))
|
||||
addr2, err := s.CreateAddress(user.ID, "addr2@example.com", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
|
||||
addr3, err := s.CreateAddress(user.ID, "addr3@example.com", []byte("pass"))
|
||||
addr3, err := s.CreateAddress(user.ID, "addr3@example.com", []byte("pass"), true)
|
||||
require.NoError(t, err)
|
||||
|
||||
addresses, err := c.GetAddresses(context.Background())
|
||||
|
||||
Reference in New Issue
Block a user