2016-02-14 21:10:18 -08:00
|
|
|
package vain
|
|
|
|
|
|
|
|
import (
|
2016-02-15 01:10:14 -08:00
|
|
|
"bytes"
|
2016-02-23 22:09:29 -08:00
|
|
|
"encoding/json"
|
2016-02-14 21:10:18 -08:00
|
|
|
"fmt"
|
2016-02-15 01:10:14 -08:00
|
|
|
"io"
|
2016-06-03 13:42:05 -07:00
|
|
|
"io/ioutil"
|
2016-02-14 21:10:18 -08:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2016-02-23 22:09:29 -08:00
|
|
|
"net/url"
|
2016-02-14 21:10:18 -08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2016-05-14 21:30:58 -07:00
|
|
|
"time"
|
2016-02-14 21:10:18 -08:00
|
|
|
)
|
|
|
|
|
2016-05-14 21:30:58 -07:00
|
|
|
const window = 5 * time.Minute
|
|
|
|
|
2016-02-14 21:10:18 -08:00
|
|
|
func TestAdd(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
2016-02-23 22:09:29 -08:00
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-02-23 22:09:29 -08:00
|
|
|
ts := httptest.NewServer(sm)
|
2016-04-11 20:43:18 -07:00
|
|
|
tok, err := db.addUser("sm@example.org")
|
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
|
2016-02-14 21:10:18 -08:00
|
|
|
resp, err := http.Get(ts.URL)
|
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't GET: %v", err)
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
if got, want := len(db.Pkgs()), 0; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("started with something in it; got %d, want %d", got, want)
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
bad := ts.URL
|
|
|
|
body := strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)
|
|
|
|
req, err := http.NewRequest("POST", bad, 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 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
t.Fatalf("bad request got incorrect status: got %d, want %d", got, want)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
resp.Body.Close()
|
|
|
|
|
|
|
|
if got, want := len(db.Pkgs()), 0; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("started with something in it; got %d, want %d", got, want)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
|
|
|
|
2016-02-23 22:09:29 -08:00
|
|
|
{
|
2016-04-11 20:43:18 -07:00
|
|
|
u := fmt.Sprintf("%s/%s", ts.URL, prefix["pkgs"])
|
2016-02-23 22:09:29 -08:00
|
|
|
resp, err := http.Get(u)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
pkgs := []Package{}
|
|
|
|
if err := json.NewDecoder(buf).Decode(&pkgs); err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("problem parsing json: %v, \n%q", err, buf)
|
2016-02-23 22:09:29 -08:00
|
|
|
}
|
|
|
|
if got, want := len(pkgs), 0; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have empty pkg list; got %d, want %d", got, want)
|
2016-02-23 22:09:29 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
{
|
2016-02-14 21:10:18 -08:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
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 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("problem performing request: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%v", buf)
|
|
|
|
resp.Body.Close()
|
2016-02-14 21:10:18 -08:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
if got, want := len(db.Pkgs()), 1; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("pkgs should have something in it; got %d, want %d", got, want)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
t.Logf("packages: %v", db.Pkgs())
|
|
|
|
|
|
|
|
ur, err := url.Parse(ts.URL)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
good := fmt.Sprintf("%s/foo", ur.Host)
|
|
|
|
|
2016-06-22 23:01:21 -07:00
|
|
|
if !db.PackageExists(path(good)) {
|
2016-04-11 20:43:18 -07:00
|
|
|
t.Fatalf("did not find package for %s; should have posted a valid package", good)
|
|
|
|
}
|
|
|
|
p, err := db.Package(good)
|
|
|
|
t.Logf("%+v", p)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("problem getting package: %v", err)
|
|
|
|
}
|
|
|
|
if got, want := p.Path, good; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("package name did not go through as expected; got %q, want %q", got, want)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
if got, want := p.Repo, "https://s.mcquay.me/sm/vain"; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("repo did not go through as expected; got %q, want %q", got, want)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
if got, want := p.Vcs, "git"; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("Vcs did not go through as expected; got %q, want %q", got, want)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
2016-02-15 01:10:14 -08:00
|
|
|
|
2016-04-23 22:16:40 -07:00
|
|
|
resp, err = http.Get(ts.URL + "?go-get=1")
|
2016-02-15 01:10:14 -08:00
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't GET: %v", err)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if want := http.StatusOK; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("Should have succeeded to fetch /; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
if _, err := io.Copy(buf, resp.Body); err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't read content from server: %v", err)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-03-02 22:07:06 -08:00
|
|
|
if got, want := strings.Count(buf.String(), "<meta"), 1; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("did not find all the tags I need; got %d, want %d", got, want)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-02-23 22:09:29 -08:00
|
|
|
|
|
|
|
{
|
2016-04-11 20:43:18 -07:00
|
|
|
u := fmt.Sprintf("%s/%s", ts.URL, prefix["pkgs"])
|
2016-02-23 22:09:29 -08:00
|
|
|
resp, err := http.Get(u)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
pkgs := []Package{}
|
|
|
|
if err := json.NewDecoder(buf).Decode(&pkgs); err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("problem parsing json: %v, \n%q", err, buf)
|
2016-02-23 22:09:29 -08:00
|
|
|
}
|
|
|
|
if got, want := len(pkgs), 1; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should (mildly) populated pkg list; got %d, want %d", got, want)
|
2016-02-23 22:09:29 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidPath(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
defer done()
|
2016-02-14 21:10:18 -08:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
|
|
|
if err != nil {
|
2016-05-23 23:54:35 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bad := ts.URL
|
|
|
|
body := strings.NewReader(`{"repo": "https://s.mcquay.me/sm/vain"}`)
|
|
|
|
req, err := http.NewRequest("POST", bad, body)
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok))
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2016-02-14 21:10:18 -08:00
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
if len(db.Pkgs()) != 0 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to insert; got %d, want %d", len(db.Pkgs()), 0)
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to post at bad route; got %s, want %s", http.StatusText(got), http.StatusText(want))
|
2016-02-14 21:10:18 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-14 21:52:26 -08:00
|
|
|
|
|
|
|
func TestCannotDuplicateExistingPath(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
defer done()
|
2016-02-14 21:52:26 -08:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
2016-02-14 21:52:26 -08:00
|
|
|
if err != nil {
|
2016-05-23 23:54:35 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
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 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
if want := http.StatusOK; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
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 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
if want := http.StatusConflict; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCannotAddExistingSubPath(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
defer done()
|
2016-02-14 21:52:26 -08:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
2016-02-14 21:52:26 -08:00
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
{
|
|
|
|
u := fmt.Sprintf("%s/foo/bar", ts.URL)
|
|
|
|
t.Logf("url: %v", u)
|
|
|
|
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 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
if want := http.StatusOK; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
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 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
if want := http.StatusConflict; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("initial post should have worked; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
2016-02-14 21:52:26 -08:00
|
|
|
}
|
|
|
|
}
|
2016-02-15 01:10:14 -08:00
|
|
|
|
|
|
|
func TestMissingRepo(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
u := fmt.Sprintf("%s/foo", ts.URL)
|
|
|
|
body := strings.NewReader(`{}`)
|
|
|
|
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)
|
2016-02-15 01:10:14 -08:00
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
if len(db.Pkgs()) != 0 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to insert; got %d, want %d", len(db.Pkgs()), 0)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
if want := http.StatusBadRequest; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to post with bad payload; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadJson(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
u := fmt.Sprintf("%s/foo", ts.URL)
|
|
|
|
body := strings.NewReader(`{`)
|
|
|
|
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)
|
2016-02-15 01:10:14 -08:00
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
if len(db.Pkgs()) != 0 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to insert; got %d, want %d", len(db.Pkgs()), 0)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
if want := http.StatusBadRequest; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
func TestNoAuth(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
2016-03-01 23:41:44 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
// here we don't set the Authorization header
|
|
|
|
// req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok))
|
|
|
|
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2016-03-01 23:41:44 -08:00
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-03-01 23:41:44 -08:00
|
|
|
}
|
|
|
|
resp.Body.Close()
|
2016-04-11 20:43:18 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusUnauthorized; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("posted with missing auth; got %v, want %v", http.StatusText(got), http.StatusText(want))
|
2016-03-01 23:41:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
func TestBadVcs(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
2016-02-15 01:10:14 -08:00
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
u := fmt.Sprintf("%s/foo", ts.URL)
|
|
|
|
body := strings.NewReader(`{"vcs": "bitbucket", "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 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
resp.Body.Close()
|
|
|
|
if got, want := resp.StatusCode, http.StatusBadRequest; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have reported bad vcs specified; got %v, want %v", http.StatusText(got), http.StatusText(want))
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
func TestUnsupportedMethod(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-04-11 20:43:18 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
|
2016-02-15 01:10:14 -08:00
|
|
|
url := fmt.Sprintf("%s/foo", ts.URL)
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("PUT", url, nil)
|
2016-04-11 20:43:18 -07:00
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok))
|
2016-02-15 01:10:14 -08:00
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
2016-04-11 20:43:18 -07:00
|
|
|
if len(db.Pkgs()) != 0 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to insert; got %d, want %d", len(db.Pkgs()), 0)
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
if want := http.StatusMethodNotAllowed; resp.StatusCode != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have failed to post at bad route; got %s, want %s", resp.Status, http.StatusText(want))
|
2016-02-15 01:10:14 -08:00
|
|
|
}
|
|
|
|
}
|
2016-03-01 23:34:31 -08:00
|
|
|
|
|
|
|
func TestDelete(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-04-11 20:43:18 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
2016-03-01 23:34:31 -08:00
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, false)
|
2016-03-01 23:34:31 -08:00
|
|
|
ts := httptest.NewServer(sm)
|
2016-04-11 20:43:18 -07:00
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
2016-03-01 23:34:31 -08:00
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-03-01 23:34:31 -08:00
|
|
|
}
|
2016-06-22 23:01:21 -07:00
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
t.Logf("%v", tok)
|
|
|
|
if len(db.Pkgs()) != 0 {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("started with something in it; got %d, want %d", len(db.Pkgs()), 0)
|
2016-03-01 23:34:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
u := fmt.Sprintf("%s/foo", ts.URL)
|
2016-04-11 20:43:18 -07:00
|
|
|
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)
|
2016-03-01 23:34:31 -08:00
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-03-01 23:34:31 -08:00
|
|
|
}
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
if got, want := len(db.Pkgs()), 1; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("pkgs should have something in it; got %d, want %d", got, want)
|
2016-03-01 23:34:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// test not found
|
|
|
|
u := fmt.Sprintf("%s/bar", ts.URL)
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("DELETE", u, nil)
|
2016-04-11 20:43:18 -07:00
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok))
|
2016-03-01 23:34:31 -08:00
|
|
|
resp, err = client.Do(req)
|
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-03-01 23:34:31 -08:00
|
|
|
}
|
|
|
|
if got, want := resp.StatusCode, http.StatusNotFound; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("should have not been able to delete unknown package; got %v, want %v", http.StatusText(got), http.StatusText(want))
|
2016-03-01 23:34:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-11 20:43:18 -07:00
|
|
|
{
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("DELETE", u, nil)
|
|
|
|
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", tok))
|
|
|
|
resp, err = client.Do(req)
|
|
|
|
if err != nil {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if got, want := len(db.Pkgs()), 0; got != want {
|
2016-04-23 22:16:40 -07:00
|
|
|
t.Fatalf("pkgs should be empty; got %d, want %d", got, want)
|
2016-04-11 20:43:18 -07:00
|
|
|
}
|
2016-03-01 23:34:31 -08:00
|
|
|
}
|
|
|
|
}
|
2016-06-03 16:44:50 -07:00
|
|
|
|
|
|
|
func TestSingleGet(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-06-03 16:44:50 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
2016-06-03 13:42:05 -07:00
|
|
|
NewServer(sm, db, nil, "", window, true)
|
2016-06-03 16:44:50 -07:00
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
tok, err := db.addUser("sm@example.org")
|
|
|
|
if err != nil {
|
2016-06-22 23:01:21 -07:00
|
|
|
t.Errorf("failure to add user: %v", err)
|
2016-06-03 16:44:50 -07:00
|
|
|
}
|
|
|
|
|
2016-06-22 23:01:21 -07:00
|
|
|
ns := namespace("foo")
|
2016-06-03 16:44:50 -07:00
|
|
|
|
|
|
|
if err := db.NSForToken(ns, tok); err != nil {
|
|
|
|
t.Fatalf("could not initialize namespace %q for user %q: %v", ns, tok, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := Package{
|
|
|
|
Vcs: "git",
|
|
|
|
Repo: "https://example.org/foo",
|
|
|
|
Path: fmt.Sprintf("%s/foo/bar", strings.TrimPrefix(ts.URL, "http://")),
|
|
|
|
Ns: ns,
|
|
|
|
}
|
|
|
|
if err := db.AddPackage(p); err != nil {
|
|
|
|
t.Fatalf("couldn't add package %v: %v", p, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// expected failure
|
|
|
|
resp, err := http.Get(ts.URL + "/bleh/blah?go-get=1")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("problem getting route: %v", err)
|
|
|
|
}
|
|
|
|
if got, want := resp.StatusCode, http.StatusNotFound; got != want {
|
|
|
|
t.Fatalf("should have failed to GET unknown route; got %s, want %s", http.StatusText(got), http.StatusText(want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
url := ts.URL + "/foo/bar?go-get=1"
|
|
|
|
resp, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("problem getting route: %v", err)
|
|
|
|
}
|
|
|
|
if got, want := resp.StatusCode, http.StatusOK; got != want {
|
|
|
|
t.Fatalf("should have failed to GET unknown route; got %s, want %s", http.StatusText(got), http.StatusText(want))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
func TestRegister(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-06-03 13:42:05 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
|
|
|
mm := &mockMail{}
|
|
|
|
NewServer(sm, db, mm, "", window, true)
|
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
u := fmt.Sprintf("%s%s", ts.URL, prefix["register"])
|
|
|
|
req, err := http.NewRequest("POST", u, nil)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
|
|
|
if status := resp.StatusCode; status != http.StatusBadRequest {
|
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
u = fmt.Sprintf("%s%s?email=notARealEmail", ts.URL, prefix["register"])
|
|
|
|
req, err = http.NewRequest("POST", u, nil)
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
|
|
|
if status := resp.StatusCode; status != http.StatusBadRequest {
|
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
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", mm.msg, nil)
|
|
|
|
_, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = db.user("fake@example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("user was no correctly added to database: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRoundTrip(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-06-03 13:42:05 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
|
|
|
mm := &mockMail{}
|
|
|
|
NewServer(sm, db, mm, "", window, true)
|
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
u := fmt.Sprintf("%s%s?email=fake@example.com", ts.URL, prefix["register"])
|
|
|
|
req, err := http.NewRequest("POST", u, nil)
|
2016-06-21 07:54:00 -07:00
|
|
|
resp, err := http.DefaultClient.Do(req)
|
2016-06-03 13:42:05 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
2016-06-21 07:54:00 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusOK; got != want {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
t.Fatalf("bad request got incorrect status: got %d, want %d", got, want)
|
|
|
|
}
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
req, err = http.NewRequest("GET", mm.msg, nil)
|
2016-06-21 07:54:00 -07:00
|
|
|
resp, err = http.DefaultClient.Do(req)
|
2016-06-03 13:42:05 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
2016-06-21 07:54:00 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusOK; got != want {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
t.Fatalf("bad request got incorrect status: got %d, want %d", got, want)
|
|
|
|
}
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
_, err = db.user("fake@example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("user was no correctly added to database: %v", err)
|
|
|
|
}
|
|
|
|
bs, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse response body: %v", err)
|
|
|
|
}
|
2016-06-21 21:35:50 -07:00
|
|
|
tok := strings.TrimSpace(strings.TrimPrefix(string(bs), "new token: "))
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2016-06-21 07:54:00 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusOK; got != want {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
t.Fatalf("bad request got incorrect status: got %d, want %d", got, want)
|
|
|
|
}
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
if got, want := len(db.Pkgs()), 1; got != want {
|
|
|
|
t.Fatalf("pkgs should have something in it; got %d, want %d", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestForgot(t *testing.T) {
|
2016-06-22 23:01:21 -07:00
|
|
|
db, done := TestDB(t)
|
2016-06-03 13:42:05 -07:00
|
|
|
if db == nil {
|
|
|
|
t.Fatalf("could not create temp db")
|
|
|
|
}
|
|
|
|
defer done()
|
|
|
|
|
|
|
|
sm := http.NewServeMux()
|
|
|
|
mm := &mockMail{}
|
|
|
|
NewServer(sm, db, mm, "", window, true)
|
|
|
|
ts := httptest.NewServer(sm)
|
|
|
|
|
|
|
|
//try to do forget before user is added
|
|
|
|
u := fmt.Sprintf("%s%s?email=fake@example.com", ts.URL, prefix["forgot"])
|
|
|
|
req, err := http.NewRequest("POST", u, nil)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
|
|
|
if status := resp.StatusCode; status != http.StatusNotFound {
|
2016-06-21 07:54:00 -07:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
2016-06-03 13:42:05 -07:00
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
u = fmt.Sprintf("%s%s?email=notARealEmail", ts.URL, prefix["forgot"])
|
|
|
|
req, err = http.NewRequest("POST", u, nil)
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
|
|
|
if status := resp.StatusCode; status != http.StatusBadRequest {
|
2016-06-21 07:54:00 -07:00
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
2016-06-03 13:42:05 -07:00
|
|
|
t.Fatalf("handler returned wrong status code: got %v want %v",
|
|
|
|
status, http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
//register a new user
|
|
|
|
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", mm.msg, nil)
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
2016-06-21 07:54:00 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusOK; got != want {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
t.Fatalf("bad request got incorrect status: got %d, want %d", got, want)
|
|
|
|
}
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
//check database for new user
|
|
|
|
_, err = db.user("fake@example.com")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("user was no correctly added to database: %v", err)
|
|
|
|
}
|
|
|
|
bs, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse response body: %v", err)
|
|
|
|
}
|
2016-06-21 21:35:50 -07:00
|
|
|
iniTok := strings.TrimPrefix(string(bs), "new token: ")
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
//get new token for user (using forgot)
|
|
|
|
u = fmt.Sprintf("%s%s?email=fake@example.com", ts.URL, prefix["forgot"])
|
|
|
|
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", mm.msg, nil)
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
2016-06-21 07:54:00 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusOK; got != want {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
t.Fatalf("bad request got incorrect status: got %d, want %d", got, want)
|
|
|
|
}
|
2016-06-03 13:42:05 -07:00
|
|
|
ft, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to parse response body: %v", err)
|
|
|
|
}
|
2016-06-21 21:35:50 -07:00
|
|
|
recTok := strings.TrimSpace(strings.TrimPrefix(string(ft), "new token: "))
|
2016-06-03 13:42:05 -07:00
|
|
|
|
|
|
|
if iniTok == recTok {
|
|
|
|
t.Fatalf("tokens should not be the same; old token %s, new token %s", iniTok, recTok)
|
|
|
|
}
|
|
|
|
|
|
|
|
//add new pkg using new token
|
|
|
|
u = fmt.Sprintf("%s/bar", 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", recTok))
|
|
|
|
resp, err = http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't POST: %v", err)
|
|
|
|
}
|
2016-06-21 07:54:00 -07:00
|
|
|
if got, want := resp.StatusCode, http.StatusOK; got != want {
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
io.Copy(buf, resp.Body)
|
|
|
|
t.Logf("%s", buf.Bytes())
|
|
|
|
t.Fatalf("bad request got incorrect status: got %d, want %d", got, want)
|
|
|
|
}
|
2016-06-03 13:42:05 -07:00
|
|
|
if got, want := len(db.Pkgs()), 1; got != want {
|
|
|
|
t.Fatalf("pkgs should have something in it; got %d, want %d", got, want)
|
|
|
|
}
|
|
|
|
}
|