Phase F follow-ups (ADR-16): - Calendars: events are now stored in calendar_events by a calendars.sync job (every 15 minutes per account, 90 days back and 400 ahead), so journals open at once and keep showing events while a source is down. Google accounts connect with OAuth (calendar.readonly) through DOCWELL_GOOGLE_CLIENT_ID/SECRET; reconnecting renews access. - OneNote: whole notebooks import through Microsoft Graph (Notes.Read, DOCWELL_MICROSOFT_CLIENT_ID/SECRET/TENANT) in the background, with section groups, sections, subpages, pictures and attached files. The Microsoft sign-in lives in memory for an hour and is never stored. - Readwise: an hourly sync with the person's sealed access token; new books get a page under "Readwise", new highlights are appended once. New readwise_syncs collection. - internal/oauth: signed states and one callback route per provider, routed by purpose. directory gains ImportConverted and CallerOf. - Phone app: "Today's journal" on the home screen and calendar events on journal pages. - Web clipper: `npm run package` builds Chrome/Edge, Firefox and source zips; store/ holds listing texts, permission notes and a privacy policy. - OpenAPI, generated clients, translations, compose and .env examples, and user docs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
189 lines
7.2 KiB
Go
189 lines
7.2 KiB
Go
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package onenote
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"docwell/server/internal/access"
|
|
"docwell/server/internal/convert"
|
|
"docwell/server/internal/directory"
|
|
"docwell/server/internal/oauth"
|
|
)
|
|
|
|
type fakeImporter struct {
|
|
pages []convert.NotionPage
|
|
assets []convert.Asset
|
|
space string
|
|
}
|
|
|
|
func (f *fakeImporter) ImportConverted(_ context.Context, _ access.Caller, space, _ string, pages []convert.NotionPage, assets []convert.Asset) ([]directory.Page, error) {
|
|
f.pages, f.assets, f.space = pages, assets, space
|
|
return []directory.Page{{ID: "p1", Title: pages[0].Title}}, nil
|
|
}
|
|
|
|
func fakeGraph(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
png := "\x89PNG\r\n\x1a\n0000"
|
|
var srv *httptest.Server
|
|
mux := http.NewServeMux()
|
|
write := func(w http.ResponseWriter, v any) { _ = json.NewEncoder(w).Encode(v) }
|
|
auth := func(next http.HandlerFunc) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
if r.Header.Get("Authorization") != "Bearer at" {
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
return
|
|
}
|
|
next(w, r)
|
|
}
|
|
}
|
|
mux.HandleFunc("POST /token", func(w http.ResponseWriter, r *http.Request) {
|
|
_ = r.ParseForm()
|
|
if r.Form.Get("code") != "code-1" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
_, _ = w.Write([]byte(`{"error":"invalid_grant"}`))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(`{"access_token":"at","refresh_token":"rt","expires_in":3600}`))
|
|
})
|
|
mux.HandleFunc("GET /v1.0/me", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, map[string]string{"userPrincipalName": "ann@contoso.com"})
|
|
}))
|
|
throttled := false
|
|
mux.HandleFunc("GET /v1.0/me/onenote/notebooks", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
if !throttled {
|
|
throttled = true
|
|
w.Header().Set("Retry-After", "1")
|
|
w.WriteHeader(http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
write(w, map[string]any{"value": []map[string]string{{"id": "nb1", "displayName": "Work"}}})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/notebooks/nb1", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, map[string]string{"id": "nb1", "displayName": "Work"})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/notebooks/nb1/sectionGroups", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, map[string]any{"value": []map[string]string{{"id": "g1", "displayName": "Archive"}}})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/notebooks/nb1/sections", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, map[string]any{"value": []map[string]string{{"id": "s1", "displayName": "Meetings"}}})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/sectionGroups/g1/sectionGroups", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, map[string]any{"value": []any{}})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/sectionGroups/g1/sections", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, map[string]any{"value": []map[string]string{{"id": "s2", "displayName": "2025"}}})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/sections/s1/pages", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Query().Get("pagelevel") != "true" {
|
|
t.Error("pages asked without pagelevel")
|
|
}
|
|
if r.URL.Query().Get("page") == "" {
|
|
write(w, map[string]any{
|
|
"value": []map[string]any{{"id": "pB", "title": "Notes", "level": 1, "order": 1}, {"id": "pA", "title": "Kickoff", "level": 0, "order": 0}},
|
|
"@odata.nextLink": srv.URL + "/v1.0/me/onenote/sections/s1/pages?pagelevel=true&page=2",
|
|
})
|
|
return
|
|
}
|
|
write(w, map[string]any{"value": []map[string]any{{"id": "pC", "title": "Retro", "level": 0, "order": 2}}})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/sections/s2/pages", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
write(w, map[string]any{"value": []any{}})
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/me/onenote/pages/{id}/content", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
res := srv.URL + "/v1.0/users('ann')/onenote/resources/r1/$value"
|
|
switch r.PathValue("id") {
|
|
case "pA":
|
|
_, _ = w.Write([]byte(`<html><head><title>Kickoff</title></head><body><div><p>Agenda</p><img src="` + res + `" data-src-type="image/png">` +
|
|
`<object data-attachment="plan.pdf" type="application/pdf" data="` + srv.URL + `/v1.0/users('ann')/onenote/resources/r2/$value"></object></div></body></html>`))
|
|
default:
|
|
_, _ = w.Write([]byte(`<html><body><p>Page ` + r.PathValue("id") + `</p><img src="` + res + `"></body></html>`))
|
|
}
|
|
}))
|
|
mux.HandleFunc("GET /v1.0/{who}/onenote/resources/{rest...}", auth(func(w http.ResponseWriter, r *http.Request) {
|
|
if strings.Contains(r.URL.Path, "/resources/r2/") {
|
|
_, _ = w.Write([]byte("%PDF-1.4 plan"))
|
|
return
|
|
}
|
|
_, _ = w.Write([]byte(png))
|
|
}))
|
|
srv = httptest.NewServer(mux)
|
|
return srv
|
|
}
|
|
|
|
func TestImportNotebook(t *testing.T) {
|
|
g := fakeGraph(t)
|
|
defer g.Close()
|
|
imp := &fakeImporter{}
|
|
s := New(imp, Options{
|
|
Provider: oauth.Provider{Name: "microsoft", ClientID: "c", ClientSecret: "s", AuthURL: g.URL + "/auth", TokenURL: g.URL + "/token"},
|
|
States: oauth.NewStates("x"), PublicURL: "https://api.test", WebURL: "https://web.test",
|
|
Graph: g.URL + "/v1.0", Client: g.Client(),
|
|
})
|
|
ann := access.Caller{ID: "ann"}
|
|
start, err := s.ConnectURL(ann, "space1", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
u, _ := url.Parse(start)
|
|
st, err := s.o.States.Verify(u.Query().Get("state"), Purpose)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
back, _ := url.Parse(s.Callback(context.Background(), st, "code-1", ""))
|
|
conn := back.Query().Get("onenote")
|
|
if back.Path != "/s/space1" || conn == "" {
|
|
t.Fatalf("callback went to %s", back)
|
|
}
|
|
if email, _ := s.Account(ann, conn); email != "ann@contoso.com" {
|
|
t.Errorf("account = %q", email)
|
|
}
|
|
if _, err := s.Notebooks(context.Background(), access.Caller{ID: "bob"}, conn); err == nil {
|
|
t.Fatal("someone else used the connection")
|
|
}
|
|
nbs, err := s.Notebooks(context.Background(), ann, conn)
|
|
if err != nil || len(nbs) != 1 || nbs[0].Name != "Work" {
|
|
t.Fatalf("notebooks = %+v, %v", nbs, err)
|
|
}
|
|
job, err := s.Import(context.Background(), ann, conn, "nb1", "space1", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(20 * time.Millisecond) {
|
|
if job, _ = s.JobStatus(ann, job.ID); job.Status != "running" || time.Now().After(deadline) {
|
|
break
|
|
}
|
|
}
|
|
if job.Status != "done" || job.Pages != 3 || len(job.Created) != 1 {
|
|
t.Fatalf("job = %+v", job)
|
|
}
|
|
var titles []string
|
|
parent := map[string]string{}
|
|
for _, p := range imp.pages {
|
|
titles = append(titles, p.Title)
|
|
if p.Parent >= 0 {
|
|
parent[p.Title] = imp.pages[p.Parent].Title
|
|
}
|
|
}
|
|
if strings.Join(titles, ",") != "Work,Meetings,Kickoff,Notes,Retro,Archive,2025" {
|
|
t.Errorf("pages = %v", titles)
|
|
}
|
|
if parent["Notes"] != "Kickoff" || parent["Kickoff"] != "Meetings" || parent["Retro"] != "Meetings" || parent["2025"] != "Archive" {
|
|
t.Errorf("parents = %v", parent)
|
|
}
|
|
if len(imp.assets) != 2 || imp.assets[0].Name != "onenote-r1.png" || imp.assets[1].Name != "plan.pdf" {
|
|
t.Errorf("assets = %+v", imp.assets)
|
|
}
|
|
kick, _ := json.Marshal(imp.pages[2].Doc)
|
|
if !strings.Contains(string(kick), `"src":"asset:0"`) || !strings.Contains(string(kick), `"href":"asset:1"`) || !strings.Contains(string(kick), "plan.pdf") {
|
|
t.Errorf("kickoff = %s", kick)
|
|
}
|
|
}
|