Skip to content

Commit

Permalink
fix(get): Get returns 404 if dataset is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Feb 20, 2019
1 parent fa71775 commit 291296b
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 2 deletions.
12 changes: 10 additions & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func init() {
}

func newTestRepo(t *testing.T) (r repo.Repo, teardown func()) {
return newTestRepoWithNumDatasets(t, 0)
}

func newTestRepoWithNumDatasets(t *testing.T, num int) (r repo.Repo, teardown func()) {
var err error
if err = confirmQriNotRunning(); err != nil {
t.Fatal(err.Error())
Expand All @@ -40,7 +44,7 @@ func newTestRepo(t *testing.T) (r repo.Repo, teardown func()) {
golog.SetLogLevel("qriapi", "error")

// use a test registry server (with a pinset) & client & client
rc, registryServer := regmock.NewMockServer()
rc, registryServer := regmock.NewMockServerWithNumDatasets(num)
// to keep hashes consistent, artificially specify the timestamp by overriding
// the dsfs.Timestamp func
prevTs := dsfs.Timestamp
Expand Down Expand Up @@ -74,8 +78,12 @@ func newTestRepo(t *testing.T) (r repo.Repo, teardown func()) {
}

func newTestNode(t *testing.T) (node *p2p.QriNode, teardown func()) {
return newTestNodeWithNumDatasets(t, 0)
}

func newTestNodeWithNumDatasets(t *testing.T, num int) (node *p2p.QriNode, teardown func()) {
var r repo.Repo
r, teardown = newTestRepo(t)
r, teardown = newTestRepoWithNumDatasets(t, num)
node, err := p2p.NewQriNode(r, config.DefaultP2PForTesting())
if err != nil {
t.Fatal(err.Error())
Expand Down
6 changes: 6 additions & 0 deletions api/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"fmt"
"net/http"
"strings"

util "github.com/datatogether/api/apiutil"
"github.com/qri-io/qri/lib"
Expand Down Expand Up @@ -131,6 +132,11 @@ func (h *RegistryHandlers) registryDatasetHandler(w http.ResponseWriter, r *http

err = h.RegistryRequests.GetDataset(&ref, res)
if err != nil {
// If error was that the dataset wasn't found, send back 404 Not Found.
if strings.HasPrefix(err.Error(), "error 404") {
util.NotFoundHandler(w, r)
return
}
util.WriteErrResponse(w, http.StatusInternalServerError, err)
return
}
Expand Down
50 changes: 50 additions & 0 deletions api/registry_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package api

import (
"io/ioutil"
"net/http/httptest"
"strings"

"testing"
)

Expand All @@ -23,3 +27,49 @@ func TestRegistryHandlers(t *testing.T) {
}
runHandlerTestCases(t, "registryDatasets", h.RegistryDatasetsHandler, registryDatasetsCases)
}


func TestRegistryGet(t *testing.T) {
node, teardown := newTestNodeWithNumDatasets(t, 1)
defer teardown()
h := NewRegistryHandlers(node)

req := httptest.NewRequest("GET", "/registry/me/ds_0", strings.NewReader(""))
w := httptest.NewRecorder()
h.RegistryDatasetHandler(w, req)
res := w.Result()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf(err.Error())
}
got := string(body)

expect := `{"data":{"peername":"peer","name":"ds_0","path":"QmAbC0","dataset":{"name":"ds_0","path":"QmAbC0","peername":"peer","qri":"ds:0"},"published":true},"meta":{"code":200}}`
if got != expect {
t.Errorf("did not match, got:\n%s\nexpect:\n%s\n", got, expect)
}
}


func TestRegistryGetNotFound(t *testing.T) {
node, teardown := newTestNodeWithNumDatasets(t, 1)
defer teardown()
h := NewRegistryHandlers(node)

req := httptest.NewRequest("GET", "/registry/me/not_found", strings.NewReader(""))
w := httptest.NewRecorder()
h.RegistryDatasetHandler(w, req)
res := w.Result()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
t.Fatalf(err.Error())
}
got := string(body)

expect := `{ "meta": { "code": 404, "status": "not found" }, "data": null }`
if got != expect {
t.Errorf("did not match, got:\n%s\nexpect:\n%s\n", got, expect)
}
}

0 comments on commit 291296b

Please sign in to comment.