github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/service/apiserver_test.go (about) 1 package service 2 3 import ( 4 "testing" 5 6 "github.com/keybase/client/go/kbtest" 7 "github.com/keybase/client/go/libkb" 8 keybase1 "github.com/keybase/client/go/protocol/keybase1" 9 jsonw "github.com/keybase/go-jsonw" 10 ) 11 12 func TestAPIServerGet(t *testing.T) { 13 tc := libkb.SetupTest(t, "apiserver", 2) 14 defer tc.Cleanup() 15 tc.G.SetService() 16 17 _, err := kbtest.CreateAndSignupFakeUser("apivr", tc.G) 18 if err != nil { 19 t.Fatal(err) 20 } 21 22 harg := []keybase1.StringKVPair{ 23 {Key: "username", Value: "t_alice"}, 24 {Key: "fields", Value: "basics"}, 25 } 26 27 arg := keybase1.GetArg{ 28 Endpoint: "user/lookup", 29 Args: harg, 30 } 31 mctx := libkb.NewMetaContextForTest(tc) 32 handler := NewAPIServerHandler(nil, tc.G) 33 res, err := handler.doGet(mctx, arg, false) 34 if err != nil { 35 t.Fatal(err) 36 } 37 38 jw, err := jsonw.Unmarshal([]byte(res.Body)) 39 if err != nil { 40 t.Fatal(err) 41 } 42 43 usernamew := jw.AtKey("them").AtKey("basics").AtKey("username") 44 username, err := usernamew.GetString() 45 if err != nil { 46 t.Fatal(err) 47 } 48 49 if username != "t_alice" { 50 t.Fatalf("wrong username returned: %s != %s", username, "t_alice") 51 } 52 } 53 54 func TestAPIServerPost(t *testing.T) { 55 56 tc := libkb.SetupTest(t, "apiserver", 2) 57 defer tc.Cleanup() 58 tc.G.SetService() 59 60 _, err := kbtest.CreateAndSignupFakeUser("apivr", tc.G) 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 harg := []keybase1.StringKVPair{ 66 {Key: "email_or_username", Value: "t_alice"}, 67 } 68 69 arg := keybase1.PostArg{ 70 Endpoint: "getsalt", 71 Args: harg, 72 } 73 74 handler := NewAPIServerHandler(nil, tc.G) 75 mctx := libkb.NewMetaContextForTest(tc) 76 res, err := handler.doPost(mctx, arg) 77 if err != nil { 78 t.Fatal(err) 79 } 80 81 jw, err := jsonw.Unmarshal([]byte(res.Body)) 82 if err != nil { 83 t.Fatal(err) 84 } 85 86 namew := jw.AtKey("status").AtKey("name") 87 name, err := namew.GetString() 88 if err != nil { 89 t.Fatal(err) 90 } 91 92 if name != "OK" { 93 t.Fatalf("wrong name returned: %s != %s", name, "OK") 94 } 95 } 96 97 func TestAPIServerPostJSON(t *testing.T) { 98 99 tc := libkb.SetupTest(t, "apiserver", 2) 100 defer tc.Cleanup() 101 tc.G.SetService() 102 103 _, err := kbtest.CreateAndSignupFakeUser("apivr", tc.G) 104 if err != nil { 105 t.Fatal(err) 106 } 107 108 jsonPayload := []keybase1.StringKVPair{ 109 {Key: "sigs", Value: "[]"}, 110 } 111 112 arg := keybase1.PostJSONArg{ 113 Endpoint: "key/multi", 114 JSONPayload: jsonPayload, 115 } 116 117 handler := NewAPIServerHandler(nil, tc.G) 118 res, err := handler.doPostJSON(libkb.NewMetaContextForTest(tc), arg) 119 if err != nil { 120 t.Fatal(err) 121 } 122 123 jw, err := jsonw.Unmarshal([]byte(res.Body)) 124 if err != nil { 125 t.Fatal(err) 126 } 127 128 namew := jw.AtKey("status").AtKey("name") 129 name, err := namew.GetString() 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 if name != "OK" { 135 t.Fatalf("wrong name returned: %s != %s", name, "OK") 136 } 137 }