Skip to content

Commit

Permalink
Merge pull request #243 from linkernetworks/johnlin/registry-auth-test
Browse files Browse the repository at this point in the history
[Task] registry auth test
  • Loading branch information
John-Lin authored Aug 13, 2018
2 parents 2a394bc + 1f99775 commit 08c9579
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions src/server/handler_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package server

import (
"encoding/json"
"math/rand"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"

restful "github.com/emicklei/go-restful"
"github.com/linkernetworks/mongo"
"github.com/linkernetworks/vortex/src/config"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/serviceprovider"
"github.com/stretchr/testify/suite"
)

func init() {
rand.Seed(time.Now().UnixNano())
}

type RegistryTestSuite struct {
suite.Suite
sp *serviceprovider.Container
wc *restful.Container
session *mongo.Session
}

func (suite *RegistryTestSuite) SetupSuite() {
cf := config.MustRead("../../config/testing.json")
sp := serviceprovider.NewForTesting(cf)

suite.sp = sp
// init session
suite.session = sp.Mongo.NewSession()
// init restful container
suite.wc = restful.NewContainer()
service := newRegistryService(suite.sp)
suite.wc.Add(service)
}

func (suite *RegistryTestSuite) TearDownSuite() {}

func TestRegistrySuite(t *testing.T) {
suite.Run(t, new(RegistryTestSuite))
}

func (suite *RegistryTestSuite) TestRegistryBasicAuth() {
cred := entity.RegistryBasicAuthCredential{
Username: "test",
Password: "test",
}

bodyBytes, err := json.MarshalIndent(cred, "", " ")
suite.NoError(err)

bodyReader := strings.NewReader(string(bodyBytes))
httpRequest, err := http.NewRequest("POST", "http://localhost:7890/v1/registry/auth", bodyReader)
suite.NoError(err)

httpRequest.Header.Add("Content-Type", "application/json")
httpWriter := httptest.NewRecorder()
suite.wc.Dispatch(httpWriter, httpRequest)
assertResponseCode(suite.T(), http.StatusUnauthorized, httpWriter)
}

0 comments on commit 08c9579

Please sign in to comment.