go-mastodon/apps_test.go

143 lines
3.6 KiB
Go
Raw Permalink Normal View History

2017-04-16 13:06:54 +02:00
package mastodon
import (
"context"
2017-04-16 13:06:54 +02:00
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"
2017-04-16 13:06:54 +02:00
)
func TestRegisterApp(t *testing.T) {
2017-04-20 19:29:22 +02:00
isNotJSON := true
2017-04-16 13:06:54 +02:00
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
2017-04-20 19:29:22 +02:00
} else if r.URL.Path != "/api/v1/apps" {
2017-04-16 13:06:54 +02:00
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
2017-04-20 19:29:22 +02:00
} else if r.FormValue("redirect_uris") != "urn:ietf:wg:oauth:2.0:oob" {
2017-04-16 13:06:54 +02:00
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
2017-04-20 19:29:22 +02:00
} else if isNotJSON {
isNotJSON = false
fmt.Fprintln(w, `<html><head><title>Apps</title></head></html>`)
return
2017-04-16 13:06:54 +02:00
}
2017-11-30 06:55:18 +01:00
fmt.Fprintln(w, `{"id": 123, "client_id": "foo", "client_secret": "bar"}`)
2017-04-16 13:06:54 +02:00
}))
defer ts.Close()
2017-04-20 19:29:22 +02:00
// Status not ok.
_, err := RegisterApp(context.Background(), &AppConfig{
Server: ts.URL,
RedirectURIs: "/",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Error in url.Parse
_, err = RegisterApp(context.Background(), &AppConfig{
Server: ":",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Error in json.NewDecoder
_, err = RegisterApp(context.Background(), &AppConfig{
Server: ts.URL,
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
// Success.
app, err := RegisterApp(context.Background(), &AppConfig{
2017-04-16 13:06:54 +02:00
Server: ts.URL,
Scopes: "read write follow",
})
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
2017-11-30 06:55:18 +01:00
if string(app.ID) != "123" {
t.Fatalf("want %q but %q", "bar", app.ClientSecret)
}
2017-04-16 13:06:54 +02:00
if app.ClientID != "foo" {
t.Fatalf("want %q but %q", "foo", app.ClientID)
}
if app.ClientSecret != "bar" {
t.Fatalf("want %q but %q", "bar", app.ClientSecret)
}
}
func TestRegisterAppWithCancel(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(3 * time.Second)
fmt.Fprintln(w, `{"client_id": "foo", "client_secret": "bar"}`)
}))
defer ts.Close()
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := RegisterApp(ctx, &AppConfig{
Server: ts.URL,
Scopes: "read write follow",
})
if err == nil {
t.Fatalf("should be fail: %v", err)
}
2019-09-30 10:04:44 +02:00
if want := fmt.Sprintf("Post %q: context canceled", ts.URL+"/api/v1/apps"); want != err.Error() {
t.Fatalf("want %q but %q", want, err.Error())
}
}
2022-08-27 01:33:24 +02:00
func TestVerifyAppCredentials(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer zoo" {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
return
}
if r.URL.Path != "/api/v1/apps/verify_credentials" {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
fmt.Fprintln(w, `{"name":"zzz","website":"yyy","vapid_key":"xxx"}`)
}))
defer ts.Close()
client := NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zip",
})
_, err := client.VerifyAppCredentials(context.Background())
if err == nil {
t.Fatalf("should be fail: %v", err)
}
client = NewClient(&Config{
Server: ts.URL,
ClientID: "foo",
ClientSecret: "bar",
AccessToken: "zoo",
})
a, err := client.VerifyAppCredentials(context.Background())
if err != nil {
t.Fatalf("should not be fail: %v", err)
}
if a.Name != "zzz" {
t.Fatalf("want %q but %q", "zzz", a.Name)
}
if a.Website != "yyy" {
t.Fatalf("want %q but %q", "yyy", a.Name)
}
if a.VapidKey != "xxx" {
t.Fatalf("want %q but %q", "xxx", a.Name)
}
}