Go boilerplate library for building atproto apps
atproto go
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 119 lines 2.5 kB view raw
1package sqlite 2 3import ( 4 "context" 5 "database/sql" 6 "testing" 7 8 "github.com/bluesky-social/indigo/atproto/auth/oauth" 9 "github.com/bluesky-social/indigo/atproto/syntax" 10 _ "modernc.org/sqlite" 11) 12 13func testStore(t *testing.T) *Store { 14 t.Helper() 15 db, err := sql.Open("sqlite", ":memory:") 16 if err != nil { 17 t.Fatal(err) 18 } 19 s, err := New(db) 20 if err != nil { 21 t.Fatal(err) 22 } 23 t.Cleanup(func() { db.Close() }) 24 return s 25} 26 27func TestNew(t *testing.T) { 28 s := testStore(t) 29 if s == nil { 30 t.Fatal("expected non-nil store") 31 } 32} 33 34func TestSessionCRUD(t *testing.T) { 35 s := testStore(t) 36 ctx := context.Background() 37 did, _ := syntax.ParseDID("did:plc:test123") 38 39 sess := oauth.ClientSessionData{ 40 AccountDID: did, 41 SessionID: "sess-1", 42 HostURL: "https://pds.example.com", 43 } 44 45 // Save 46 if err := s.SaveSession(ctx, sess); err != nil { 47 t.Fatal(err) 48 } 49 50 // Get 51 got, err := s.GetSession(ctx, did, "sess-1") 52 if err != nil { 53 t.Fatal(err) 54 } 55 if got.HostURL != "https://pds.example.com" { 56 t.Fatalf("got HostURL %q, want %q", got.HostURL, "https://pds.example.com") 57 } 58 59 // Upsert 60 sess.HostURL = "https://pds2.example.com" 61 if err := s.SaveSession(ctx, sess); err != nil { 62 t.Fatal(err) 63 } 64 got, _ = s.GetSession(ctx, did, "sess-1") 65 if got.HostURL != "https://pds2.example.com" { 66 t.Fatalf("upsert failed: got %q", got.HostURL) 67 } 68 69 // Get non-existent 70 _, err = s.GetSession(ctx, did, "no-such") 71 if err == nil { 72 t.Fatal("expected error for missing session") 73 } 74 75 // Delete 76 if err := s.DeleteSession(ctx, did, "sess-1"); err != nil { 77 t.Fatal(err) 78 } 79 _, err = s.GetSession(ctx, did, "sess-1") 80 if err == nil { 81 t.Fatal("expected error after delete") 82 } 83} 84 85func TestAuthRequestCRUD(t *testing.T) { 86 s := testStore(t) 87 ctx := context.Background() 88 89 info := oauth.AuthRequestData{ 90 State: "state-abc", 91 AuthServerURL: "https://auth.example.com", 92 PKCEVerifier: "verifier123", 93 } 94 95 if err := s.SaveAuthRequestInfo(ctx, info); err != nil { 96 t.Fatal(err) 97 } 98 99 got, err := s.GetAuthRequestInfo(ctx, "state-abc") 100 if err != nil { 101 t.Fatal(err) 102 } 103 if got.PKCEVerifier != "verifier123" { 104 t.Fatalf("got verifier %q", got.PKCEVerifier) 105 } 106 107 _, err = s.GetAuthRequestInfo(ctx, "no-such") 108 if err == nil { 109 t.Fatal("expected error for missing auth request") 110 } 111 112 if err := s.DeleteAuthRequestInfo(ctx, "state-abc"); err != nil { 113 t.Fatal(err) 114 } 115 _, err = s.GetAuthRequestInfo(ctx, "state-abc") 116 if err == nil { 117 t.Fatal("expected error after delete") 118 } 119}