package handlers import ( "bytes" "encoding/json" "io/ioutil" "net/http" "net/http/httptest" "testing" "golang.org/x/net/context" "github.com/docker/notary/server/storage" "github.com/docker/notary/tuf/data" "github.com/docker/notary/tuf/signed" "github.com/docker/notary/tuf/testutils" "github.com/docker/notary/utils" "github.com/stretchr/testify/assert" ) func TestMainHandlerGet(t *testing.T) { hand := utils.RootHandlerFactory(nil, context.Background(), &signed.Ed25519{}) handler := hand(MainHandler) ts := httptest.NewServer(handler) defer ts.Close() _, err := http.Get(ts.URL) if err != nil { t.Fatalf("Received error on GET /: %s", err.Error()) } } func TestMainHandlerNotGet(t *testing.T) { hand := utils.RootHandlerFactory(nil, context.Background(), &signed.Ed25519{}) handler := hand(MainHandler) ts := httptest.NewServer(handler) defer ts.Close() res, err := http.Head(ts.URL) if err != nil { t.Fatalf("Received error on GET /: %s", err.Error()) } if res.StatusCode != http.StatusNotFound { t.Fatalf("Expected 404, received %d", res.StatusCode) } } func TestGetHandlerRoot(t *testing.T) { store := storage.NewMemStorage() _, repo, _ := testutils.EmptyRepo() ctx := context.Background() ctx = context.WithValue(ctx, "metaStore", store) root, err := repo.SignRoot(data.DefaultExpires("root")) rootJSON, err := json.Marshal(root) assert.NoError(t, err) store.UpdateCurrent("gun", storage.MetaUpdate{Role: "root", Version: 1, Data: rootJSON}) req := &http.Request{ Body: ioutil.NopCloser(bytes.NewBuffer(nil)), } vars := map[string]string{ "imageName": "gun", "tufRole": "root", } rw := httptest.NewRecorder() err = getHandler(ctx, rw, req, vars) assert.NoError(t, err) } func TestGetHandlerTimestamp(t *testing.T) { store := storage.NewMemStorage() _, repo, crypto := testutils.EmptyRepo() ctx := context.Background() ctx = context.WithValue(ctx, "metaStore", store) ctx = context.WithValue(ctx, "cryptoService", crypto) sn, err := repo.SignSnapshot(data.DefaultExpires("snapshot")) snJSON, err := json.Marshal(sn) assert.NoError(t, err) store.UpdateCurrent("gun", storage.MetaUpdate{Role: "snapshot", Version: 1, Data: snJSON}) ts, err := repo.SignTimestamp(data.DefaultExpires("timestamp")) tsJSON, err := json.Marshal(ts) assert.NoError(t, err) store.UpdateCurrent("gun", storage.MetaUpdate{Role: "timestamp", Version: 1, Data: tsJSON}) req := &http.Request{ Body: ioutil.NopCloser(bytes.NewBuffer(nil)), } vars := map[string]string{ "imageName": "gun", "tufRole": "timestamp", } rw := httptest.NewRecorder() err = getHandler(ctx, rw, req, vars) assert.NoError(t, err) } func TestGetHandlerSnapshot(t *testing.T) { store := storage.NewMemStorage() _, repo, crypto := testutils.EmptyRepo() ctx := context.Background() ctx = context.WithValue(ctx, "metaStore", store) ctx = context.WithValue(ctx, "cryptoService", crypto) sn, err := repo.SignSnapshot(data.DefaultExpires("snapshot")) snJSON, err := json.Marshal(sn) assert.NoError(t, err) store.UpdateCurrent("gun", storage.MetaUpdate{Role: "snapshot", Version: 1, Data: snJSON}) req := &http.Request{ Body: ioutil.NopCloser(bytes.NewBuffer(nil)), } vars := map[string]string{ "imageName": "gun", "tufRole": "snapshot", } rw := httptest.NewRecorder() err = getHandler(ctx, rw, req, vars) assert.NoError(t, err) } func TestGetHandler404(t *testing.T) { store := storage.NewMemStorage() ctx := context.Background() ctx = context.WithValue(ctx, "metaStore", store) req := &http.Request{ Body: ioutil.NopCloser(bytes.NewBuffer(nil)), } vars := map[string]string{ "imageName": "gun", "tufRole": "root", } rw := httptest.NewRecorder() err := getHandler(ctx, rw, req, vars) assert.Error(t, err) } func TestGetHandlerNilData(t *testing.T) { store := storage.NewMemStorage() store.UpdateCurrent("gun", storage.MetaUpdate{Role: "root", Version: 1, Data: nil}) ctx := context.Background() ctx = context.WithValue(ctx, "metaStore", store) req := &http.Request{ Body: ioutil.NopCloser(bytes.NewBuffer(nil)), } vars := map[string]string{ "imageName": "gun", "tufRole": "root", } rw := httptest.NewRecorder() err := getHandler(ctx, rw, req, vars) assert.Error(t, err) } func TestGetHandlerNoStorage(t *testing.T) { ctx := context.Background() req := &http.Request{ Body: ioutil.NopCloser(bytes.NewBuffer(nil)), } err := GetHandler(ctx, nil, req) assert.Error(t, err) }