porting all github actions from bluesky-social/indigo to tangled CI
at main 2.9 kB view raw
1package pds 2 3import ( 4 "context" 5 "crypto/ecdsa" 6 "crypto/elliptic" 7 "crypto/rand" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/bluesky-social/indigo/api/atproto" 13 "github.com/bluesky-social/indigo/carstore" 14 "github.com/bluesky-social/indigo/plc" 15 "github.com/bluesky-social/indigo/util/cliutil" 16 "github.com/whyrusleeping/go-did" 17 "gorm.io/gorm" 18) 19 20func testCarStore(t *testing.T, db *gorm.DB) (carstore.CarStore, func()) { 21 t.Helper() 22 tempdir, err := os.MkdirTemp("", "msttest-") 23 if err != nil { 24 t.Fatal(err) 25 } 26 27 sharddir := filepath.Join(tempdir, "shards") 28 if err := os.MkdirAll(sharddir, 0775); err != nil { 29 t.Fatal(err) 30 } 31 32 cs, err := carstore.NewCarStore(db, []string{sharddir}) 33 if err != nil { 34 t.Fatal(err) 35 } 36 37 return cs, func() { _ = os.RemoveAll(tempdir) } 38} 39 40func newTestServer(t *testing.T) (*Server, func()) { 41 t.Helper() 42 db, err := cliutil.SetupDatabase("sqlite://:memory:", 40) 43 if err != nil { 44 t.Fatal(err) 45 } 46 cs, cleanup := testCarStore(t, db) 47 fakePlc := plc.NewFakeDid(db) 48 raw, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) 49 if err != nil { 50 t.Fatal(err) 51 } 52 serkey := &did.PrivKey{ 53 Raw: raw, 54 Type: did.KeyTypeP256, 55 } 56 57 s, err := NewServer(db, cs, serkey, ".test", "", fakePlc, []byte("jwtsecretplaceholder")) 58 if err != nil { 59 t.Fatal(err) 60 } 61 return s, func() { 62 cleanup() 63 sqlDB, err := db.DB() 64 if err != nil { 65 t.Fatal(err) 66 } 67 err = sqlDB.Close() 68 if err != nil { 69 t.Fatal(err) 70 } 71 } 72} 73 74func TestHandleComAtprotoAccountCreate(t *testing.T) { 75 s, cleanup := newTestServer(t) 76 defer cleanup() 77 78 e := "test@foo.com" 79 p := "password" 80 o, err := s.handleComAtprotoServerCreateAccount(context.Background(), &atproto.ServerCreateAccount_Input{ 81 Email: &e, 82 Password: &p, 83 Handle: "testman.test", 84 }) 85 if err != nil { 86 t.Fatal(err) 87 } 88 u, err := s.lookupUserByDid(context.Background(), o.Did) 89 if err != nil { 90 t.Fatal(err) 91 } 92 if u.Email != "test@foo.com" { 93 t.Fatal("user has different email") 94 } 95 96} 97 98func TestHandleComAtprotoSessionCreate(t *testing.T) { 99 s, cleanup := newTestServer(t) 100 defer cleanup() 101 102 e := "test@foo.com" 103 p := "password" 104 o, err := s.handleComAtprotoServerCreateAccount(context.Background(), &atproto.ServerCreateAccount_Input{ 105 Email: &e, 106 Password: &p, 107 Handle: "testman.test", 108 }) 109 if err != nil { 110 t.Fatal(err) 111 } 112 so, err := s.handleComAtprotoServerCreateSession(context.Background(), &atproto.ServerCreateSession_Input{ 113 Identifier: o.Handle, 114 Password: "password", 115 }) 116 if err != nil { 117 t.Fatal(err) 118 } 119 if so.Handle != o.Handle { 120 t.Fatal("user has different handle") 121 } 122 123 _, err = s.handleComAtprotoServerCreateSession(context.Background(), &atproto.ServerCreateSession_Input{ 124 Identifier: o.Handle, 125 Password: "invalid", 126 }) 127 if err != ErrInvalidUsernameOrPassword { 128 t.Fatalf("expected error %s, got %s\n", ErrInvalidUsernameOrPassword, err) 129 } 130}