package msgraph import ( "context" "errors" "fmt" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/warmbly/warmbly/internal/errx" "golang.org/x/oauth2" ) func TestResolveMessageIDRequiresCompleteIdentityLookup(t *testing.T) { const more = `"@odata.nextLink":"https://graph.microsoft.com/v1.0/me/messages?$skiptoken=more"` for _, tc := range []struct { name string pages []string want string uncertain bool }{ {"terminal absence", []string{`{"value":[]}`}, "", false}, {"terminal absence with whitespace", []string{"{\"value\":[]} \n"}, "", false}, {"terminal identity", []string{`{"value":[{"id":"live-id"}]}`}, "live-id", false}, {"identity page continues to terminal page", []string{`{"value":[{"id":"live-id"}],` + more + `}`, `{"value":[]}`}, "live-id", false}, {"empty page continues to identity", []string{`{"value":[],` + more + `}`, `{"value":[{"id":"live-id"}]}`}, "live-id", false}, {"same identity on two pages", []string{`{"value":[{"id":"live-id"}],` + more + `}`, `{"value":[{"id":"live-id"}]}`}, "live-id", false}, {"identity on a later page conflicts", []string{`{"value":[{"id":"one"}],` + more + `}`, `{"value":[{"id":"two"}]}`}, "", true}, {"continuation never ends", []string{`{"value":[],` + more + `}`}, "", true}, {"terminal absence followed by identity", []string{`{"value":[]}{"value":[{"id":"live-id"}]}`}, "", true}, {"identity followed by another page", []string{`{"value":[{"id":"live-id"}]}{"value":[]}`}, "", true}, {"terminal absence followed by junk", []string{`{"value":[]}private-marker`}, "", true}, {"missing collection", []string{`{}`}, "", true}, {"null collection", []string{`{"value":null}`}, "", true}, {"null response", []string{`null`}, "", true}, {"missing identity", []string{`{"value":[{}]}`}, "", true}, {"null identity", []string{`{"value":[{"id":null}]}`}, "", true}, {"empty identity", []string{`{"value":[{"id":""}]}`}, "", true}, {"blank identity", []string{`{"value":[{"id":" \t\n "}]}`}, "", true}, {"padded identity", []string{`{"value":[{"id":" live-id "}]}`}, "", true}, {"ambiguous identities", []string{`{"value":[{"id":"one"},{"id":"two"}]}`}, "", true}, {"foreign continuation", []string{`{"value":[],"@odata.nextLink":"https://other.example.test/collect?token=private-marker"}`}, "", true}, {"relative continuation", []string{`{"value":[],"@odata.nextLink":"/v1.0/me/messages?$skiptoken=more"}`}, "", true}, {"http continuation", []string{`{"value":[],"@odata.nextLink":"http://graph.microsoft.com/v1.0/me/messages?$skiptoken=more"}`}, "", true}, {"other version continuation", []string{`{"value":[],"@odata.nextLink":"https://graph.microsoft.com/beta/me/messages?$skiptoken=more"}`}, "", true}, {"empty continuation", []string{`{"value":[],"@odata.nextLink":""}`}, "", true}, {"blank continuation", []string{`{"value":[],"@odata.nextLink":" "}`}, "", true}, {"null continuation", []string{`{"value":[],"@odata.nextLink":null}`}, "", true}, {"wrong continuation type", []string{`{"value":[],"@odata.nextLink":{"token":"private-marker"}}`}, "", true}, } { t.Run(tc.name, func(t *testing.T) { requests := 0 c := deltaTestClient(t, func(w http.ResponseWriter, r *http.Request) { requests++ if r.Method != http.MethodGet || r.URL.Path != "/v1.0/me/messages" { t.Errorf("lookup left the message collection: %s %s", r.Method, r.URL.Path) } if requests == 1 && (r.URL.Query().Get("$top") != "10" || r.URL.Query().Get("$select") != "id" || r.URL.Query().Get("$filter") != "internetMessageId eq ''") { t.Errorf("lookup changed scope: %s", r.URL.RawQuery) } if requests > 1 && r.URL.Query().Get("$skiptoken") != "more" { t.Errorf("continuation not followed as given: %s", r.URL.RawQuery) } fmt.Fprint(w, tc.pages[min(requests, len(tc.pages))-1]) }, nil) id, err := c.ResolveMessageID(t.Context(), "") if id != tc.want || (err != nil) != tc.uncertain || requests > messageLookupMaxPages { t.Fatalf("lookup: id=%q err=%v requests=%d", id, err, requests) } if tc.uncertain { merr := mailErrorOf(t, err) if !errors.Is(err, errMessageLookupIncomplete) || merr.Type != errx.MailErrorWarning || merr.Code != errx.MailErrorCodeServerUnreachable || merr.ResolveMethod != errx.MailErrorResolveMethodRetry { t.Fatalf("incomplete lookup must be temporary: %v", err) } } if err != nil && strings.Contains(err.Error(), "private-marker") { t.Fatal("lookup error exposed continuation payload") } }) } } func TestResolveMessageIDRejectsBlankIdentityBeforeRequest(t *testing.T) { requests := 0 c := deltaTestClient(t, func(w http.ResponseWriter, _ *http.Request) { requests++ fmt.Fprint(w, `{"value":[]}`) }, nil) for _, id := range []string{"", " \t\n "} { if _, err := c.ResolveMessageID(t.Context(), id); !errors.Is(err, errMessageLookupIncomplete) { t.Fatalf("blank identity must fail closed: %v", err) } } if requests != 0 { t.Fatal("blank identity queried provider") } } func TestResolveMessageIDMalformedResponseDoesNotEstablishAbsence(t *testing.T) { for _, body := range []string{"{", "", `{"value":{}}`, `{"value":"private-marker"}`, `{"value":[null]}`, `{"value":[{"id":42}]}`} { c := graphStatus(t, http.StatusOK, body) if id, err := c.ResolveMessageID(t.Context(), ""); err == nil || id != "" || strings.Contains(err.Error(), "private-marker") { t.Fatalf("malformed response resolved or exposed payload: id=%q err=%v", id, err) } } } func TestResolveMessageIDPreservesProviderRetryEvidence(t *testing.T) { requests := 0 c := deltaTestClient(t, func(w http.ResponseWriter, _ *http.Request) { requests++ w.Header().Set("Retry-After", "90") w.WriteHeader(http.StatusServiceUnavailable) fmt.Fprint(w, `{"error":{"code":"ServiceUnavailable"}}`) }, nil) if id, err := c.ResolveMessageID(t.Context(), ""); id != "" || err == nil { t.Fatalf("unavailable provider resolved: id=%q err=%v", id, err) } else if merr := mailErrorOf(t, err); merr.Code != errx.MailErrorCodeServerUnreachable || merr.RetryAfter != 90*time.Second || requests != 1 { t.Fatalf("lost provider retry evidence: code=%s retry=%v requests=%d", merr.Code, merr.RetryAfter, requests) } } func TestWarmupFilingDoesNotMoveAlreadyFiledGraphMessage(t *testing.T) { for _, archive := range []bool{false, true} { t.Run(map[bool]string{false: "folder", true: "archive"}[archive], func(t *testing.T) { moves := 0 parent := "inbox-id" srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") switch { case r.Method == "POST" && strings.HasSuffix(r.URL.Path, "/move"): moves++ parent = "target-id" fmt.Fprint(w, `{"id":"new-id"}`) case strings.HasSuffix(r.URL.Path, "/mailFolders/archive"): fmt.Fprint(w, `{"id":"target-id"}`) case strings.HasSuffix(r.URL.Path, "/mailFolders"): fmt.Fprint(w, `{"value":[{"id":"target-id","displayName":"Warmbly"}]}`) default: fmt.Fprintf(w, `{"parentFolderId":%q}`, parent) } })) defer srv.Close() ctx := context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{Transport: rewriteTo(srv.URL)}) client := &Client{} if err := client.Init(ctx, &oauth2.Token{AccessToken: "test", Expiry: time.Now().Add(time.Hour)}, oauth2.Config{}); err != nil { t.Fatal(err) } move := func(id string) (string, error) { if archive { return client.MoveToArchive(ctx, id) } return client.MoveToFolder(ctx, id, "Warmbly") } id, err := move("old-id") if err != nil || id != "new-id" || moves != 1 { t.Fatalf("initial filing: id=%q moves=%d err=%v", id, moves, err) } id, err = move("new-id") if err != nil || id != "new-id" || moves != 1 { t.Fatalf("repeated filing moved message again: id=%q moves=%d err=%v", id, moves, err) } }) } }