package vain import ( "bytes" "encoding/json" "fmt" "io" "io/ioutil" "net/http" "net/http/httptest" "net/url" "strings" "testing" "time" ) const window = 5 * time.Minute func TestAdd(t *testing.T) { db, done := TestDB(t) if db == nil { t.Fatalf("could not create temp db") } defer done() sm := http.NewServeMux() NewServer(sm, db, nil, "", window, false) ts := httptest.NewServer(sm) tok, err := db.addUser("sm@example.org") if err != nil { t.Errorf("failure to add user: %v", err) } resp, err := http.Get(ts.URL) if err != nil { t.Fatalf("couldn't GET: %v", err) } resp.Body.Close() if got, want := len(db.Pkgs()), 0; got != want { t.Fatalf("started with something in it; got %d, want %d", got, want) } { 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 { t.Fatalf("couldn't POST: %v", err) } if got, want := resp.StatusCode, http.StatusBadRequest; 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) } resp.Body.Close() if got, want := len(db.Pkgs()), 0; got != want { t.Fatalf("started with something in it; got %d, want %d", got, want) } } { u := fmt.Sprintf("%s/%s", ts.URL, prefix["pkgs"]) 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 { t.Fatalf("problem parsing json: %v, \n%q", err, buf) } if got, want := len(pkgs), 0; got != want { t.Fatalf("should have empty pkg list; got %d, want %d", got, want) } } { 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("problem performing request: %v", err) } buf := &bytes.Buffer{} io.Copy(buf, resp.Body) t.Logf("%v", buf) resp.Body.Close() if got, want := len(db.Pkgs()), 1; got != want { t.Fatalf("pkgs should have something in it; got %d, want %d", got, want) } 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) if !db.PackageExists(path(good)) { 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 { t.Fatalf("package name did not go through as expected; got %q, want %q", got, want) } if got, want := p.Repo, "https://s.mcquay.me/sm/vain"; got != want { t.Fatalf("repo did not go through as expected; got %q, want %q", got, want) } if got, want := p.Vcs, "git"; got != want { t.Fatalf("Vcs did not go through as expected; got %q, want %q", got, want) } } resp, err = http.Get(ts.URL + "?go-get=1") if err != nil { t.Fatalf("couldn't GET: %v", err) } defer resp.Body.Close() if want := http.StatusOK; resp.StatusCode != want { t.Fatalf("Should have succeeded to fetch /; got %s, want %s", resp.Status, http.StatusText(want)) } buf := &bytes.Buffer{} if _, err := io.Copy(buf, resp.Body); err != nil { t.Fatalf("couldn't read content from server: %v", err) } if got, want := strings.Count(buf.String(), "