| 1 |
package server |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"crypto/tls" |
| 6 |
"encoding/json" |
| 7 |
"errors" |
| 8 |
"fmt" |
| 9 |
"net/http" |
| 10 |
"net/mail" |
| 11 |
"net/url" |
| 12 |
"os" |
| 13 |
"runtime" |
| 14 |
"sync" |
| 15 |
"sync/atomic" |
| 16 |
"testing" |
| 17 |
"time" |
| 18 |
|
| 19 |
"github.com/bradenaw/juniper/parallel" |
| 20 |
|
| 21 |
"github.com/Masterminds/semver/v3" |
| 22 |
"github.com/ProtonMail/gluon/async" |
| 23 |
"github.com/ProtonMail/gluon/rfc822" |
| 24 |
"github.com/ProtonMail/gopenpgp/v2/crypto" |
| 25 |
"github.com/bradenaw/juniper/iterator" |
| 26 |
"github.com/bradenaw/juniper/stream" |
| 27 |
"github.com/bradenaw/juniper/xslices" |
| 28 |
"github.com/google/uuid" |
| 29 |
"github.com/henrybear327/go-proton-api" |
| 30 |
"github.com/stretchr/testify/require" |
| 31 |
"golang.org/x/exp/slices" |
| 32 |
) |
| 33 |
|
| 34 |
func TestServer_LoginLogout(t *testing.T) { |
| 35 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 36 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 37 |
user, err := c.GetUser(ctx) |
| 38 |
require.NoError(t, err) |
| 39 |
require.Equal(t, "user", user.Name) |
| 40 |
require.Equal(t, "user@"+s.GetDomain(), user.Email) |
| 41 |
|
| 42 |
// Logout from the test API. |
| 43 |
require.NoError(t, c.AuthDelete(ctx)) |
| 44 |
|
| 45 |
// Future requests should fail. |
| 46 |
require.Error(t, c.AuthDelete(ctx)) |
| 47 |
}) |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
func TestServerMulti(t *testing.T) { |
| 52 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 53 |
_, _, err := s.CreateUser("user", []byte("pass")) |
| 54 |
require.NoError(t, err) |
| 55 |
|
| 56 |
// Create one client. |
| 57 |
c1, _, err := m.NewClientWithLogin(ctx, "user", []byte("pass")) |
| 58 |
require.NoError(t, err) |
| 59 |
defer c1.Close() |
| 60 |
|
| 61 |
// Create another client. |
| 62 |
c2, _, err := m.NewClientWithLogin(ctx, "user", []byte("pass")) |
| 63 |
require.NoError(t, err) |
| 64 |
defer c2.Close() |
| 65 |
|
| 66 |
// Both clients should be able to make requests. |
| 67 |
must(c1.GetUser(ctx)) |
| 68 |
must(c2.GetUser(ctx)) |
| 69 |
|
| 70 |
// Logout the first client; it should no longer be able to make requests. |
| 71 |
require.NoError(t, c1.AuthDelete(ctx)) |
| 72 |
require.Panics(t, func() { must(c1.GetUser(ctx)) }) |
| 73 |
|
| 74 |
// The second client should still be able to make requests. |
| 75 |
must(c2.GetUser(ctx)) |
| 76 |
}) |
| 77 |
} |
| 78 |
|
| 79 |
func TestServer_Ping(t *testing.T) { |
| 80 |
withServer(t, func(ctx context.Context, s *Server, _ *proton.Manager) { |
| 81 |
ctl := proton.NewNetCtl() |
| 82 |
|
| 83 |
m := proton.New( |
| 84 |
proton.WithHostURL(s.GetHostURL()), |
| 85 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 86 |
) |
| 87 |
|
| 88 |
var status proton.Status |
| 89 |
|
| 90 |
m.AddStatusObserver(func(s proton.Status) { |
| 91 |
status = s |
| 92 |
}) |
| 93 |
|
| 94 |
// When the network goes down, ping should fail. |
| 95 |
ctl.Disable() |
| 96 |
require.Error(t, m.Ping(ctx)) |
| 97 |
require.Equal(t, proton.StatusDown, status) |
| 98 |
|
| 99 |
// When the network goes up, ping should succeed. |
| 100 |
ctl.Enable() |
| 101 |
require.NoError(t, m.Ping(ctx)) |
| 102 |
require.Equal(t, proton.StatusUp, status) |
| 103 |
|
| 104 |
// When the API is down, ping should still succeed if the API is at least reachable. |
| 105 |
s.SetOffline(true) |
| 106 |
require.NoError(t, m.Ping(ctx)) |
| 107 |
require.Equal(t, proton.StatusUp, status) |
| 108 |
|
| 109 |
// When the API is down, ping should fail if the API cannot be reached. |
| 110 |
ctl.Disable() |
| 111 |
require.Error(t, m.Ping(ctx)) |
| 112 |
require.Equal(t, proton.StatusDown, status) |
| 113 |
|
| 114 |
// When the network goes up, ping should succeed, even if the API is down. |
| 115 |
ctl.Enable() |
| 116 |
require.NoError(t, m.Ping(ctx)) |
| 117 |
require.Equal(t, proton.StatusUp, status) |
| 118 |
|
| 119 |
// When the API comes back alive, ping should succeed. |
| 120 |
s.SetOffline(false) |
| 121 |
require.NoError(t, m.Ping(ctx)) |
| 122 |
require.Equal(t, proton.StatusUp, status) |
| 123 |
}) |
| 124 |
} |
| 125 |
|
| 126 |
func TestServer_Bool(t *testing.T) { |
| 127 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 128 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 129 |
withMessages(ctx, t, c, "pass", 1, func([]string) { |
| 130 |
metadata, err := c.GetMessageMetadata(ctx, proton.MessageFilter{}) |
| 131 |
require.NoError(t, err) |
| 132 |
|
| 133 |
// By default the message is unread. |
| 134 |
require.True(t, bool(must(c.GetMessage(ctx, metadata[0].ID)).Unread)) |
| 135 |
|
| 136 |
// Mark the message as read. |
| 137 |
require.NoError(t, c.MarkMessagesRead(ctx, metadata[0].ID)) |
| 138 |
|
| 139 |
// Now the message is read. |
| 140 |
require.False(t, bool(must(c.GetMessage(ctx, metadata[0].ID)).Unread)) |
| 141 |
}) |
| 142 |
}) |
| 143 |
}) |
| 144 |
} |
| 145 |
|
| 146 |
func TestServer_Messages(t *testing.T) { |
| 147 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 148 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 149 |
withMessages(ctx, t, c, "pass", 1000, func(messageIDs []string) { |
| 150 |
// Get the messages. |
| 151 |
metadata, err := c.GetMessageMetadata(ctx, proton.MessageFilter{}) |
| 152 |
require.NoError(t, err) |
| 153 |
|
| 154 |
// The messages should be the ones we created. |
| 155 |
require.ElementsMatch(t, messageIDs, xslices.Map(metadata, func(metadata proton.MessageMetadata) string { |
| 156 |
return metadata.ID |
| 157 |
})) |
| 158 |
|
| 159 |
// The messages should be in All Mail and should be unread. |
| 160 |
for _, message := range metadata { |
| 161 |
require.True(t, bool(message.Unread)) |
| 162 |
require.Equal(t, []string{proton.AllMailLabel}, message.LabelIDs) |
| 163 |
} |
| 164 |
|
| 165 |
// Mark the first three messages as read and put them in archive. |
| 166 |
require.NoError(t, c.MarkMessagesRead(ctx, messageIDs[0], messageIDs[1], messageIDs[2])) |
| 167 |
require.NoError(t, c.LabelMessages(ctx, []string{messageIDs[0], messageIDs[1], messageIDs[2]}, proton.ArchiveLabel)) |
| 168 |
|
| 169 |
// They should now be read. |
| 170 |
require.False(t, bool(must(c.GetMessage(ctx, messageIDs[0])).Unread)) |
| 171 |
require.False(t, bool(must(c.GetMessage(ctx, messageIDs[1])).Unread)) |
| 172 |
require.False(t, bool(must(c.GetMessage(ctx, messageIDs[2])).Unread)) |
| 173 |
|
| 174 |
// They should now be in archive. |
| 175 |
require.ElementsMatch(t, []string{proton.ArchiveLabel, proton.AllMailLabel}, must(c.GetMessage(ctx, messageIDs[0])).LabelIDs) |
| 176 |
require.ElementsMatch(t, []string{proton.ArchiveLabel, proton.AllMailLabel}, must(c.GetMessage(ctx, messageIDs[1])).LabelIDs) |
| 177 |
require.ElementsMatch(t, []string{proton.ArchiveLabel, proton.AllMailLabel}, must(c.GetMessage(ctx, messageIDs[2])).LabelIDs) |
| 178 |
|
| 179 |
// Put them in inbox. |
| 180 |
require.NoError(t, c.LabelMessages(ctx, []string{messageIDs[0], messageIDs[1], messageIDs[2]}, proton.ArchiveLabel)) |
| 181 |
}) |
| 182 |
}) |
| 183 |
}) |
| 184 |
} |
| 185 |
|
| 186 |
func TestServer_GetMessageMetadataPage(t *testing.T) { |
| 187 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 188 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 189 |
withMessages(ctx, t, c, "pass", 1000, func(messageIDs []string) { |
| 190 |
for _, chunk := range xslices.Chunk(messageIDs, 150) { |
| 191 |
// Get the messages. |
| 192 |
metadata, err := c.GetMessageMetadataPage(ctx, 0, 150, proton.MessageFilter{ID: chunk}) |
| 193 |
require.NoError(t, err) |
| 194 |
|
| 195 |
// The messages should be the ones we created. |
| 196 |
require.ElementsMatch(t, chunk, xslices.Map(metadata, func(metadata proton.MessageMetadata) string { |
| 197 |
return metadata.ID |
| 198 |
})) |
| 199 |
|
| 200 |
} |
| 201 |
}) |
| 202 |
}) |
| 203 |
}) |
| 204 |
} |
| 205 |
|
| 206 |
func TestServer_MessageFilter(t *testing.T) { |
| 207 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 208 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 209 |
withMessages(ctx, t, c, "pass", 1000, func(messageIDs []string) { |
| 210 |
// Get the messages. |
| 211 |
metadata, err := c.GetMessageMetadata(ctx, proton.MessageFilter{}) |
| 212 |
require.NoError(t, err) |
| 213 |
|
| 214 |
// The messages should be the ones we created. |
| 215 |
require.ElementsMatch(t, messageIDs, xslices.Map(metadata, func(metadata proton.MessageMetadata) string { |
| 216 |
return metadata.ID |
| 217 |
})) |
| 218 |
|
| 219 |
// Get metadata for just the first three messages. |
| 220 |
partial, err := c.GetMessageMetadata(ctx, proton.MessageFilter{ |
| 221 |
ID: []string{ |
| 222 |
metadata[0].ID, |
| 223 |
metadata[1].ID, |
| 224 |
metadata[2].ID, |
| 225 |
}, |
| 226 |
}) |
| 227 |
require.NoError(t, err) |
| 228 |
|
| 229 |
// The messages should be just the first three. |
| 230 |
require.Equal(t, metadata[:3], partial) |
| 231 |
}) |
| 232 |
}) |
| 233 |
}) |
| 234 |
} |
| 235 |
|
| 236 |
func TestServer_MessageFilterDesc(t *testing.T) { |
| 237 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 238 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 239 |
withMessages(ctx, t, c, "pass", 100, func(messageIDs []string) { |
| 240 |
allMetadata := make([]proton.MessageMetadata, 0, 100) |
| 241 |
|
| 242 |
// first request. |
| 243 |
{ |
| 244 |
metadata, err := c.GetMessageMetadataPage(ctx, 0, 10, proton.MessageFilter{Desc: true}) |
| 245 |
require.NoError(t, err) |
| 246 |
|
| 247 |
allMetadata = append(allMetadata, metadata...) |
| 248 |
} |
| 249 |
|
| 250 |
for i := 1; i < 11; i++ { |
| 251 |
// Get the messages. |
| 252 |
metadata, err := c.GetMessageMetadataPage(ctx, 0, 10, proton.MessageFilter{Desc: true, EndID: allMetadata[len(allMetadata)-1].ID}) |
| 253 |
require.NoError(t, err) |
| 254 |
require.NotEmpty(t, metadata) |
| 255 |
require.Equal(t, metadata[0].ID, allMetadata[len(allMetadata)-1].ID) |
| 256 |
allMetadata = append(allMetadata, metadata[1:]...) |
| 257 |
} |
| 258 |
|
| 259 |
// Final check. Asking for EndID as last message multiple times will always return the last id. |
| 260 |
metadata, err := c.GetMessageMetadataPage(ctx, 0, 10, proton.MessageFilter{Desc: true, EndID: allMetadata[len(allMetadata)-1].ID}) |
| 261 |
require.NoError(t, err) |
| 262 |
require.Len(t, metadata, 1) |
| 263 |
require.Equal(t, metadata[0].ID, allMetadata[len(allMetadata)-1].ID) |
| 264 |
|
| 265 |
// The messages should be the ones we created. |
| 266 |
require.ElementsMatch(t, messageIDs, xslices.Map(allMetadata, func(metadata proton.MessageMetadata) string { |
| 267 |
return metadata.ID |
| 268 |
})) |
| 269 |
}) |
| 270 |
}) |
| 271 |
}) |
| 272 |
} |
| 273 |
|
| 274 |
func TestServer_MessageIDs(t *testing.T) { |
| 275 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 276 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 277 |
withMessages(ctx, t, c, "pass", 10000, func(wantMessageIDs []string) { |
| 278 |
allMessageIDs, err := c.GetAllMessageIDs(ctx, "") |
| 279 |
require.NoError(t, err) |
| 280 |
require.ElementsMatch(t, wantMessageIDs, allMessageIDs) |
| 281 |
|
| 282 |
halfMessageIDs, err := c.GetAllMessageIDs(ctx, allMessageIDs[len(allMessageIDs)/2]) |
| 283 |
require.NoError(t, err) |
| 284 |
require.ElementsMatch(t, allMessageIDs[len(allMessageIDs)/2+1:], halfMessageIDs) |
| 285 |
}) |
| 286 |
}) |
| 287 |
}) |
| 288 |
} |
| 289 |
|
| 290 |
func TestServer_MessagesDelete(t *testing.T) { |
| 291 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 292 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 293 |
withMessages(ctx, t, c, "pass", 1000, func(messageIDs []string) { |
| 294 |
// Get the messages. |
| 295 |
metadata, err := c.GetMessageMetadata(ctx, proton.MessageFilter{}) |
| 296 |
require.NoError(t, err) |
| 297 |
|
| 298 |
// The messages should be the ones we created. |
| 299 |
require.ElementsMatch(t, messageIDs, xslices.Map(metadata, func(metadata proton.MessageMetadata) string { |
| 300 |
return metadata.ID |
| 301 |
})) |
| 302 |
|
| 303 |
// Delete half the messages. |
| 304 |
require.NoError(t, c.DeleteMessage(ctx, messageIDs[0:500]...)) |
| 305 |
|
| 306 |
// Get the remaining messages. |
| 307 |
remaining, err := c.GetMessageMetadata(ctx, proton.MessageFilter{}) |
| 308 |
require.NoError(t, err) |
| 309 |
|
| 310 |
// The remaining messages should be the ones we didn't delete. |
| 311 |
require.ElementsMatch(t, messageIDs[500:], xslices.Map(remaining, func(metadata proton.MessageMetadata) string { |
| 312 |
return metadata.ID |
| 313 |
})) |
| 314 |
}) |
| 315 |
}) |
| 316 |
}) |
| 317 |
} |
| 318 |
|
| 319 |
func TestServer_MessagesDeleteAfterUpdate(t *testing.T) { |
| 320 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 321 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 322 |
withMessages(ctx, t, c, "pass", 1000, func(messageIDs []string) { |
| 323 |
// Get the initial event ID. |
| 324 |
eventID, err := c.GetLatestEventID(ctx) |
| 325 |
require.NoError(t, err) |
| 326 |
|
| 327 |
// Put half the messages in archive. |
| 328 |
require.NoError(t, c.LabelMessages(ctx, messageIDs[0:500], proton.ArchiveLabel)) |
| 329 |
|
| 330 |
// Delete half the messages. |
| 331 |
require.NoError(t, c.DeleteMessage(ctx, messageIDs[0:500]...)) |
| 332 |
|
| 333 |
// Get the event reflecting this change. |
| 334 |
event, more, err := c.GetEvent(ctx, eventID) |
| 335 |
require.NoError(t, err) |
| 336 |
require.False(t, more) |
| 337 |
require.Equal(t, 1, len(event)) |
| 338 |
|
| 339 |
// The event should have the correct number of message events. |
| 340 |
require.Len(t, event[0].Messages, 500) |
| 341 |
|
| 342 |
// All the events should be delete events. |
| 343 |
for _, message := range event[0].Messages { |
| 344 |
require.Equal(t, proton.EventDelete, message.Action) |
| 345 |
} |
| 346 |
}) |
| 347 |
}) |
| 348 |
}) |
| 349 |
} |
| 350 |
|
| 351 |
func TestServer_Events(t *testing.T) { |
| 352 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 353 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 354 |
withMessages(ctx, t, c, "pass", 3, func(messageIDs []string) { |
| 355 |
// Get the latest event ID to stream from. |
| 356 |
fromEventID, err := c.GetLatestEventID(ctx) |
| 357 |
require.NoError(t, err) |
| 358 |
|
| 359 |
// Begin collecting events. |
| 360 |
eventCh := c.NewEventStream(ctx, time.Second, 0, fromEventID) |
| 361 |
|
| 362 |
// Mark a message as read. |
| 363 |
require.NoError(t, c.MarkMessagesRead(ctx, messageIDs[0])) |
| 364 |
|
| 365 |
// The message should eventually be read. |
| 366 |
require.Eventually(t, func() bool { |
| 367 |
event := <-eventCh |
| 368 |
|
| 369 |
if len(event.Messages) != 1 { |
| 370 |
return false |
| 371 |
} |
| 372 |
|
| 373 |
if event.Messages[0].ID != messageIDs[0] { |
| 374 |
return false |
| 375 |
} |
| 376 |
|
| 377 |
return !bool(event.Messages[0].Message.Unread) |
| 378 |
}, 5*time.Second, time.Millisecond*100) |
| 379 |
|
| 380 |
// Add another message to archive. |
| 381 |
require.NoError(t, c.LabelMessages(ctx, []string{messageIDs[1]}, proton.ArchiveLabel)) |
| 382 |
|
| 383 |
// The message should eventually be in archive and all mail. |
| 384 |
require.Eventually(t, func() bool { |
| 385 |
event := <-eventCh |
| 386 |
|
| 387 |
if len(event.Messages) != 1 { |
| 388 |
return false |
| 389 |
} |
| 390 |
|
| 391 |
if event.Messages[0].ID != messageIDs[1] { |
| 392 |
return false |
| 393 |
} |
| 394 |
|
| 395 |
return elementsMatch([]string{proton.ArchiveLabel, proton.AllMailLabel}, event.Messages[0].Message.LabelIDs) |
| 396 |
}, 5*time.Second, time.Millisecond*100) |
| 397 |
|
| 398 |
// Perform a sequence of actions on the same message. |
| 399 |
require.NoError(t, c.LabelMessages(ctx, []string{messageIDs[2]}, proton.TrashLabel)) |
| 400 |
require.NoError(t, c.UnlabelMessages(ctx, []string{messageIDs[2]}, proton.TrashLabel)) |
| 401 |
require.NoError(t, c.MarkMessagesRead(ctx, messageIDs[2])) |
| 402 |
require.NoError(t, c.MarkMessagesUnread(ctx, messageIDs[2])) |
| 403 |
|
| 404 |
// The final state of the message should be correct. |
| 405 |
require.Eventually(t, func() bool { |
| 406 |
event := <-eventCh |
| 407 |
|
| 408 |
if len(event.Messages) != 1 { |
| 409 |
return false |
| 410 |
} |
| 411 |
|
| 412 |
if event.Messages[0].ID != messageIDs[2] { |
| 413 |
return false |
| 414 |
} |
| 415 |
|
| 416 |
return bool(event.Messages[0].Message.Unread) && elementsMatch([]string{proton.AllMailLabel}, event.Messages[0].Message.LabelIDs) |
| 417 |
}, 5*time.Second, time.Millisecond*100) |
| 418 |
|
| 419 |
// No more events should be sent. |
| 420 |
select { |
| 421 |
case <-eventCh: |
| 422 |
t.Fatal("unexpected event") |
| 423 |
|
| 424 |
default: |
| 425 |
// .... |
| 426 |
} |
| 427 |
}) |
| 428 |
}) |
| 429 |
}) |
| 430 |
} |
| 431 |
|
| 432 |
func TestServer_Events_Multi(t *testing.T) { |
| 433 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 434 |
for i := 0; i < 10; i++ { |
| 435 |
withUser(ctx, t, s, m, fmt.Sprintf("user%v", i), "pass", func(c *proton.Client) { |
| 436 |
latest, err := c.GetLatestEventID(ctx) |
| 437 |
require.NoError(t, err) |
| 438 |
|
| 439 |
// Fetching latest again should return the same event ID. |
| 440 |
latestAgain, err := c.GetLatestEventID(ctx) |
| 441 |
require.NoError(t, err) |
| 442 |
require.Equal(t, latest, latestAgain) |
| 443 |
|
| 444 |
events, more, err := c.GetEvent(ctx, latest) |
| 445 |
require.NoError(t, err) |
| 446 |
require.False(t, more) |
| 447 |
|
| 448 |
// The event should be empty. |
| 449 |
require.Equal(t, []proton.Event{{EventID: events[0].EventID}}, events) |
| 450 |
|
| 451 |
// After fetching an empty event, its ID should still be the latest. |
| 452 |
eventAgain, more, err := c.GetEvent(ctx, events[0].EventID) |
| 453 |
require.NoError(t, err) |
| 454 |
require.False(t, more) |
| 455 |
require.Equal(t, eventAgain[0].EventID, events[0].EventID) |
| 456 |
}) |
| 457 |
} |
| 458 |
}) |
| 459 |
} |
| 460 |
|
| 461 |
func TestServer_Events_Refresh(t *testing.T) { |
| 462 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 463 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 464 |
user, err := c.GetUser(ctx) |
| 465 |
require.NoError(t, err) |
| 466 |
|
| 467 |
// Get the latest event ID to stream from. |
| 468 |
fromEventID, err := c.GetLatestEventID(ctx) |
| 469 |
require.NoError(t, err) |
| 470 |
|
| 471 |
// Refresh the user's mail. |
| 472 |
require.NoError(t, s.RefreshUser(user.ID, proton.RefreshMail)) |
| 473 |
|
| 474 |
// Begin collecting events. |
| 475 |
eventCh := c.NewEventStream(ctx, time.Second, 0, fromEventID) |
| 476 |
|
| 477 |
// The user should eventually be refreshed. |
| 478 |
require.Eventually(t, func() bool { |
| 479 |
return (<-eventCh).Refresh&proton.RefreshMail != 0 |
| 480 |
}, 5*time.Second, time.Millisecond*100) |
| 481 |
}) |
| 482 |
}) |
| 483 |
} |
| 484 |
|
| 485 |
func TestServer_Events_UserSettings(t *testing.T) { |
| 486 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 487 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 488 |
user, err := c.GetUser(ctx) |
| 489 |
require.NoError(t, err) |
| 490 |
|
| 491 |
_, err = s.b.SetUserSettingsTelemetry(user.ID, proton.SettingDisabled) |
| 492 |
require.NoError(t, err) |
| 493 |
|
| 494 |
// Get the latest event ID to stream from. |
| 495 |
fromEventID, err := c.GetLatestEventID(ctx) |
| 496 |
require.NoError(t, err) |
| 497 |
|
| 498 |
// Refresh the user's mail. |
| 499 |
_, err = s.b.SetUserSettingsTelemetry(user.ID, proton.SettingEnabled) |
| 500 |
require.NoError(t, err) |
| 501 |
|
| 502 |
// Begin collecting events. |
| 503 |
eventCh := c.NewEventStream(ctx, time.Second, 0, fromEventID) |
| 504 |
|
| 505 |
// The user should eventually be refreshed. |
| 506 |
require.Eventually(t, func() bool { |
| 507 |
e := <-eventCh |
| 508 |
return e.UserSettings != nil && e.UserSettings.Telemetry == proton.SettingEnabled |
| 509 |
}, 5*time.Second, time.Millisecond*100) |
| 510 |
}) |
| 511 |
}) |
| 512 |
} |
| 513 |
|
| 514 |
func TestServer_RevokeUser(t *testing.T) { |
| 515 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 516 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 517 |
user, err := c.GetUser(ctx) |
| 518 |
require.NoError(t, err) |
| 519 |
require.Equal(t, "user", user.Name) |
| 520 |
require.Equal(t, "user@"+s.GetDomain(), user.Email) |
| 521 |
|
| 522 |
// Revoke the user's auth. |
| 523 |
require.NoError(t, s.RevokeUser(user.ID)) |
| 524 |
|
| 525 |
// Future requests should fail. |
| 526 |
require.Error(t, c.AuthDelete(ctx)) |
| 527 |
}) |
| 528 |
}) |
| 529 |
} |
| 530 |
|
| 531 |
func TestServer_Calls(t *testing.T) { |
| 532 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 533 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 534 |
var calls []Call |
| 535 |
|
| 536 |
// Watch calls that are made. |
| 537 |
s.AddCallWatcher(func(call Call) { |
| 538 |
calls = append(calls, call) |
| 539 |
}) |
| 540 |
|
| 541 |
// Get the user. |
| 542 |
_, err := c.GetUser(ctx) |
| 543 |
require.NoError(t, err) |
| 544 |
|
| 545 |
// Logout the user. |
| 546 |
require.NoError(t, c.AuthDelete(ctx)) |
| 547 |
|
| 548 |
// The user call should be correct. |
| 549 |
userCall := calls[0] |
| 550 |
require.Equal(t, "/core/v4/users", userCall.URL.Path) |
| 551 |
require.Equal(t, "GET", userCall.Method) |
| 552 |
require.Equal(t, http.StatusOK, userCall.Status) |
| 553 |
|
| 554 |
// The logout call should be correct. |
| 555 |
logoutCall := calls[1] |
| 556 |
require.Equal(t, "/auth/v4", logoutCall.URL.Path) |
| 557 |
require.Equal(t, "DELETE", logoutCall.Method) |
| 558 |
require.Equal(t, http.StatusOK, logoutCall.Status) |
| 559 |
}) |
| 560 |
}) |
| 561 |
} |
| 562 |
|
| 563 |
func TestServer_Calls_Status(t *testing.T) { |
| 564 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 565 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 566 |
var calls []Call |
| 567 |
|
| 568 |
// Watch calls that are made. |
| 569 |
s.AddCallWatcher(func(call Call) { |
| 570 |
calls = append(calls, call) |
| 571 |
}) |
| 572 |
|
| 573 |
// Make a bad call. |
| 574 |
_, err := c.GetMessage(ctx, "no such message ID") |
| 575 |
require.Error(t, err) |
| 576 |
|
| 577 |
// The user call should have error status. |
| 578 |
require.Equal(t, http.StatusUnprocessableEntity, calls[0].Status) |
| 579 |
}) |
| 580 |
}) |
| 581 |
} |
| 582 |
|
| 583 |
func TestServer_Calls_Request(t *testing.T) { |
| 584 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 585 |
var calls []Call |
| 586 |
|
| 587 |
s.AddCallWatcher(func(call Call) { |
| 588 |
calls = append(calls, call) |
| 589 |
}) |
| 590 |
|
| 591 |
withUser(ctx, t, s, m, "user", "pass", func(*proton.Client) { |
| 592 |
require.Equal( |
| 593 |
t, |
| 594 |
calls[0].RequestBody, |
| 595 |
must(json.Marshal(proton.AuthInfoReq{Username: "user"})), |
| 596 |
) |
| 597 |
}) |
| 598 |
}) |
| 599 |
} |
| 600 |
|
| 601 |
func TestServer_Calls_Response(t *testing.T) { |
| 602 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 603 |
var calls []Call |
| 604 |
|
| 605 |
s.AddCallWatcher(func(call Call) { |
| 606 |
calls = append(calls, call) |
| 607 |
}) |
| 608 |
|
| 609 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 610 |
salts, err := c.GetSalts(ctx) |
| 611 |
require.NoError(t, err) |
| 612 |
|
| 613 |
require.Equal( |
| 614 |
t, |
| 615 |
calls[len(calls)-1].ResponseBody, |
| 616 |
must(json.Marshal(struct{ KeySalts []proton.Salt }{salts})), |
| 617 |
) |
| 618 |
}) |
| 619 |
}) |
| 620 |
} |
| 621 |
|
| 622 |
func TestServer_Calls_Cookies(t *testing.T) { |
| 623 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 624 |
var calls []Call |
| 625 |
|
| 626 |
s.AddCallWatcher(func(call Call) { |
| 627 |
calls = append(calls, call) |
| 628 |
}) |
| 629 |
|
| 630 |
withUser(ctx, t, s, m, "user", "pass", func(*proton.Client) { |
| 631 |
// The header in the first call's response should set the Session-Id cookie. |
| 632 |
resHeader := (&http.Response{Header: calls[len(calls)-2].ResponseHeader}) |
| 633 |
require.Len(t, resHeader.Cookies(), 1) |
| 634 |
require.Equal(t, "Session-Id", resHeader.Cookies()[0].Name) |
| 635 |
require.NotEmpty(t, resHeader.Cookies()[0].Value) |
| 636 |
|
| 637 |
// The cookie should be sent in the next call. |
| 638 |
reqHeader := (&http.Request{Header: calls[len(calls)-1].RequestHeader}) |
| 639 |
require.Len(t, reqHeader.Cookies(), 1) |
| 640 |
require.Equal(t, "Session-Id", reqHeader.Cookies()[0].Name) |
| 641 |
require.NotEmpty(t, reqHeader.Cookies()[0].Value) |
| 642 |
|
| 643 |
// The cookie should be the same. |
| 644 |
require.Equal(t, resHeader.Cookies()[0].Value, reqHeader.Cookies()[0].Value) |
| 645 |
}) |
| 646 |
}) |
| 647 |
} |
| 648 |
|
| 649 |
func TestServer_Calls_Manager(t *testing.T) { |
| 650 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 651 |
var calls []Call |
| 652 |
|
| 653 |
// Watch calls that are made. |
| 654 |
s.AddCallWatcher(func(call Call) { |
| 655 |
calls = append(calls, call) |
| 656 |
}) |
| 657 |
|
| 658 |
// Make a non-user request. |
| 659 |
require.NoError(t, m.ReportBug(ctx, proton.ReportBugReq{})) |
| 660 |
|
| 661 |
// The call should be correct. |
| 662 |
reportCall := calls[0] |
| 663 |
require.Equal(t, "/core/v4/reports/bug", reportCall.URL.Path) |
| 664 |
require.Equal(t, "POST", reportCall.Method) |
| 665 |
require.Equal(t, http.StatusOK, reportCall.Status) |
| 666 |
}) |
| 667 |
} |
| 668 |
|
| 669 |
func TestServer_CreateMessage(t *testing.T) { |
| 670 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 671 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 672 |
user, err := c.GetUser(ctx) |
| 673 |
require.NoError(t, err) |
| 674 |
|
| 675 |
addr, err := c.GetAddresses(ctx) |
| 676 |
require.NoError(t, err) |
| 677 |
|
| 678 |
salt, err := c.GetSalts(ctx) |
| 679 |
require.NoError(t, err) |
| 680 |
|
| 681 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 682 |
require.NoError(t, err) |
| 683 |
|
| 684 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 685 |
require.NoError(t, err) |
| 686 |
|
| 687 |
draft, err := c.CreateDraft(ctx, addrKRs[addr[0].ID], proton.CreateDraftReq{ |
| 688 |
Message: proton.DraftTemplate{ |
| 689 |
Subject: "My subject", |
| 690 |
Sender: &mail.Address{Address: addr[0].Email}, |
| 691 |
ToList: []*mail.Address{{Address: "[email protected]"}}, |
| 692 |
}, |
| 693 |
}) |
| 694 |
require.NoError(t, err) |
| 695 |
|
| 696 |
require.Equal(t, addr[0].ID, draft.AddressID) |
| 697 |
require.Equal(t, "My subject", draft.Subject) |
| 698 |
require.Equal(t, &mail.Address{Address: "user@" + s.GetDomain()}, draft.Sender) |
| 699 |
require.Equal(t, []*mail.Address{{Address: "[email protected]"}}, draft.ToList) |
| 700 |
require.ElementsMatch(t, []string{proton.AllMailLabel, proton.AllDraftsLabel, proton.DraftsLabel}, draft.LabelIDs) |
| 701 |
}) |
| 702 |
}) |
| 703 |
} |
| 704 |
|
| 705 |
func TestServer_UpdateDraft(t *testing.T) { |
| 706 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 707 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 708 |
user, err := c.GetUser(ctx) |
| 709 |
require.NoError(t, err) |
| 710 |
|
| 711 |
addr, err := c.GetAddresses(ctx) |
| 712 |
require.NoError(t, err) |
| 713 |
|
| 714 |
salt, err := c.GetSalts(ctx) |
| 715 |
require.NoError(t, err) |
| 716 |
|
| 717 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 718 |
require.NoError(t, err) |
| 719 |
|
| 720 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 721 |
require.NoError(t, err) |
| 722 |
|
| 723 |
// Create the draft. |
| 724 |
draft, err := c.CreateDraft(ctx, addrKRs[addr[0].ID], proton.CreateDraftReq{ |
| 725 |
Message: proton.DraftTemplate{ |
| 726 |
Subject: "My subject", |
| 727 |
Sender: &mail.Address{Address: addr[0].Email}, |
| 728 |
ToList: []*mail.Address{{Address: "[email protected]"}}, |
| 729 |
}, |
| 730 |
}) |
| 731 |
require.NoError(t, err) |
| 732 |
require.Equal(t, addr[0].ID, draft.AddressID) |
| 733 |
require.Equal(t, "My subject", draft.Subject) |
| 734 |
require.Equal(t, &mail.Address{Address: "user@" + s.GetDomain()}, draft.Sender) |
| 735 |
require.Equal(t, []*mail.Address{{Address: "[email protected]"}}, draft.ToList) |
| 736 |
|
| 737 |
// Create an event stream to watch for an update event. |
| 738 |
fromEventID, err := c.GetLatestEventID(ctx) |
| 739 |
require.NoError(t, err) |
| 740 |
|
| 741 |
eventCh := c.NewEventStream(ctx, time.Second, 0, fromEventID) |
| 742 |
|
| 743 |
// Update the draft subject/to-list. |
| 744 |
msg, err := c.UpdateDraft(ctx, draft.ID, addrKRs[addr[0].ID], proton.UpdateDraftReq{ |
| 745 |
Message: proton.DraftTemplate{ |
| 746 |
Subject: "Edited subject", |
| 747 |
Sender: &mail.Address{Address: addr[0].Email}, |
| 748 |
ToList: []*mail.Address{{Address: "[email protected]"}}, |
| 749 |
MIMEType: rfc822.TextPlain, |
| 750 |
}, |
| 751 |
}) |
| 752 |
require.NoError(t, err) |
| 753 |
require.Equal(t, "Edited subject", msg.Subject) |
| 754 |
|
| 755 |
// We should eventually get an update event. |
| 756 |
require.Eventually(t, func() bool { |
| 757 |
event := <-eventCh |
| 758 |
|
| 759 |
if len(event.Messages) < 1 { |
| 760 |
return false |
| 761 |
} |
| 762 |
|
| 763 |
if event.Messages[0].ID != draft.ID { |
| 764 |
return false |
| 765 |
} |
| 766 |
|
| 767 |
if event.Messages[0].Action != proton.EventUpdate { |
| 768 |
return false |
| 769 |
} |
| 770 |
|
| 771 |
require.Equal(t, draft.ID, event.Messages[0].ID) |
| 772 |
require.Equal(t, "Edited subject", event.Messages[0].Message.Subject) |
| 773 |
require.Equal(t, []*mail.Address{{Address: "[email protected]"}}, event.Messages[0].Message.ToList) |
| 774 |
|
| 775 |
return true |
| 776 |
}, 5*time.Second, time.Millisecond*100) |
| 777 |
}) |
| 778 |
}) |
| 779 |
} |
| 780 |
|
| 781 |
func TestServer_SendMessage(t *testing.T) { |
| 782 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 783 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 784 |
user, err := c.GetUser(ctx) |
| 785 |
require.NoError(t, err) |
| 786 |
|
| 787 |
addr, err := c.GetAddresses(ctx) |
| 788 |
require.NoError(t, err) |
| 789 |
|
| 790 |
salt, err := c.GetSalts(ctx) |
| 791 |
require.NoError(t, err) |
| 792 |
|
| 793 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 794 |
require.NoError(t, err) |
| 795 |
|
| 796 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 797 |
require.NoError(t, err) |
| 798 |
|
| 799 |
draft, err := c.CreateDraft(ctx, addrKRs[addr[0].ID], proton.CreateDraftReq{ |
| 800 |
Message: proton.DraftTemplate{ |
| 801 |
Subject: "My subject", |
| 802 |
Sender: &mail.Address{Address: addr[0].Email}, |
| 803 |
ToList: []*mail.Address{{Address: "[email protected]"}}, |
| 804 |
}, |
| 805 |
}) |
| 806 |
require.NoError(t, err) |
| 807 |
|
| 808 |
sent, err := c.SendDraft(ctx, draft.ID, proton.SendDraftReq{}) |
| 809 |
require.NoError(t, err) |
| 810 |
|
| 811 |
require.Equal(t, draft.ID, sent.ID) |
| 812 |
require.Equal(t, addr[0].ID, sent.AddressID) |
| 813 |
require.Equal(t, "My subject", sent.Subject) |
| 814 |
require.Equal(t, []*mail.Address{{Address: "[email protected]"}}, sent.ToList) |
| 815 |
require.Contains(t, sent.LabelIDs, proton.SentLabel) |
| 816 |
}) |
| 817 |
}) |
| 818 |
} |
| 819 |
|
| 820 |
func TestServer_AuthDelete(t *testing.T) { |
| 821 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 822 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 823 |
require.NoError(t, c.AuthDelete(ctx)) |
| 824 |
}) |
| 825 |
}) |
| 826 |
} |
| 827 |
|
| 828 |
func TestServer_ForceUpgrade(t *testing.T) { |
| 829 |
ctx, cancel := context.WithCancel(context.Background()) |
| 830 |
defer cancel() |
| 831 |
|
| 832 |
s := New() |
| 833 |
defer s.Close() |
| 834 |
|
| 835 |
s.SetMinAppVersion(semver.MustParse("1.0.0")) |
| 836 |
|
| 837 |
if _, _, err := s.CreateUser("user", []byte("pass")); err != nil { |
| 838 |
t.Fatal(err) |
| 839 |
} |
| 840 |
|
| 841 |
m := proton.New( |
| 842 |
proton.WithHostURL(s.GetHostURL()), |
| 843 |
proton.WithAppVersion("proton_0.9.0"), |
| 844 |
proton.WithTransport(proton.InsecureTransport()), |
| 845 |
) |
| 846 |
defer m.Close() |
| 847 |
|
| 848 |
var called bool |
| 849 |
|
| 850 |
m.AddErrorHandler(proton.AppVersionBadCode, func() { |
| 851 |
called = true |
| 852 |
}) |
| 853 |
|
| 854 |
if _, _, err := m.NewClientWithLogin(ctx, "user", []byte("pass")); err == nil { |
| 855 |
t.Fatal(err) |
| 856 |
} |
| 857 |
|
| 858 |
require.True(t, called) |
| 859 |
} |
| 860 |
|
| 861 |
func TestServer_Import(t *testing.T) { |
| 862 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 863 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 864 |
ctx, cancel := context.WithCancel(ctx) |
| 865 |
defer cancel() |
| 866 |
|
| 867 |
user, err := c.GetUser(ctx) |
| 868 |
require.NoError(t, err) |
| 869 |
|
| 870 |
addr, err := c.GetAddresses(ctx) |
| 871 |
require.NoError(t, err) |
| 872 |
|
| 873 |
salt, err := c.GetSalts(ctx) |
| 874 |
require.NoError(t, err) |
| 875 |
|
| 876 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 877 |
require.NoError(t, err) |
| 878 |
|
| 879 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 880 |
require.NoError(t, err) |
| 881 |
|
| 882 |
res := importMessages(ctx, t, c, addr[0].ID, addrKRs[addr[0].ID], []string{}, proton.MessageFlagReceived, 1) |
| 883 |
require.NoError(t, err) |
| 884 |
require.Len(t, res, 1) |
| 885 |
require.Equal(t, proton.SuccessCode, res[0].Code) |
| 886 |
|
| 887 |
message, err := c.GetMessage(ctx, res[0].MessageID) |
| 888 |
require.NoError(t, err) |
| 889 |
|
| 890 |
dec, err := message.Decrypt(addrKRs[message.AddressID]) |
| 891 |
require.NoError(t, err) |
| 892 |
require.NotEmpty(t, dec) |
| 893 |
}) |
| 894 |
}) |
| 895 |
} |
| 896 |
|
| 897 |
func TestServer_Import_Dedup(t *testing.T) { |
| 898 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 899 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 900 |
ctx, cancel := context.WithCancel(ctx) |
| 901 |
defer cancel() |
| 902 |
|
| 903 |
user, err := c.GetUser(ctx) |
| 904 |
require.NoError(t, err) |
| 905 |
|
| 906 |
addr, err := c.GetAddresses(ctx) |
| 907 |
require.NoError(t, err) |
| 908 |
|
| 909 |
salt, err := c.GetSalts(ctx) |
| 910 |
require.NoError(t, err) |
| 911 |
|
| 912 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 913 |
require.NoError(t, err) |
| 914 |
|
| 915 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 916 |
require.NoError(t, err) |
| 917 |
|
| 918 |
subjectGenerator := func() string { |
| 919 |
return "my Subject" |
| 920 |
} |
| 921 |
|
| 922 |
res := importMessagesWithSubjectGenerator( |
| 923 |
ctx, |
| 924 |
t, |
| 925 |
c, |
| 926 |
addr[0].ID, |
| 927 |
addrKRs[addr[0].ID], |
| 928 |
[]string{}, |
| 929 |
proton.MessageFlagReceived, |
| 930 |
1, |
| 931 |
subjectGenerator, |
| 932 |
) |
| 933 |
require.NoError(t, err) |
| 934 |
require.Len(t, res, 1) |
| 935 |
require.Equal(t, proton.SuccessCode, res[0].Code) |
| 936 |
|
| 937 |
message, err := c.GetMessage(ctx, res[0].MessageID) |
| 938 |
require.NoError(t, err) |
| 939 |
|
| 940 |
dec, err := message.Decrypt(addrKRs[message.AddressID]) |
| 941 |
require.NoError(t, err) |
| 942 |
require.NotEmpty(t, dec) |
| 943 |
|
| 944 |
// Import message again should be deduped. |
| 945 |
resDedup := importMessagesWithSubjectGenerator( |
| 946 |
ctx, |
| 947 |
t, |
| 948 |
c, |
| 949 |
addr[0].ID, |
| 950 |
addrKRs[addr[0].ID], |
| 951 |
[]string{}, |
| 952 |
proton.MessageFlagReceived, |
| 953 |
1, |
| 954 |
subjectGenerator, |
| 955 |
) |
| 956 |
require.NoError(t, err) |
| 957 |
require.Len(t, resDedup, 1) |
| 958 |
require.Equal(t, proton.SuccessCode, resDedup[0].Code) |
| 959 |
require.Equal(t, res[0].MessageID, resDedup[0].MessageID) |
| 960 |
}) |
| 961 |
}, WithMessageDedup()) |
| 962 |
} |
| 963 |
|
| 964 |
func TestServer_Labels(t *testing.T) { |
| 965 |
type add string |
| 966 |
type rem string |
| 967 |
|
| 968 |
tests := []struct { |
| 969 |
name string |
| 970 |
flags proton.MessageFlag |
| 971 |
actions []any |
| 972 |
wantLabelIDs []string |
| 973 |
wantError bool |
| 974 |
}{ |
| 975 |
{ |
| 976 |
name: "received flag, no actions", |
| 977 |
flags: proton.MessageFlagReceived, |
| 978 |
wantLabelIDs: []string{proton.AllMailLabel}, |
| 979 |
}, |
| 980 |
{ |
| 981 |
name: "sent flag, no actions", |
| 982 |
flags: proton.MessageFlagSent, |
| 983 |
wantLabelIDs: []string{proton.AllMailLabel, proton.AllSentLabel}, |
| 984 |
}, |
| 985 |
{ |
| 986 |
name: "scheduled flag, no actions", |
| 987 |
flags: proton.MessageFlagScheduledSend, |
| 988 |
wantLabelIDs: []string{proton.AllMailLabel, proton.AllSentLabel}, |
| 989 |
}, |
| 990 |
{ |
| 991 |
name: "received flag, add inbox", |
| 992 |
flags: proton.MessageFlagReceived, |
| 993 |
actions: []any{add(proton.InboxLabel)}, |
| 994 |
wantLabelIDs: []string{proton.InboxLabel, proton.AllMailLabel}, |
| 995 |
}, |
| 996 |
{ |
| 997 |
name: "sent flag, add sent", |
| 998 |
flags: proton.MessageFlagSent, |
| 999 |
actions: []any{add(proton.SentLabel)}, |
| 1000 |
wantLabelIDs: []string{proton.SentLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1001 |
}, |
| 1002 |
{ |
| 1003 |
name: "scheduled flag, add scheduled", |
| 1004 |
flags: proton.MessageFlagScheduledSend, |
| 1005 |
actions: []any{add(proton.AllScheduledLabel)}, |
| 1006 |
wantLabelIDs: []string{proton.AllScheduledLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1007 |
}, |
| 1008 |
{ |
| 1009 |
name: "received flag, add inbox then add archive", |
| 1010 |
flags: proton.MessageFlagReceived, |
| 1011 |
actions: []any{add(proton.InboxLabel), add(proton.ArchiveLabel)}, |
| 1012 |
wantLabelIDs: []string{proton.ArchiveLabel, proton.AllMailLabel}, |
| 1013 |
}, |
| 1014 |
{ |
| 1015 |
name: "sent flag, add sent then add archive", |
| 1016 |
flags: proton.MessageFlagSent, |
| 1017 |
actions: []any{add(proton.SentLabel), add(proton.ArchiveLabel)}, |
| 1018 |
wantLabelIDs: []string{proton.ArchiveLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1019 |
}, |
| 1020 |
{ |
| 1021 |
name: "scheduled flag, add scheduled then add archive", |
| 1022 |
flags: proton.MessageFlagScheduledSend, |
| 1023 |
actions: []any{add(proton.AllScheduledLabel), add(proton.ArchiveLabel)}, |
| 1024 |
wantLabelIDs: []string{proton.ArchiveLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1025 |
}, |
| 1026 |
{ |
| 1027 |
name: "received flag, add inbox then remove inbox", |
| 1028 |
flags: proton.MessageFlagReceived, |
| 1029 |
actions: []any{add(proton.InboxLabel), rem(proton.InboxLabel)}, |
| 1030 |
wantLabelIDs: []string{proton.AllMailLabel}, |
| 1031 |
}, |
| 1032 |
{ |
| 1033 |
name: "sent flag, add sent then remove sent", |
| 1034 |
flags: proton.MessageFlagSent, |
| 1035 |
actions: []any{add(proton.SentLabel), rem(proton.SentLabel)}, |
| 1036 |
wantLabelIDs: []string{proton.AllMailLabel, proton.AllSentLabel}, |
| 1037 |
}, |
| 1038 |
{ |
| 1039 |
name: "scheduled flag, add scheduled then remove scheduled", |
| 1040 |
flags: proton.MessageFlagScheduledSend, |
| 1041 |
actions: []any{add(proton.AllScheduledLabel), rem(proton.AllScheduledLabel)}, |
| 1042 |
wantLabelIDs: []string{proton.AllMailLabel, proton.AllSentLabel}, |
| 1043 |
}, |
| 1044 |
{ |
| 1045 |
name: "received flag, add inbox then remove archive", |
| 1046 |
flags: proton.MessageFlagReceived, |
| 1047 |
actions: []any{add(proton.InboxLabel), rem(proton.ArchiveLabel)}, |
| 1048 |
wantLabelIDs: []string{proton.InboxLabel, proton.AllMailLabel}, |
| 1049 |
}, |
| 1050 |
{ |
| 1051 |
name: "sent flag, add sent then remove archive", |
| 1052 |
flags: proton.MessageFlagSent, |
| 1053 |
actions: []any{add(proton.SentLabel), rem(proton.ArchiveLabel)}, |
| 1054 |
wantLabelIDs: []string{proton.SentLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1055 |
}, |
| 1056 |
{ |
| 1057 |
name: "scheduled flag, add scheduled then remove archive", |
| 1058 |
flags: proton.MessageFlagScheduledSend, |
| 1059 |
actions: []any{add(proton.AllScheduledLabel), rem(proton.ArchiveLabel)}, |
| 1060 |
wantLabelIDs: []string{proton.AllScheduledLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1061 |
}, |
| 1062 |
{ |
| 1063 |
name: "received flag, add inbox then remove inbox then add archive", |
| 1064 |
flags: proton.MessageFlagReceived, |
| 1065 |
actions: []any{add(proton.InboxLabel), rem(proton.InboxLabel), add(proton.ArchiveLabel)}, |
| 1066 |
wantLabelIDs: []string{proton.ArchiveLabel, proton.AllMailLabel}, |
| 1067 |
}, |
| 1068 |
{ |
| 1069 |
name: "sent flag, add sent then remove sent then add archive", |
| 1070 |
flags: proton.MessageFlagSent, |
| 1071 |
actions: []any{add(proton.SentLabel), rem(proton.SentLabel), add(proton.ArchiveLabel)}, |
| 1072 |
wantLabelIDs: []string{proton.ArchiveLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1073 |
}, |
| 1074 |
{ |
| 1075 |
name: "scheduled flag, add scheduled then remove scheduled then add archive", |
| 1076 |
flags: proton.MessageFlagScheduledSend, |
| 1077 |
actions: []any{add(proton.AllScheduledLabel), rem(proton.AllScheduledLabel), add(proton.ArchiveLabel)}, |
| 1078 |
wantLabelIDs: []string{proton.ArchiveLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1079 |
}, |
| 1080 |
{ |
| 1081 |
name: "received flag, add starred", |
| 1082 |
flags: proton.MessageFlagReceived, |
| 1083 |
actions: []any{add(proton.StarredLabel)}, |
| 1084 |
wantLabelIDs: []string{proton.StarredLabel, proton.AllMailLabel}, |
| 1085 |
}, |
| 1086 |
{ |
| 1087 |
name: "sent flag, add starred", |
| 1088 |
flags: proton.MessageFlagSent, |
| 1089 |
actions: []any{add(proton.StarredLabel)}, |
| 1090 |
wantLabelIDs: []string{proton.StarredLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1091 |
}, |
| 1092 |
{ |
| 1093 |
name: "scheduled flag, add starred", |
| 1094 |
flags: proton.MessageFlagScheduledSend, |
| 1095 |
actions: []any{add(proton.StarredLabel)}, |
| 1096 |
wantLabelIDs: []string{proton.StarredLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1097 |
}, |
| 1098 |
{ |
| 1099 |
name: "received flag, add inbox, add starred, remove inbox", |
| 1100 |
flags: proton.MessageFlagReceived, |
| 1101 |
actions: []any{add(proton.InboxLabel), add(proton.StarredLabel), rem(proton.InboxLabel)}, |
| 1102 |
wantLabelIDs: []string{proton.StarredLabel, proton.AllMailLabel}, |
| 1103 |
}, |
| 1104 |
{ |
| 1105 |
name: "sent flag, add sent, add starred, remove sent", |
| 1106 |
flags: proton.MessageFlagSent, |
| 1107 |
actions: []any{add(proton.SentLabel), add(proton.StarredLabel), rem(proton.SentLabel)}, |
| 1108 |
wantLabelIDs: []string{proton.StarredLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1109 |
}, |
| 1110 |
{ |
| 1111 |
name: "scheduled flag, add scheduled, add starred, remove scheduled", |
| 1112 |
flags: proton.MessageFlagScheduledSend, |
| 1113 |
actions: []any{add(proton.AllScheduledLabel), add(proton.StarredLabel), rem(proton.AllScheduledLabel)}, |
| 1114 |
wantLabelIDs: []string{proton.StarredLabel, proton.AllMailLabel, proton.AllSentLabel}, |
| 1115 |
}, |
| 1116 |
{ |
| 1117 |
name: "received flag, add trash, remove trash", |
| 1118 |
flags: proton.MessageFlagReceived, |
| 1119 |
actions: []any{add(proton.TrashLabel), rem(proton.TrashLabel)}, |
| 1120 |
wantLabelIDs: []string{proton.AllMailLabel}, |
| 1121 |
}, |
| 1122 |
{ |
| 1123 |
name: "sent flag, add trash, remove trash", |
| 1124 |
flags: proton.MessageFlagSent, |
| 1125 |
actions: []any{add(proton.TrashLabel), rem(proton.TrashLabel)}, |
| 1126 |
wantLabelIDs: []string{proton.AllMailLabel, proton.AllSentLabel}, |
| 1127 |
}, |
| 1128 |
{ |
| 1129 |
name: "scheduled flag, add trash, remove trash", |
| 1130 |
flags: proton.MessageFlagScheduledSend, |
| 1131 |
actions: []any{add(proton.TrashLabel), rem(proton.TrashLabel)}, |
| 1132 |
wantLabelIDs: []string{proton.AllMailLabel, proton.AllSentLabel}, |
| 1133 |
}, |
| 1134 |
{ |
| 1135 |
name: "received flag, add inbox, add trash, remove inbox", |
| 1136 |
flags: proton.MessageFlagReceived, |
| 1137 |
actions: []any{add(proton.InboxLabel), add(proton.TrashLabel), rem(proton.InboxLabel)}, |
| 1138 |
wantLabelIDs: []string{proton.AllMailLabel, proton.TrashLabel}, |
| 1139 |
}, |
| 1140 |
{ |
| 1141 |
name: "scheduled & sent flags, add scheduled, add sent", |
| 1142 |
flags: proton.MessageFlagScheduledSend | proton.MessageFlagSent, |
| 1143 |
actions: []any{add(proton.AllScheduledLabel), add(proton.SentLabel)}, |
| 1144 |
wantLabelIDs: []string{proton.AllMailLabel, proton.SentLabel, proton.AllSentLabel}, |
| 1145 |
}, |
| 1146 |
} |
| 1147 |
|
| 1148 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1149 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1150 |
ctx, cancel := context.WithCancel(ctx) |
| 1151 |
defer cancel() |
| 1152 |
|
| 1153 |
user, err := c.GetUser(ctx) |
| 1154 |
require.NoError(t, err) |
| 1155 |
|
| 1156 |
addr, err := c.GetAddresses(ctx) |
| 1157 |
require.NoError(t, err) |
| 1158 |
|
| 1159 |
salt, err := c.GetSalts(ctx) |
| 1160 |
require.NoError(t, err) |
| 1161 |
|
| 1162 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 1163 |
require.NoError(t, err) |
| 1164 |
|
| 1165 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 1166 |
require.NoError(t, err) |
| 1167 |
|
| 1168 |
for _, tt := range tests { |
| 1169 |
t.Run(tt.name, func(t *testing.T) { |
| 1170 |
res := importMessages(ctx, t, c, addr[0].ID, addrKRs[addr[0].ID], []string{}, tt.flags, 1) |
| 1171 |
|
| 1172 |
require.True(t, (func() error { |
| 1173 |
for _, action := range tt.actions { |
| 1174 |
switch action := action.(type) { |
| 1175 |
case add: |
| 1176 |
if err := c.LabelMessages(ctx, []string{res[0].MessageID}, string(action)); err != nil { |
| 1177 |
return err |
| 1178 |
} |
| 1179 |
|
| 1180 |
case rem: |
| 1181 |
if err := c.UnlabelMessages(ctx, []string{res[0].MessageID}, string(action)); err != nil { |
| 1182 |
return err |
| 1183 |
} |
| 1184 |
} |
| 1185 |
} |
| 1186 |
|
| 1187 |
return nil |
| 1188 |
}() != nil) == tt.wantError) |
| 1189 |
|
| 1190 |
message, err := c.GetMessage(ctx, res[0].MessageID) |
| 1191 |
require.NoError(t, err) |
| 1192 |
|
| 1193 |
// The message should be in the correct labels. |
| 1194 |
require.ElementsMatch(t, tt.wantLabelIDs, message.LabelIDs) |
| 1195 |
|
| 1196 |
// The flags should be preserved after import. |
| 1197 |
require.True(t, message.Flags&tt.flags == tt.flags) |
| 1198 |
}) |
| 1199 |
} |
| 1200 |
}) |
| 1201 |
}) |
| 1202 |
} |
| 1203 |
|
| 1204 |
func TestServer_Import_FlagsAndLabels(t *testing.T) { |
| 1205 |
tests := []struct { |
| 1206 |
name string |
| 1207 |
labelIDs []string |
| 1208 |
flags proton.MessageFlag |
| 1209 |
wantLabelIDs []string |
| 1210 |
wantError bool |
| 1211 |
}{ |
| 1212 |
{ |
| 1213 |
name: "received flag --> no label", |
| 1214 |
flags: proton.MessageFlagReceived, |
| 1215 |
wantLabelIDs: []string{proton.AllMailLabel}, |
| 1216 |
}, |
| 1217 |
{ |
| 1218 |
name: "received flag --> inbox", |
| 1219 |
labelIDs: []string{proton.InboxLabel}, |
| 1220 |
flags: proton.MessageFlagReceived, |
| 1221 |
wantLabelIDs: []string{proton.InboxLabel, proton.AllMailLabel}, |
| 1222 |
}, |
| 1223 |
{ |
| 1224 |
name: "sent flag --> sent", |
| 1225 |
labelIDs: []string{proton.SentLabel}, |
| 1226 |
flags: proton.MessageFlagSent, |
| 1227 |
wantLabelIDs: []string{proton.SentLabel, proton.AllSentLabel, proton.AllMailLabel}, |
| 1228 |
}, |
| 1229 |
{ |
| 1230 |
name: "received flag --> sent", |
| 1231 |
labelIDs: []string{proton.SentLabel}, |
| 1232 |
flags: proton.MessageFlagReceived, |
| 1233 |
wantLabelIDs: []string{proton.InboxLabel, proton.AllMailLabel}, |
| 1234 |
}, |
| 1235 |
{ |
| 1236 |
name: "sent flag --> inbox", |
| 1237 |
labelIDs: []string{proton.InboxLabel}, |
| 1238 |
flags: proton.MessageFlagSent, |
| 1239 |
wantLabelIDs: []string{proton.SentLabel, proton.AllSentLabel, proton.AllMailLabel}, |
| 1240 |
}, |
| 1241 |
{ |
| 1242 |
name: "no flag --> drafts", |
| 1243 |
labelIDs: []string{proton.DraftsLabel}, |
| 1244 |
wantLabelIDs: []string{proton.DraftsLabel, proton.AllDraftsLabel, proton.AllMailLabel}, |
| 1245 |
}, |
| 1246 |
{ |
| 1247 |
name: "forbidden: received flag --> All Mail", |
| 1248 |
labelIDs: []string{proton.AllMailLabel}, |
| 1249 |
flags: proton.MessageFlagReceived, |
| 1250 |
wantError: true, |
| 1251 |
}, |
| 1252 |
{ |
| 1253 |
name: "forbidden: sent flag --> All Mail", |
| 1254 |
labelIDs: []string{proton.AllMailLabel}, |
| 1255 |
flags: proton.MessageFlagSent, |
| 1256 |
wantError: true, |
| 1257 |
}, |
| 1258 |
{ |
| 1259 |
name: "forbidden: received flag --> inbox and all mail", |
| 1260 |
labelIDs: []string{proton.InboxLabel, proton.AllMailLabel}, |
| 1261 |
flags: proton.MessageFlagReceived, |
| 1262 |
wantError: true, |
| 1263 |
}, |
| 1264 |
{ |
| 1265 |
name: "forbidden: sent flag --> sent and all mail", |
| 1266 |
labelIDs: []string{proton.SentLabel, proton.AllMailLabel}, |
| 1267 |
flags: proton.MessageFlagSent, |
| 1268 |
wantError: true, |
| 1269 |
}, |
| 1270 |
{ |
| 1271 |
name: "forbidden: received flag --> inbox and sent", |
| 1272 |
labelIDs: []string{proton.InboxLabel, proton.SentLabel}, |
| 1273 |
flags: proton.MessageFlagReceived, |
| 1274 |
wantError: true, |
| 1275 |
}, |
| 1276 |
{ |
| 1277 |
name: "forbidden: sent flag --> inbox and sent", |
| 1278 |
labelIDs: []string{proton.InboxLabel, proton.SentLabel}, |
| 1279 |
flags: proton.MessageFlagSent, |
| 1280 |
wantError: true, |
| 1281 |
}, |
| 1282 |
{ |
| 1283 |
name: "forbidden: received flag --> inbox and archive", |
| 1284 |
labelIDs: []string{proton.InboxLabel, proton.ArchiveLabel}, |
| 1285 |
flags: proton.MessageFlagReceived, |
| 1286 |
wantError: true, |
| 1287 |
}, |
| 1288 |
} |
| 1289 |
|
| 1290 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1291 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1292 |
ctx, cancel := context.WithCancel(ctx) |
| 1293 |
defer cancel() |
| 1294 |
|
| 1295 |
user, err := c.GetUser(ctx) |
| 1296 |
require.NoError(t, err) |
| 1297 |
|
| 1298 |
addr, err := c.GetAddresses(ctx) |
| 1299 |
require.NoError(t, err) |
| 1300 |
|
| 1301 |
salt, err := c.GetSalts(ctx) |
| 1302 |
require.NoError(t, err) |
| 1303 |
|
| 1304 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 1305 |
require.NoError(t, err) |
| 1306 |
|
| 1307 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 1308 |
require.NoError(t, err) |
| 1309 |
|
| 1310 |
for _, tt := range tests { |
| 1311 |
t.Run(tt.name, func(t *testing.T) { |
| 1312 |
str, err := c.ImportMessages(ctx, addrKRs[addr[0].ID], runtime.NumCPU(), runtime.NumCPU(), []proton.ImportReq{{ |
| 1313 |
Metadata: proton.ImportMetadata{ |
| 1314 |
AddressID: addr[0].ID, |
| 1315 |
Flags: tt.flags, |
| 1316 |
LabelIDs: tt.labelIDs, |
| 1317 |
}, |
| 1318 |
Message: newMessageLiteral("[email protected]", "[email protected]"), |
| 1319 |
}}...) |
| 1320 |
require.NoError(t, err) |
| 1321 |
|
| 1322 |
res, err := stream.Collect(ctx, str) |
| 1323 |
if tt.wantError { |
| 1324 |
require.Error(t, err) |
| 1325 |
} else { |
| 1326 |
require.NoError(t, err) |
| 1327 |
require.Equal(t, proton.SuccessCode, res[0].Code) |
| 1328 |
|
| 1329 |
message, err := c.GetMessage(ctx, res[0].MessageID) |
| 1330 |
require.NoError(t, err) |
| 1331 |
|
| 1332 |
// The message should be in the correct labels. |
| 1333 |
require.ElementsMatch(t, tt.wantLabelIDs, message.LabelIDs) |
| 1334 |
|
| 1335 |
// The flags should be preserved after import. |
| 1336 |
require.True(t, message.Flags&tt.flags == tt.flags) |
| 1337 |
} |
| 1338 |
}) |
| 1339 |
} |
| 1340 |
}) |
| 1341 |
}) |
| 1342 |
} |
| 1343 |
|
| 1344 |
func TestServer_PublicKeys(t *testing.T) { |
| 1345 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1346 |
if _, _, err := s.CreateUser("other", []byte("pass")); err != nil { |
| 1347 |
t.Fatal(err) |
| 1348 |
} |
| 1349 |
|
| 1350 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1351 |
intKeys, intType, err := c.GetPublicKeys(ctx, "other@"+s.GetDomain()) |
| 1352 |
require.NoError(t, err) |
| 1353 |
require.Equal(t, proton.RecipientTypeInternal, intType) |
| 1354 |
require.Len(t, intKeys, 1) |
| 1355 |
|
| 1356 |
extKeys, extType, err := c.GetPublicKeys(ctx, "[email protected]") |
| 1357 |
require.NoError(t, err) |
| 1358 |
require.Equal(t, proton.RecipientTypeExternal, extType) |
| 1359 |
require.Len(t, extKeys, 0) |
| 1360 |
}) |
| 1361 |
}) |
| 1362 |
} |
| 1363 |
|
| 1364 |
func TestServer_Proxy(t *testing.T) { |
| 1365 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1366 |
var calls []Call |
| 1367 |
|
| 1368 |
s.AddCallWatcher(func(call Call) { |
| 1369 |
calls = append(calls, call) |
| 1370 |
}) |
| 1371 |
|
| 1372 |
withUser(ctx, t, s, m, "user", "pass", func(_ *proton.Client) { |
| 1373 |
proxy := New( |
| 1374 |
WithProxyOrigin(s.GetHostURL()), |
| 1375 |
WithProxyTransport(proton.InsecureTransport()), |
| 1376 |
) |
| 1377 |
defer proxy.Close() |
| 1378 |
|
| 1379 |
m := proton.New( |
| 1380 |
proton.WithHostURL(proxy.GetProxyURL()), |
| 1381 |
proton.WithTransport(proton.InsecureTransport()), |
| 1382 |
) |
| 1383 |
defer m.Close() |
| 1384 |
|
| 1385 |
// Login -- the call should be proxied to the upstream server. |
| 1386 |
c, _, err := m.NewClientWithLogin(ctx, "user", []byte("pass")) |
| 1387 |
require.NoError(t, err) |
| 1388 |
defer c.Close() |
| 1389 |
|
| 1390 |
// The results of the call should be correct. |
| 1391 |
user, err := c.GetUser(ctx) |
| 1392 |
require.NoError(t, err) |
| 1393 |
require.Equal(t, "user", user.Name) |
| 1394 |
}) |
| 1395 |
|
| 1396 |
// Assert that the calls were proxied. |
| 1397 |
require.Greater(t, len(calls), 0) |
| 1398 |
}) |
| 1399 |
} |
| 1400 |
|
| 1401 |
func TestServer_Proxy_Cache(t *testing.T) { |
| 1402 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1403 |
withUser(ctx, t, s, m, "user", "pass", func(_ *proton.Client) { |
| 1404 |
proxy := New( |
| 1405 |
WithProxyOrigin(s.GetHostURL()), |
| 1406 |
WithProxyTransport(proton.InsecureTransport()), |
| 1407 |
WithAuthCacher(NewAuthCache()), |
| 1408 |
) |
| 1409 |
defer proxy.Close() |
| 1410 |
|
| 1411 |
// Need to skip verifying the server proofs for the proxy cache feature to work! |
| 1412 |
m := proton.New( |
| 1413 |
proton.WithHostURL(proxy.GetProxyURL()), |
| 1414 |
proton.WithTransport(proton.InsecureTransport()), |
| 1415 |
proton.WithSkipVerifyProofs(), |
| 1416 |
) |
| 1417 |
defer m.Close() |
| 1418 |
|
| 1419 |
// Login 3 times; we should produce 1 unique auth. |
| 1420 |
require.Len(t, xslices.Unique(iterator.Collect(iterator.Map(iterator.Counter(3), func(int) string { |
| 1421 |
c, auth, err := m.NewClientWithLogin(ctx, "user", []byte("pass")) |
| 1422 |
require.NoError(t, err) |
| 1423 |
defer c.Close() |
| 1424 |
|
| 1425 |
return auth.UID |
| 1426 |
}))), 1) |
| 1427 |
}) |
| 1428 |
}) |
| 1429 |
} |
| 1430 |
|
| 1431 |
func TestServer_Proxy_AuthDelete(t *testing.T) { |
| 1432 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1433 |
withUser(ctx, t, s, m, "user", "pass", func(_ *proton.Client) { |
| 1434 |
proxy := New( |
| 1435 |
WithProxyOrigin(s.GetHostURL()), |
| 1436 |
WithProxyTransport(proton.InsecureTransport()), |
| 1437 |
WithAuthCacher(NewAuthCache()), |
| 1438 |
) |
| 1439 |
defer proxy.Close() |
| 1440 |
|
| 1441 |
// Need to skip verifying the server proofs for the proxy cache feature to work! |
| 1442 |
m := proton.New( |
| 1443 |
proton.WithHostURL(proxy.GetProxyURL()), |
| 1444 |
proton.WithTransport(proton.InsecureTransport()), |
| 1445 |
) |
| 1446 |
defer m.Close() |
| 1447 |
|
| 1448 |
// Watch for login -- the calls should be proxied. |
| 1449 |
var login []Call |
| 1450 |
|
| 1451 |
s.AddCallWatcher(func(call Call) { |
| 1452 |
login = append(login, call) |
| 1453 |
}) |
| 1454 |
|
| 1455 |
// Login -- the call should be proxied to the upstream server. |
| 1456 |
c, _, err := m.NewClientWithLogin(ctx, "user", []byte("pass")) |
| 1457 |
require.NoError(t, err) |
| 1458 |
defer c.Close() |
| 1459 |
|
| 1460 |
// Assert that the login was proxied. |
| 1461 |
require.NotEmpty(t, len(login)) |
| 1462 |
|
| 1463 |
// Watch for logout -- logout should not be proxied to the upstream server. |
| 1464 |
var logout []Call |
| 1465 |
|
| 1466 |
s.AddCallWatcher(func(call Call) { |
| 1467 |
logout = append(logout, call) |
| 1468 |
}) |
| 1469 |
|
| 1470 |
// Logout -- the call should not be proxied to the upstream server. |
| 1471 |
require.NoError(t, c.AuthDelete(ctx)) |
| 1472 |
|
| 1473 |
// Assert that the logout was not proxied! |
| 1474 |
require.Empty(t, len(logout)) |
| 1475 |
}) |
| 1476 |
}) |
| 1477 |
} |
| 1478 |
|
| 1479 |
func TestServer_RealProxy(t *testing.T) { |
| 1480 |
username := os.Getenv("GO_PROTON_API_TEST_USERNAME") |
| 1481 |
password := os.Getenv("GO_PROTON_API_TEST_PASSWORD") |
| 1482 |
|
| 1483 |
if username == "" || password == "" { |
| 1484 |
t.Skip("skipping test, set the username and password to run") |
| 1485 |
} |
| 1486 |
|
| 1487 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1488 |
defer cancel() |
| 1489 |
|
| 1490 |
proxy := New() |
| 1491 |
defer proxy.Close() |
| 1492 |
|
| 1493 |
m := proton.New( |
| 1494 |
proton.WithHostURL(proxy.GetProxyURL()), |
| 1495 |
proton.WithTransport(proton.InsecureTransport()), |
| 1496 |
) |
| 1497 |
defer m.Close() |
| 1498 |
|
| 1499 |
// Login -- the call should be proxied to the upstream server. |
| 1500 |
c, _, err := m.NewClientWithLogin(ctx, username, []byte(password)) |
| 1501 |
require.NoError(t, err) |
| 1502 |
defer c.Close() |
| 1503 |
|
| 1504 |
// The results of the call should be correct. |
| 1505 |
user, err := c.GetUser(ctx) |
| 1506 |
require.NoError(t, err) |
| 1507 |
require.Equal(t, username, user.Name) |
| 1508 |
} |
| 1509 |
|
| 1510 |
func TestServer_RealProxy_Cache(t *testing.T) { |
| 1511 |
username := os.Getenv("GO_PROTON_API_TEST_USERNAME") |
| 1512 |
password := os.Getenv("GO_PROTON_API_TEST_PASSWORD") |
| 1513 |
|
| 1514 |
if username == "" || password == "" { |
| 1515 |
t.Skip("skipping test, set the username and password to run") |
| 1516 |
} |
| 1517 |
|
| 1518 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1519 |
defer cancel() |
| 1520 |
|
| 1521 |
proxy := New(WithAuthCacher(NewAuthCache())) |
| 1522 |
defer proxy.Close() |
| 1523 |
|
| 1524 |
m := proton.New( |
| 1525 |
proton.WithHostURL(proxy.GetProxyURL()), |
| 1526 |
proton.WithTransport(proton.InsecureTransport()), |
| 1527 |
proton.WithSkipVerifyProofs(), |
| 1528 |
) |
| 1529 |
defer m.Close() |
| 1530 |
|
| 1531 |
// Login 3 times; we should produce 1 unique auth. |
| 1532 |
require.Len(t, xslices.Unique(iterator.Collect(iterator.Map(iterator.Counter(3), func(int) string { |
| 1533 |
c, auth, err := m.NewClientWithLogin(ctx, username, []byte(password)) |
| 1534 |
require.NoError(t, err) |
| 1535 |
defer c.Close() |
| 1536 |
|
| 1537 |
return auth.UID |
| 1538 |
}))), 1) |
| 1539 |
} |
| 1540 |
|
| 1541 |
func TestServer_Messages_Fetch(t *testing.T) { |
| 1542 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1543 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1544 |
withMessages(ctx, t, c, "pass", 1000, func(messageIDs []string) { |
| 1545 |
ctl := proton.NewNetCtl() |
| 1546 |
|
| 1547 |
mm := proton.New( |
| 1548 |
proton.WithHostURL(s.GetHostURL()), |
| 1549 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 1550 |
) |
| 1551 |
defer mm.Close() |
| 1552 |
|
| 1553 |
cc, _, err := mm.NewClientWithLogin(ctx, "user", []byte("pass")) |
| 1554 |
require.NoError(t, err) |
| 1555 |
defer cc.Close() |
| 1556 |
|
| 1557 |
total := countBytesRead(ctl, func() { |
| 1558 |
res, err := stream.Collect(ctx, getFullMessages(ctx, cc, runtime.NumCPU(), runtime.NumCPU(), messageIDs...)) |
| 1559 |
require.NoError(t, err) |
| 1560 |
require.NotEmpty(t, res) |
| 1561 |
}) |
| 1562 |
|
| 1563 |
ctl.SetReadLimit(total / 2) |
| 1564 |
|
| 1565 |
require.Less(t, countBytesRead(ctl, func() { |
| 1566 |
res, err := stream.Collect(ctx, getFullMessages(ctx, cc, runtime.NumCPU(), runtime.NumCPU(), messageIDs...)) |
| 1567 |
require.Error(t, err) |
| 1568 |
require.Empty(t, res) |
| 1569 |
}), total) |
| 1570 |
|
| 1571 |
ctl.SetReadLimit(0) |
| 1572 |
|
| 1573 |
require.Equal(t, countBytesRead(ctl, func() { |
| 1574 |
res, err := stream.Collect(ctx, getFullMessages(ctx, cc, runtime.NumCPU(), runtime.NumCPU(), messageIDs...)) |
| 1575 |
require.NoError(t, err) |
| 1576 |
require.NotEmpty(t, res) |
| 1577 |
}), total) |
| 1578 |
}) |
| 1579 |
}) |
| 1580 |
}, WithTLS(false)) |
| 1581 |
} |
| 1582 |
|
| 1583 |
func TestServer_Status(t *testing.T) { |
| 1584 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1585 |
withUser(ctx, t, s, m, "user", "pass", func(*proton.Client) { |
| 1586 |
ctl := proton.NewNetCtl() |
| 1587 |
|
| 1588 |
mm := proton.New( |
| 1589 |
proton.WithHostURL(s.GetHostURL()), |
| 1590 |
proton.WithTransport(ctl.NewRoundTripper(&tls.Config{InsecureSkipVerify: true})), |
| 1591 |
) |
| 1592 |
defer mm.Close() |
| 1593 |
|
| 1594 |
statusCh := make(chan proton.Status, 1) |
| 1595 |
|
| 1596 |
mm.AddStatusObserver(func(status proton.Status) { |
| 1597 |
statusCh <- status |
| 1598 |
}) |
| 1599 |
|
| 1600 |
cc, _, err := mm.NewClientWithLogin(ctx, "user", []byte("pass")) |
| 1601 |
require.NoError(t, err) |
| 1602 |
defer cc.Close() |
| 1603 |
|
| 1604 |
{ |
| 1605 |
user, err := cc.GetUser(ctx) |
| 1606 |
require.NoError(t, err) |
| 1607 |
require.Equal(t, "user", user.Name) |
| 1608 |
} |
| 1609 |
|
| 1610 |
ctl.SetCanRead(false) |
| 1611 |
|
| 1612 |
{ |
| 1613 |
_, err := cc.GetUser(ctx) |
| 1614 |
require.Error(t, err) |
| 1615 |
} |
| 1616 |
|
| 1617 |
require.Equal(t, proton.StatusDown, <-statusCh) |
| 1618 |
}) |
| 1619 |
}, WithTLS(false)) |
| 1620 |
} |
| 1621 |
|
| 1622 |
func TestServer_Labels_Duplicates(t *testing.T) { |
| 1623 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1624 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1625 |
req := proton.CreateLabelReq{ |
| 1626 |
Name: uuid.NewString(), |
| 1627 |
Color: "#f66", |
| 1628 |
Type: proton.LabelTypeLabel, |
| 1629 |
} |
| 1630 |
|
| 1631 |
label, err := c.CreateLabel(context.Background(), req) |
| 1632 |
require.NoError(t, err) |
| 1633 |
require.Equal(t, req.Name, label.Name) |
| 1634 |
|
| 1635 |
_, err = c.CreateLabel(context.Background(), req) |
| 1636 |
require.Error(t, err) |
| 1637 |
}) |
| 1638 |
}) |
| 1639 |
} |
| 1640 |
|
| 1641 |
func TestServer_Labels_Duplicates_Update(t *testing.T) { |
| 1642 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1643 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1644 |
label1, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1645 |
Name: uuid.NewString(), |
| 1646 |
Color: "#f66", |
| 1647 |
Type: proton.LabelTypeLabel, |
| 1648 |
}) |
| 1649 |
require.NoError(t, err) |
| 1650 |
|
| 1651 |
label2, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1652 |
Name: uuid.NewString(), |
| 1653 |
Color: "#f66", |
| 1654 |
Type: proton.LabelTypeLabel, |
| 1655 |
}) |
| 1656 |
require.NoError(t, err) |
| 1657 |
|
| 1658 |
// Updating label1 with label2's name should fail. |
| 1659 |
_, err = c.UpdateLabel(context.Background(), label1.ID, proton.UpdateLabelReq{ |
| 1660 |
Name: label2.Name, |
| 1661 |
Color: label1.Color, |
| 1662 |
}) |
| 1663 |
require.Error(t, err) |
| 1664 |
|
| 1665 |
// Updating label1's color while preserving its name should succeed. |
| 1666 |
_, err = c.UpdateLabel(context.Background(), label1.ID, proton.UpdateLabelReq{ |
| 1667 |
Name: label1.Name, |
| 1668 |
Color: "#f00", |
| 1669 |
}) |
| 1670 |
require.NoError(t, err) |
| 1671 |
|
| 1672 |
// Updating label1 with a new name should succeed. |
| 1673 |
_, err = c.UpdateLabel(context.Background(), label1.ID, proton.UpdateLabelReq{ |
| 1674 |
Name: uuid.NewString(), |
| 1675 |
Color: label1.Color, |
| 1676 |
}) |
| 1677 |
require.NoError(t, err) |
| 1678 |
}) |
| 1679 |
}) |
| 1680 |
} |
| 1681 |
|
| 1682 |
func TestServer_Labels_Subfolders(t *testing.T) { |
| 1683 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1684 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1685 |
parent, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1686 |
Name: uuid.NewString(), |
| 1687 |
Color: "#f66", |
| 1688 |
Type: proton.LabelTypeFolder, |
| 1689 |
}) |
| 1690 |
require.NoError(t, err) |
| 1691 |
|
| 1692 |
child, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1693 |
Name: uuid.NewString(), |
| 1694 |
ParentID: parent.ID, |
| 1695 |
Color: "#f66", |
| 1696 |
Type: proton.LabelTypeFolder, |
| 1697 |
}) |
| 1698 |
require.NoError(t, err) |
| 1699 |
require.Equal(t, []string{parent.Name, child.Name}, child.Path) |
| 1700 |
|
| 1701 |
child2, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1702 |
Name: uuid.NewString(), |
| 1703 |
ParentID: child.ID, |
| 1704 |
Color: "#f66", |
| 1705 |
Type: proton.LabelTypeFolder, |
| 1706 |
}) |
| 1707 |
require.NoError(t, err) |
| 1708 |
require.Equal(t, []string{parent.Name, child.Name, child2.Name}, child2.Path) |
| 1709 |
}) |
| 1710 |
}) |
| 1711 |
} |
| 1712 |
|
| 1713 |
func TestServer_Labels_Subfolders_Reassign(t *testing.T) { |
| 1714 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1715 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1716 |
parent1, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1717 |
Name: uuid.NewString(), |
| 1718 |
Color: "#f66", |
| 1719 |
Type: proton.LabelTypeFolder, |
| 1720 |
}) |
| 1721 |
require.NoError(t, err) |
| 1722 |
|
| 1723 |
parent2, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1724 |
Name: uuid.NewString(), |
| 1725 |
Color: "#f66", |
| 1726 |
Type: proton.LabelTypeFolder, |
| 1727 |
}) |
| 1728 |
require.NoError(t, err) |
| 1729 |
|
| 1730 |
// Create a child initially under parent1. |
| 1731 |
child, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1732 |
Name: uuid.NewString(), |
| 1733 |
ParentID: parent1.ID, |
| 1734 |
Color: "#f66", |
| 1735 |
Type: proton.LabelTypeFolder, |
| 1736 |
}) |
| 1737 |
require.NoError(t, err) |
| 1738 |
require.Equal(t, []string{parent1.Name, child.Name}, child.Path) |
| 1739 |
|
| 1740 |
// Reassign the child to parent2. |
| 1741 |
child2, err := c.UpdateLabel(context.Background(), child.ID, proton.UpdateLabelReq{ |
| 1742 |
Name: child.Name, |
| 1743 |
Color: child.Color, |
| 1744 |
ParentID: parent2.ID, |
| 1745 |
}) |
| 1746 |
require.NoError(t, err) |
| 1747 |
require.Equal(t, []string{parent2.Name, child.Name}, child2.Path) |
| 1748 |
|
| 1749 |
// Reassign the child to no parent. |
| 1750 |
child3, err := c.UpdateLabel(context.Background(), child.ID, proton.UpdateLabelReq{ |
| 1751 |
Name: child2.Name, |
| 1752 |
Color: child2.Color, |
| 1753 |
ParentID: "", |
| 1754 |
}) |
| 1755 |
require.NoError(t, err) |
| 1756 |
require.Equal(t, []string{child3.Name}, child3.Path) |
| 1757 |
}) |
| 1758 |
}) |
| 1759 |
} |
| 1760 |
|
| 1761 |
func TestServer_Labels_Subfolders_DeleteParentWithChildren(t *testing.T) { |
| 1762 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1763 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1764 |
parent, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1765 |
Name: uuid.NewString(), |
| 1766 |
Color: "#f66", |
| 1767 |
Type: proton.LabelTypeFolder, |
| 1768 |
}) |
| 1769 |
require.NoError(t, err) |
| 1770 |
|
| 1771 |
child, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1772 |
Name: uuid.NewString(), |
| 1773 |
ParentID: parent.ID, |
| 1774 |
Color: "#f66", |
| 1775 |
Type: proton.LabelTypeFolder, |
| 1776 |
}) |
| 1777 |
require.NoError(t, err) |
| 1778 |
require.Equal(t, []string{parent.Name, child.Name}, child.Path) |
| 1779 |
|
| 1780 |
other, err := c.CreateLabel(context.Background(), proton.CreateLabelReq{ |
| 1781 |
Name: uuid.NewString(), |
| 1782 |
Color: "#f66", |
| 1783 |
Type: proton.LabelTypeFolder, |
| 1784 |
}) |
| 1785 |
require.NoError(t, err) |
| 1786 |
|
| 1787 |
// Get labels before. |
| 1788 |
before, err := c.GetLabels(context.Background(), proton.LabelTypeFolder) |
| 1789 |
require.NoError(t, err) |
| 1790 |
|
| 1791 |
// Delete the parent. |
| 1792 |
require.NoError(t, c.DeleteLabel(context.Background(), parent.ID)) |
| 1793 |
|
| 1794 |
// Get labels after. |
| 1795 |
after, err := c.GetLabels(context.Background(), proton.LabelTypeFolder) |
| 1796 |
require.NoError(t, err) |
| 1797 |
|
| 1798 |
// Both parent and child are deleted. |
| 1799 |
require.Equal(t, len(before)-2, len(after)) |
| 1800 |
|
| 1801 |
// The only label left is the other one. |
| 1802 |
require.Equal(t, other.ID, after[0].ID) |
| 1803 |
}) |
| 1804 |
}) |
| 1805 |
} |
| 1806 |
|
| 1807 |
func TestServer_AddressCreateDelete(t *testing.T) { |
| 1808 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1809 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1810 |
user, err := c.GetUser(context.Background()) |
| 1811 |
require.NoError(t, err) |
| 1812 |
|
| 1813 |
// Create an address. |
| 1814 |
alias, err := s.CreateAddress(user.ID, "[email protected]", []byte("pass")) |
| 1815 |
require.NoError(t, err) |
| 1816 |
|
| 1817 |
// The user should have two addresses, both enabled. |
| 1818 |
{ |
| 1819 |
addr, err := c.GetAddresses(context.Background()) |
| 1820 |
require.NoError(t, err) |
| 1821 |
require.Len(t, addr, 2) |
| 1822 |
require.Equal(t, addr[0].Status, proton.AddressStatusEnabled) |
| 1823 |
require.Equal(t, addr[1].Status, proton.AddressStatusEnabled) |
| 1824 |
} |
| 1825 |
|
| 1826 |
// Disable the alias. |
| 1827 |
require.NoError(t, c.DisableAddress(context.Background(), alias)) |
| 1828 |
|
| 1829 |
// The user should have two addresses, the primary enabled and the alias disabled. |
| 1830 |
{ |
| 1831 |
addr, err := c.GetAddresses(context.Background()) |
| 1832 |
require.NoError(t, err) |
| 1833 |
require.Len(t, addr, 2) |
| 1834 |
require.Equal(t, addr[0].Status, proton.AddressStatusEnabled) |
| 1835 |
require.Equal(t, addr[1].Status, proton.AddressStatusDisabled) |
| 1836 |
} |
| 1837 |
|
| 1838 |
// Delete the alias. |
| 1839 |
require.NoError(t, c.DeleteAddress(context.Background(), alias)) |
| 1840 |
|
| 1841 |
// The user should have one address, the primary enabled. |
| 1842 |
{ |
| 1843 |
addr, err := c.GetAddresses(context.Background()) |
| 1844 |
require.NoError(t, err) |
| 1845 |
require.Len(t, addr, 1) |
| 1846 |
require.Equal(t, addr[0].Status, proton.AddressStatusEnabled) |
| 1847 |
} |
| 1848 |
}) |
| 1849 |
}) |
| 1850 |
} |
| 1851 |
|
| 1852 |
func TestServer_AddressOrder(t *testing.T) { |
| 1853 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1854 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1855 |
user, err := c.GetUser(context.Background()) |
| 1856 |
require.NoError(t, err) |
| 1857 |
|
| 1858 |
primary, err := c.GetAddresses(context.Background()) |
| 1859 |
require.NoError(t, err) |
| 1860 |
|
| 1861 |
// Create 3 additional addresses. |
| 1862 |
addr1, err := s.CreateAddress(user.ID, "[email protected]", []byte("pass")) |
| 1863 |
require.NoError(t, err) |
| 1864 |
|
| 1865 |
addr2, err := s.CreateAddress(user.ID, "[email protected]", []byte("pass")) |
| 1866 |
require.NoError(t, err) |
| 1867 |
|
| 1868 |
addr3, err := s.CreateAddress(user.ID, "[email protected]", []byte("pass")) |
| 1869 |
require.NoError(t, err) |
| 1870 |
|
| 1871 |
addresses, err := c.GetAddresses(context.Background()) |
| 1872 |
require.NoError(t, err) |
| 1873 |
|
| 1874 |
// Check the order. |
| 1875 |
require.Equal(t, primary[0].ID, addresses[0].ID) |
| 1876 |
require.Equal(t, addr1, addresses[1].ID) |
| 1877 |
require.Equal(t, addr2, addresses[2].ID) |
| 1878 |
require.Equal(t, addr3, addresses[3].ID) |
| 1879 |
|
| 1880 |
// Update the order. |
| 1881 |
require.NoError(t, c.OrderAddresses(ctx, proton.OrderAddressesReq{ |
| 1882 |
AddressIDs: []string{addr3, addr2, addr1, primary[0].ID}, |
| 1883 |
})) |
| 1884 |
|
| 1885 |
// Check the order. |
| 1886 |
addresses, err = c.GetAddresses(context.Background()) |
| 1887 |
require.NoError(t, err) |
| 1888 |
|
| 1889 |
require.Equal(t, addr3, addresses[0].ID) |
| 1890 |
require.Equal(t, addr2, addresses[1].ID) |
| 1891 |
require.Equal(t, addr1, addresses[2].ID) |
| 1892 |
require.Equal(t, primary[0].ID, addresses[3].ID) |
| 1893 |
}) |
| 1894 |
}) |
| 1895 |
} |
| 1896 |
|
| 1897 |
func TestServer_MailSettings(t *testing.T) { |
| 1898 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1899 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1900 |
settings, err := c.GetMailSettings(context.Background()) |
| 1901 |
require.NoError(t, err) |
| 1902 |
require.Equal(t, proton.Bool(false), settings.AttachPublicKey) |
| 1903 |
|
| 1904 |
updated, err := c.SetAttachPublicKey(context.Background(), proton.SetAttachPublicKeyReq{AttachPublicKey: true}) |
| 1905 |
require.NoError(t, err) |
| 1906 |
require.Equal(t, proton.Bool(true), updated.AttachPublicKey) |
| 1907 |
}) |
| 1908 |
}) |
| 1909 |
} |
| 1910 |
|
| 1911 |
func TestServer_UserSettings(t *testing.T) { |
| 1912 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1913 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1914 |
settings, err := c.GetUserSettings(context.Background()) |
| 1915 |
require.NoError(t, err) |
| 1916 |
require.Equal(t, proton.SettingEnabled, settings.Telemetry) |
| 1917 |
require.Equal(t, proton.SettingEnabled, settings.CrashReports) |
| 1918 |
|
| 1919 |
settings, err = c.SetUserSettingsTelemetry(context.Background(), proton.SetTelemetryReq{Telemetry: proton.SettingDisabled}) |
| 1920 |
require.NoError(t, err) |
| 1921 |
require.Equal(t, proton.SettingDisabled, settings.Telemetry) |
| 1922 |
require.Equal(t, proton.SettingEnabled, settings.CrashReports) |
| 1923 |
|
| 1924 |
settings, err = c.SetUserSettingsCrashReports(context.Background(), proton.SetCrashReportReq{CrashReports: proton.SettingDisabled}) |
| 1925 |
require.NoError(t, err) |
| 1926 |
require.Equal(t, proton.SettingDisabled, settings.Telemetry) |
| 1927 |
require.Equal(t, proton.SettingDisabled, settings.CrashReports) |
| 1928 |
|
| 1929 |
settings, err = c.SetUserSettingsTelemetry(context.Background(), proton.SetTelemetryReq{Telemetry: 2}) |
| 1930 |
require.Error(t, err) |
| 1931 |
|
| 1932 |
settings, err = c.SetUserSettingsCrashReports(context.Background(), proton.SetCrashReportReq{CrashReports: 2}) |
| 1933 |
require.Error(t, err) |
| 1934 |
|
| 1935 |
settings, err = c.GetUserSettings(context.Background()) |
| 1936 |
require.NoError(t, err) |
| 1937 |
require.Equal(t, proton.SettingDisabled, settings.Telemetry) |
| 1938 |
require.Equal(t, proton.SettingDisabled, settings.CrashReports) |
| 1939 |
|
| 1940 |
}) |
| 1941 |
}) |
| 1942 |
} |
| 1943 |
|
| 1944 |
func TestServer_Domains(t *testing.T) { |
| 1945 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1946 |
domains, err := m.GetDomains(ctx) |
| 1947 |
require.NoError(t, err) |
| 1948 |
require.Equal(t, []string{s.GetDomain()}, domains) |
| 1949 |
}) |
| 1950 |
} |
| 1951 |
|
| 1952 |
func TestServer_StatusHooks(t *testing.T) { |
| 1953 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1954 |
s.AddStatusHook(func(req *http.Request) (int, bool) { |
| 1955 |
if req.URL.Path == "/core/v4/addresses" { |
| 1956 |
return http.StatusBadRequest, true |
| 1957 |
} |
| 1958 |
|
| 1959 |
return 0, false |
| 1960 |
}) |
| 1961 |
|
| 1962 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1963 |
addr, err := c.GetAddresses(context.Background()) |
| 1964 |
require.Error(t, err) |
| 1965 |
require.Nil(t, addr) |
| 1966 |
|
| 1967 |
if apiErr := new(proton.APIError); errors.As(err, &apiErr) { |
| 1968 |
require.Equal(t, http.StatusBadRequest, apiErr.Status) |
| 1969 |
} else { |
| 1970 |
require.Fail(t, "expected APIError") |
| 1971 |
} |
| 1972 |
}) |
| 1973 |
}) |
| 1974 |
} |
| 1975 |
|
| 1976 |
func TestServer_SendDataEvent(t *testing.T) { |
| 1977 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 1978 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 1979 |
// Send data event minimal |
| 1980 |
err := c.SendDataEvent(context.Background(), proton.SendStatsReq{MeasurementGroup: "proton.any.test"}) |
| 1981 |
require.NoError(t, err) |
| 1982 |
|
| 1983 |
// Send data event Full. |
| 1984 |
var req proton.SendStatsReq |
| 1985 |
req.MeasurementGroup = "proton.any.test" |
| 1986 |
req.Event = "test" |
| 1987 |
req.Values = map[string]any{"string": "string", "integer": 42} |
| 1988 |
req.Dimensions = map[string]any{"string": "string", "integer": 42} |
| 1989 |
err = c.SendDataEvent(context.Background(), req) |
| 1990 |
require.NoError(t, err) |
| 1991 |
|
| 1992 |
// Send bad data event. |
| 1993 |
err = c.SendDataEvent(context.Background(), proton.SendStatsReq{}) |
| 1994 |
require.Error(t, err) |
| 1995 |
}) |
| 1996 |
}) |
| 1997 |
} |
| 1998 |
|
| 1999 |
func TestServer_SendDataEventMultiple(t *testing.T) { |
| 2000 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 2001 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 2002 |
// Send multiple minimal data event. |
| 2003 |
var req proton.SendStatsMultiReq |
| 2004 |
req.EventInfo = append(req.EventInfo, proton.SendStatsReq{MeasurementGroup: "proton.any.test"}) |
| 2005 |
req.EventInfo = append(req.EventInfo, proton.SendStatsReq{MeasurementGroup: "proton.any.test2"}) |
| 2006 |
err := c.SendDataEventMultiple(context.Background(), req) |
| 2007 |
require.NoError(t, err) |
| 2008 |
|
| 2009 |
// send empty multiple data event. |
| 2010 |
err = c.SendDataEventMultiple(context.Background(), proton.SendStatsMultiReq{}) |
| 2011 |
require.NoError(t, err) |
| 2012 |
|
| 2013 |
// Send bad multiple data event. |
| 2014 |
var badReq proton.SendStatsMultiReq |
| 2015 |
badReq.EventInfo = append(badReq.EventInfo, proton.SendStatsReq{}) |
| 2016 |
err = c.SendDataEventMultiple(context.Background(), badReq) |
| 2017 |
require.Error(t, err) |
| 2018 |
}) |
| 2019 |
}) |
| 2020 |
} |
| 2021 |
|
| 2022 |
func TestServer_GetMessageGroupCount(t *testing.T) { |
| 2023 |
withServer(t, func(ctx context.Context, s *Server, m *proton.Manager) { |
| 2024 |
withUser(ctx, t, s, m, "user", "pass", func(c *proton.Client) { |
| 2025 |
ctx, cancel := context.WithCancel(ctx) |
| 2026 |
defer cancel() |
| 2027 |
|
| 2028 |
user, err := c.GetUser(ctx) |
| 2029 |
require.NoError(t, err) |
| 2030 |
|
| 2031 |
addr, err := c.GetAddresses(ctx) |
| 2032 |
require.NoError(t, err) |
| 2033 |
|
| 2034 |
salt, err := c.GetSalts(ctx) |
| 2035 |
require.NoError(t, err) |
| 2036 |
|
| 2037 |
pass, err := salt.SaltForKey([]byte("pass"), user.Keys.Primary().ID) |
| 2038 |
require.NoError(t, err) |
| 2039 |
|
| 2040 |
_, addrKRs, err := proton.Unlock(user, addr, pass, async.NoopPanicHandler{}) |
| 2041 |
require.NoError(t, err) |
| 2042 |
|
| 2043 |
expected := []proton.MessageGroupCount{ |
| 2044 |
{ |
| 2045 |
LabelID: proton.InboxLabel, |
| 2046 |
Total: 10, |
| 2047 |
Unread: 4, |
| 2048 |
}, |
| 2049 |
{ |
| 2050 |
LabelID: proton.SentLabel, |
| 2051 |
Total: 4, |
| 2052 |
Unread: 0, |
| 2053 |
}, |
| 2054 |
{ |
| 2055 |
LabelID: proton.ArchiveLabel, |
| 2056 |
Total: 3, |
| 2057 |
Unread: 0, |
| 2058 |
}, |
| 2059 |
{ |
| 2060 |
LabelID: proton.TrashLabel, |
| 2061 |
Total: 6, |
| 2062 |
Unread: 0, |
| 2063 |
}, |
| 2064 |
{ |
| 2065 |
LabelID: proton.AllMailLabel, |
| 2066 |
Total: 23, |
| 2067 |
Unread: 4, |
| 2068 |
}, |
| 2069 |
} |
| 2070 |
|
| 2071 |
for _, st := range expected { |
| 2072 |
if st.LabelID == proton.AllMailLabel { |
| 2073 |
continue |
| 2074 |
} |
| 2075 |
|
| 2076 |
var flags proton.MessageFlag |
| 2077 |
if st.LabelID == proton.InboxLabel { |
| 2078 |
flags = proton.MessageFlagReceived |
| 2079 |
} else if st.LabelID == proton.SentLabel { |
| 2080 |
flags = proton.MessageFlagSent |
| 2081 |
} |
| 2082 |
|
| 2083 |
res := importMessages(ctx, t, c, addr[0].ID, addrKRs[addr[0].ID], []string{}, flags, st.Total) |
| 2084 |
msgIDs := xslices.Map(res, func(r proton.ImportRes) string { |
| 2085 |
return r.MessageID |
| 2086 |
}) |
| 2087 |
require.NoError(t, c.LabelMessages(ctx, msgIDs, st.LabelID)) |
| 2088 |
if st.Unread == 0 { |
| 2089 |
require.NoError(t, c.MarkMessagesRead(ctx, msgIDs...)) |
| 2090 |
} else { |
| 2091 |
require.NoError(t, c.MarkMessagesRead(ctx, msgIDs[st.Unread:]...)) |
| 2092 |
} |
| 2093 |
} |
| 2094 |
|
| 2095 |
counts, err := c.GetGroupedMessageCount(ctx) |
| 2096 |
require.NoError(t, err) |
| 2097 |
|
| 2098 |
counts = xslices.Filter(counts, func(t proton.MessageGroupCount) bool { |
| 2099 |
switch t.LabelID { |
| 2100 |
case proton.InboxLabel, proton.TrashLabel, proton.ArchiveLabel, proton.AllMailLabel, proton.SentLabel: |
| 2101 |
return true |
| 2102 |
default: |
| 2103 |
return false |
| 2104 |
} |
| 2105 |
}) |
| 2106 |
require.NotEmpty(t, counts) |
| 2107 |
require.ElementsMatch(t, expected, counts) |
| 2108 |
|
| 2109 |
}) |
| 2110 |
}) |
| 2111 |
} |
| 2112 |
|
| 2113 |
func withServer(t *testing.T, fn func(ctx context.Context, s *Server, m *proton.Manager), opts ...Option) { |
| 2114 |
ctx, cancel := context.WithCancel(context.Background()) |
| 2115 |
defer cancel() |
| 2116 |
|
| 2117 |
s := New(opts...) |
| 2118 |
defer s.Close() |
| 2119 |
|
| 2120 |
m := proton.New( |
| 2121 |
proton.WithHostURL(s.GetHostURL()), |
| 2122 |
proton.WithCookieJar(newTestCookieJar()), |
| 2123 |
proton.WithTransport(proton.InsecureTransport()), |
| 2124 |
) |
| 2125 |
defer m.Close() |
| 2126 |
|
| 2127 |
fn(ctx, s, m) |
| 2128 |
} |
| 2129 |
|
| 2130 |
func withUser(ctx context.Context, t *testing.T, s *Server, m *proton.Manager, user, pass string, fn func(c *proton.Client)) { |
| 2131 |
_, _, err := s.CreateUser(user, []byte(pass)) |
| 2132 |
require.NoError(t, err) |
| 2133 |
|
| 2134 |
c, _, err := m.NewClientWithLogin(ctx, user, []byte(pass)) |
| 2135 |
require.NoError(t, err) |
| 2136 |
defer c.Close() |
| 2137 |
|
| 2138 |
fn(c) |
| 2139 |
} |
| 2140 |
|
| 2141 |
func withMessages(ctx context.Context, t *testing.T, c *proton.Client, pass string, count int, fn func([]string)) { |
| 2142 |
user, err := c.GetUser(ctx) |
| 2143 |
require.NoError(t, err) |
| 2144 |
|
| 2145 |
addr, err := c.GetAddresses(ctx) |
| 2146 |
require.NoError(t, err) |
| 2147 |
|
| 2148 |
salt, err := c.GetSalts(ctx) |
| 2149 |
require.NoError(t, err) |
| 2150 |
|
| 2151 |
keyPass, err := salt.SaltForKey([]byte(pass), user.Keys.Primary().ID) |
| 2152 |
require.NoError(t, err) |
| 2153 |
|
| 2154 |
_, addrKRs, err := proton.Unlock(user, addr, keyPass, async.NoopPanicHandler{}) |
| 2155 |
require.NoError(t, err) |
| 2156 |
|
| 2157 |
fn(xslices.Map(importMessages(ctx, t, c, addr[0].ID, addrKRs[addr[0].ID], []string{}, proton.MessageFlagReceived, count), func(res proton.ImportRes) string { |
| 2158 |
return res.MessageID |
| 2159 |
})) |
| 2160 |
} |
| 2161 |
|
| 2162 |
func importMessagesWithSubjectGenerator( |
| 2163 |
ctx context.Context, |
| 2164 |
t *testing.T, |
| 2165 |
c *proton.Client, |
| 2166 |
addrID string, |
| 2167 |
addrKR *crypto.KeyRing, |
| 2168 |
labelIDs []string, |
| 2169 |
flags proton.MessageFlag, |
| 2170 |
count int, |
| 2171 |
subjectGenerator func() string, |
| 2172 |
) []proton.ImportRes { |
| 2173 |
req := iterator.Collect(iterator.Map(iterator.Counter(count), func(int) proton.ImportReq { |
| 2174 |
return proton.ImportReq{ |
| 2175 |
Metadata: proton.ImportMetadata{ |
| 2176 |
AddressID: addrID, |
| 2177 |
LabelIDs: labelIDs, |
| 2178 |
Flags: flags, |
| 2179 |
Unread: true, |
| 2180 |
}, |
| 2181 |
Message: newMessageLiteralWithSubject("[email protected]", "[email protected]", subjectGenerator()), |
| 2182 |
} |
| 2183 |
})) |
| 2184 |
|
| 2185 |
str, err := c.ImportMessages(ctx, addrKR, runtime.NumCPU(), runtime.NumCPU(), req...) |
| 2186 |
require.NoError(t, err) |
| 2187 |
|
| 2188 |
res, err := stream.Collect(ctx, str) |
| 2189 |
require.NoError(t, err) |
| 2190 |
|
| 2191 |
return res |
| 2192 |
} |
| 2193 |
|
| 2194 |
func importMessages( |
| 2195 |
ctx context.Context, |
| 2196 |
t *testing.T, |
| 2197 |
c *proton.Client, |
| 2198 |
addrID string, |
| 2199 |
addrKR *crypto.KeyRing, |
| 2200 |
labelIDs []string, |
| 2201 |
flags proton.MessageFlag, |
| 2202 |
count int, |
| 2203 |
) []proton.ImportRes { |
| 2204 |
return importMessagesWithSubjectGenerator(ctx, t, c, addrID, addrKR, labelIDs, flags, count, func() string { |
| 2205 |
return uuid.NewString() |
| 2206 |
}) |
| 2207 |
} |
| 2208 |
|
| 2209 |
func countBytesRead(ctl *proton.NetCtl, fn func()) uint64 { |
| 2210 |
var read uint64 |
| 2211 |
|
| 2212 |
ctl.OnRead(func(b []byte) { |
| 2213 |
atomic.AddUint64(&read, uint64(len(b))) |
| 2214 |
}) |
| 2215 |
|
| 2216 |
fn() |
| 2217 |
|
| 2218 |
return read |
| 2219 |
} |
| 2220 |
|
| 2221 |
type testCookieJar struct { |
| 2222 |
cookies map[string][]*http.Cookie |
| 2223 |
lock sync.RWMutex |
| 2224 |
} |
| 2225 |
|
| 2226 |
func newTestCookieJar() *testCookieJar { |
| 2227 |
return &testCookieJar{ |
| 2228 |
cookies: make(map[string][]*http.Cookie), |
| 2229 |
} |
| 2230 |
} |
| 2231 |
|
| 2232 |
func (j *testCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) { |
| 2233 |
j.lock.Lock() |
| 2234 |
defer j.lock.Unlock() |
| 2235 |
|
| 2236 |
j.cookies[u.Host] = cookies |
| 2237 |
} |
| 2238 |
|
| 2239 |
func (j *testCookieJar) Cookies(u *url.URL) []*http.Cookie { |
| 2240 |
j.lock.RLock() |
| 2241 |
defer j.lock.RUnlock() |
| 2242 |
|
| 2243 |
return j.cookies[u.Host] |
| 2244 |
} |
| 2245 |
|
| 2246 |
func must[T any](t T, err error) T { |
| 2247 |
if err != nil { |
| 2248 |
panic(err) |
| 2249 |
} |
| 2250 |
|
| 2251 |
return t |
| 2252 |
} |
| 2253 |
|
| 2254 |
func elementsMatch[T comparable](want, got []T) bool { |
| 2255 |
if len(want) != len(got) { |
| 2256 |
return false |
| 2257 |
} |
| 2258 |
|
| 2259 |
for _, w := range want { |
| 2260 |
if !slices.Contains(got, w) { |
| 2261 |
return false |
| 2262 |
} |
| 2263 |
} |
| 2264 |
|
| 2265 |
return true |
| 2266 |
} |
| 2267 |
|
| 2268 |
func getFullMessages(ctx context.Context, |
| 2269 |
c *proton.Client, |
| 2270 |
workers, buffer int, |
| 2271 |
messageIDs ...string) stream.Stream[proton.FullMessage] { |
| 2272 |
scheduler := proton.NewSequentialScheduler() |
| 2273 |
attachmentStorageProvider := proton.NewDefaultAttachmentAllocator() |
| 2274 |
return parallel.MapStream( |
| 2275 |
ctx, |
| 2276 |
stream.FromIterator(iterator.Slice(messageIDs)), |
| 2277 |
workers, |
| 2278 |
buffer, |
| 2279 |
func(ctx context.Context, messageID string) (proton.FullMessage, error) { |
| 2280 |
return c.GetFullMessage(ctx, messageID, scheduler, attachmentStorageProvider) |
| 2281 |
}, |
| 2282 |
) |
| 2283 |
} |