Skip to content

Commit

Permalink
feat(GODT-3122): server now support display name for addresses.
Browse files Browse the repository at this point in the history
  • Loading branch information
xmichelo committed Nov 16, 2023
1 parent 0ea2258 commit c9bc6f7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
44 changes: 44 additions & 0 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,50 @@ import (
"github.com/stretchr/testify/require"
)

func TestAddress_DisplayName(t *testing.T) {
s := server.New()
defer s.Close()

// Create a user on the server.
userID, _, err := s.CreateUser("user", []byte("pass"))
require.NoError(t, err)
addr1, err := s.CreateAddress(userID, "[email protected]", []byte("pass"))
require.NoError(t, err)
require.NoError(t, s.ChangeAddressDisplayName(userID, addr1, "User 1"))

addr2, err := s.CreateAddress(userID, "[email protected]", []byte("pass"))
require.NoError(t, err)
require.NoError(t, s.ChangeAddressDisplayName(userID, addr2, "User 2"))

require.NoError(t, err)

m := proton.New(
proton.WithHostURL(s.GetHostURL()),
proton.WithTransport(proton.InsecureTransport()),
)
defer m.Close()

// Create one session for the user.
c, auth, err := m.NewClientWithLogin(context.Background(), "user", []byte("pass"))
require.NoError(t, err)
require.Equal(t, userID, auth.UserID)

// Get addresses for the user.
addrs, err := c.GetAddresses(context.Background())
require.NoError(t, err)

for _, addr := range addrs {
switch addr.ID {
case addr1:
require.Equal(t, addr.Email, "[email protected]")
require.Equal(t, addr.DisplayName, "User 1")
case addr2:
require.Equal(t, addr.Email, "[email protected]")
require.Equal(t, addr.DisplayName, "User 2")
}
}
}

func TestAddress_Types(t *testing.T) {
s := server.New()
defer s.Close()
Expand Down
17 changes: 9 additions & 8 deletions server/backend/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

type address struct {
addrID string
email string
order int
status proton.AddressStatus
addrType proton.AddressType
keys []key
allowSend bool
addrID string
email string
displayName string
order int
status proton.AddressStatus
addrType proton.AddressType
keys []key
allowSend bool
}

func (add *address) toAddress() proton.Address {
Expand All @@ -27,7 +28,7 @@ func (add *address) toAddress() proton.Address {
Type: add.addrType,

Order: add.order,
DisplayName: add.email,
DisplayName: add.displayName,

Keys: xslices.Map(add.keys, func(key key) proton.Key {
privKey, err := crypto.NewKeyFromArmored(key.key)
Expand Down
33 changes: 23 additions & 10 deletions server/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,13 +275,14 @@ func (b *Backend) createAddress(
addressID := uuid.NewString()

acc.addresses[addressID] = &address{
addrID: addressID,
email: email,
order: len(acc.addresses) + 1,
status: status,
addrType: addrType,
keys: keys,
allowSend: allowSend,
addrID: addressID,
email: email,
displayName: email,
order: len(acc.addresses) + 1,
status: status,
addrType: addrType,
keys: keys,
allowSend: allowSend,
}

var update update
Expand All @@ -302,15 +303,15 @@ func (b *Backend) createAddress(
})
}

func (b *Backend) ChangeAddressType(userID, addrId string, addrType proton.AddressType) error {
func (b *Backend) ChangeAddressType(userID, addrID string, addrType proton.AddressType) error {
return b.withAcc(userID, func(acc *account) error {
for _, addr := range acc.addresses {
if addr.addrID == addrId {
if addr.addrID == addrID {
addr.addrType = addrType
return nil
}
}
return fmt.Errorf("no addrID matching %s for user %s", addrId, userID)
return fmt.Errorf("no addrID matching %s for user %s", addrID, userID)
})
}

Expand All @@ -326,6 +327,18 @@ func (b *Backend) ChangeAddressAllowSend(userID, addrId string, allowSend bool)
})
}

func (b *Backend) ChangeAddressDisplayName(userID, addrID, displayName string) error {
return b.withAcc(userID, func(acc *account) error {
for _, addr := range acc.addresses {
if addr.addrID == addrID {
addr.displayName = displayName
return nil
}
}
return fmt.Errorf("no addrID matching %s for user %s", addrID, userID)
})
}

func (b *Backend) CreateAddressKey(userID, addrID string, password []byte) error {
return b.withAcc(userID, func(acc *account) error {
token, err := crypto.RandomToken(32)
Expand Down
4 changes: 4 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (s *Server) ChangeAddressType(userID, addrId string, addrType proton.Addres
return s.b.ChangeAddressType(userID, addrId, addrType)
}

func (s *Server) ChangeAddressDisplayName(userID, addrID, displayName string) error {
return s.b.ChangeAddressDisplayName(userID, addrID, displayName)
}

func (s *Server) RemoveAddress(userID, addrID string) error {
return s.b.RemoveAddress(userID, addrID)
}
Expand Down

0 comments on commit c9bc6f7

Please sign in to comment.