Added tests for roundtrip and register #4
90
api_test.go
90
api_test.go
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -508,3 +509,92 @@ func TestDelete(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRegister(t *testing.T) {
|
||||||
|
db, done := testDB(t)
|
||||||
|
if db == nil {
|
||||||
|
t.Fatalf("could not create temp db")
|
||||||
|
}
|
||||||
|
defer done()
|
||||||
|
|
||||||
|
sm := http.NewServeMux()
|
||||||
|
mm := &mockMail{}
|
||||||
|
NewServer(sm, db, mm, "", window, false)
|
||||||
|
ts := httptest.NewServer(sm)
|
||||||
|
|
||||||
|
u := fmt.Sprintf("%s%s?email=fake@example.com", ts.URL, prefix["register"])
|
||||||
|
req, err := http.NewRequest("POST", u, nil)
|
||||||
|
_, err = http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("couldn't POST: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err = http.NewRequest(
|
||||||
|
"GET",
|
||||||
|
strings.Replace(mm.msg, "https", "http", -1), //ugly hack to get around https vs http
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
_, err = http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("couldn't POST: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.hasUser("fake@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("user was no correctly added to database", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRoundTrip(t *testing.T) {
|
||||||
|
db, done := testDB(t)
|
||||||
|
if db == nil {
|
||||||
|
t.Fatalf("could not create temp db")
|
||||||
|
}
|
||||||
|
defer done()
|
||||||
|
|
||||||
|
sm := http.NewServeMux()
|
||||||
|
mm := &mockMail{}
|
||||||
|
NewServer(sm, db, mm, "", window, false)
|
||||||
|
ts := httptest.NewServer(sm)
|
||||||
|
|
||||||
|
u := fmt.Sprintf("%s%s?email=fake@example.com", ts.URL, prefix["register"])
|
||||||
|
req, err := http.NewRequest("POST", u, nil)
|
||||||
|
_, err = http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("couldn't POST: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err = http.NewRequest(
|
||||||
|
"GET",
|
||||||
|
strings.Replace(mm.msg, "https", "http", -1), //ugly hack to get around https vs http
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
resp, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("couldn't POST: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.hasUser("fake@example.com")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("user was no correctly added to database", err)
|
||||||
|
}
|
||||||
|
bs, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("Failed to parse response body", err)
|
||||||
|
}
|
||||||
|
tok := strings.Trim(string(bs), "new token: ")
|
||||||
|
|
||||||
|
u = fmt.Sprintf("%s/foo", ts.URL)
|
||||||
|
body := strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)
|
||||||
|
req, err = http.NewRequest("POST", u, body)
|
||||||
|
req.Header.Add("Content-Type", "application/json")
|
||||||
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok))
|
||||||
|
resp, err = http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("couldn't POST: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got, want := len(db.Pkgs()), 1; got != want {
|
||||||
|
t.Fatalf("pkgs should have something in it; got %d, want %d", got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user