| new file mode 100644 |
| index 0000000..987537b |
| --- /dev/null |
| +++ b/store_test.go |
| @@ -0,0 +1,218 @@ |
| +package aviso |
| + |
| +import ( |
| + "context" |
| + "crypto/ecdh" |
| + "crypto/rand" |
| + "database/sql" |
| + "encoding/base64" |
| + "errors" |
| + "path/filepath" |
| + "testing" |
| + "time" |
| + |
| + "amadan.net/rastrillo/rastrillo/db" |
| + "amadan.net/rastrillo/rastrillo/migrate" |
| +) |
| + |
| +func openInternalDB(t *testing.T) *sql.DB { |
| + t.Helper() |
| + d, err := db.Open(filepath.Join(t.TempDir(), "aviso.db"), nil) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { d.Close() }) |
| + if _, err := migrate.Apply(context.Background(), d, Schema); err != nil { |
| + t.Fatal(err) |
| + } |
| + return d.Writer() |
| +} |
| + |
| +func newInternalService(t *testing.T) *Service { |
| + t.Helper() |
| + key, _ := GenerateKey() |
| + s, err := New(Config{DB: openInternalDB(t), PrivateKey: key, Contact: "mailto:x@y", Origin: "https://a"}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + return s |
| +} |
| + |
| +// sub builds a subscription with real keys: webpush-go decodes p256dh |
| +// into a P-256 point and auth into 16 bytes before any HTTP happens, |
| +// so a placeholder string would fail in the encryptor, not the test. |
| +func sub(endpoint string) Subscription { |
| + k, err := ecdh.P256().GenerateKey(rand.Reader) |
| + if err != nil { |
| + panic(err) |
| + } |
| + auth := make([]byte, 16) |
| + if _, err := rand.Read(auth); err != nil { |
| + panic(err) |
| + } |
| + return Subscription{ |
| + Endpoint: endpoint, |
| + P256dh: base64.RawURLEncoding.EncodeToString(k.PublicKey().Bytes()), |
| + Auth: base64.RawURLEncoding.EncodeToString(auth), |
| + } |
| +} |
| + |
| +func TestPutInsertsThenUpdatesSameOwner(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + if err := s.put(ctx, "alice", sub("https://push.example/1"), ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, _ := s.List(ctx, "alice") |
| + if len(rows) != 1 || rows[0].Revision != 1 || rows[0].VAPIDKeyID != s.keyID || rows[0].Subject != "alice" { |
| + t.Fatalf("after insert: %+v", rows) |
| + } |
| + again := sub("https://push.example/1") |
| + if err := s.put(ctx, "alice", again, ""); err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, _ = s.List(ctx, "alice") |
| + if len(rows) != 1 || rows[0].Revision != 2 || rows[0].Auth != again.Auth { |
| + t.Fatalf("after update: %+v", rows) |
| + } |
| +} |
| + |
| +func TestPutRefusesCrossOwner(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + _ = s.put(ctx, "alice", sub("https://push.example/1"), "") |
| + err := s.put(ctx, "bob", sub("https://push.example/1"), "") |
| + if !errors.Is(err, ErrOwnedElsewhere) { |
| + t.Fatalf("got %v, want ErrOwnedElsewhere", err) |
| + } |
| + rows, _ := s.List(ctx, "alice") |
| + if len(rows) != 1 { |
| + t.Fatal("alice lost her row") |
| + } |
| + if rows, _ := s.List(ctx, "bob"); len(rows) != 0 { |
| + t.Fatal("bob gained a row") |
| + } |
| +} |
| + |
| +func TestPutDeletesPreviousOnlyWhenOwned(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + _ = s.put(ctx, "alice", sub("https://push.example/old"), "") |
| + _ = s.put(ctx, "bob", sub("https://push.example/bobs"), "") |
| + // alice re-subscribes and names her old endpoint: gone. |
| + if err := s.put(ctx, "alice", sub("https://push.example/new"), "https://push.example/old"); err != nil { |
| + t.Fatal(err) |
| + } |
| + rows, _ := s.List(ctx, "alice") |
| + if len(rows) != 1 || rows[0].Endpoint != "https://push.example/new" { |
| + t.Fatalf("alice rows: %+v", rows) |
| + } |
| + // alice names bob's endpoint as previous: bob keeps it. |
| + _ = s.put(ctx, "alice", sub("https://push.example/new2"), "https://push.example/bobs") |
| + rows, _ = s.List(ctx, "bob") |
| + if len(rows) != 1 { |
| + t.Fatal("bob's row deleted by alice's previousEndpoint") |
| + } |
| +} |
| + |
| +func TestPutPreviousEqualToNewIsNotADelete(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + _ = s.put(ctx, "alice", sub("https://push.example/same"), "") |
| + if err := s.put(ctx, "alice", sub("https://push.example/same"), "https://push.example/same"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rows, _ := s.List(ctx, "alice"); len(rows) != 1 || rows[0].Revision != 2 { |
| + t.Fatalf("rows: %+v", rows) |
| + } |
| +} |
| + |
| +func TestDeleteOwnIsOwnerScoped(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + _ = s.put(ctx, "alice", sub("https://push.example/1"), "") |
| + if err := s.deleteOwn(ctx, "bob", "https://push.example/1"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rows, _ := s.List(ctx, "alice"); len(rows) != 1 { |
| + t.Fatal("bob deleted alice's row") |
| + } |
| + _ = s.deleteOwn(ctx, "alice", "https://push.example/1") |
| + if rows, _ := s.List(ctx, "alice"); len(rows) != 0 { |
| + t.Fatal("own delete did nothing") |
| + } |
| +} |
| + |
| +func TestConfirmAndPruneAreRevisionConditional(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + _ = s.put(ctx, "alice", sub("https://push.example/1"), "") |
| + before, _ := s.List(ctx, "alice") |
| + id := before[0].ID |
| + // Browser refreshes: revision 2. |
| + _ = s.put(ctx, "alice", sub("https://push.example/1"), "") |
| + // A send that captured revision 1 comes back 410: must not prune. |
| + if err := s.prune(ctx, id, 1); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rows, _ := s.List(ctx, "alice"); len(rows) != 1 { |
| + t.Fatal("stale prune deleted a refreshed subscription") |
| + } |
| + // Stale confirm must not touch last_confirmed_at. |
| + s.now = func() time.Time { return time.Unix(1_800_000_000, 0) } |
| + _ = s.confirm(ctx, id, 1) |
| + var got int64 |
| + _ = s.cfg.DB.QueryRow(`SELECT last_confirmed_at FROM aviso_subscriptions WHERE id=?`, id).Scan(&got) |
| + if got == 1_800_000_000 { |
| + t.Fatal("stale confirm bumped last_confirmed_at") |
| + } |
| + _ = s.confirm(ctx, id, 2) |
| + _ = s.cfg.DB.QueryRow(`SELECT last_confirmed_at FROM aviso_subscriptions WHERE id=?`, id).Scan(&got) |
| + if got != 1_800_000_000 { |
| + t.Fatalf("current confirm did not bump: %d", got) |
| + } |
| + if err := s.prune(ctx, id, 2); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rows, _ := s.List(ctx, "alice"); len(rows) != 0 { |
| + t.Fatal("current prune did not delete") |
| + } |
| +} |
| + |
| +func TestSweepAndDeleteSubject(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + s.now = func() time.Time { return time.Unix(1000, 0) } |
| + _ = s.put(ctx, "alice", sub("https://push.example/old"), "") |
| + s.now = func() time.Time { return time.Unix(2000, 0) } |
| + _ = s.put(ctx, "alice", sub("https://push.example/new"), "") |
| + _ = s.put(ctx, "bob", sub("https://push.example/bob"), "") |
| + if err := s.Sweep(ctx, time.Unix(1500, 0)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rows, _ := s.List(ctx, "alice"); len(rows) != 1 || rows[0].Endpoint != "https://push.example/new" { |
| + t.Fatalf("sweep: %+v", rows) |
| + } |
| + if err := s.DeleteSubject(ctx, "bob"); err != nil { |
| + t.Fatal(err) |
| + } |
| + if rows, _ := s.List(ctx, "bob"); len(rows) != 0 { |
| + t.Fatal("DeleteSubject left rows") |
| + } |
| + if rows, _ := s.List(ctx, "alice"); len(rows) != 1 { |
| + t.Fatal("DeleteSubject touched another subject") |
| + } |
| +} |
| + |
| +func TestListOrdersOldestFirst(t *testing.T) { |
| + s := newInternalService(t) |
| + ctx := context.Background() |
| + s.now = func() time.Time { return time.Unix(2000, 0) } |
| + _ = s.put(ctx, "alice", sub("https://push.example/second"), "") |
| + s.now = func() time.Time { return time.Unix(1000, 0) } |
| + _ = s.put(ctx, "alice", sub("https://push.example/first"), "") |
| + rows, _ := s.List(ctx, "alice") |
| + if len(rows) != 2 || rows[0].Endpoint != "https://push.example/first" { |
| + t.Fatalf("order: %+v", rows) |
| + } |
| +} |