Skip to content

Commit

Permalink
Redesign test context creation (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
KirilKabakchiev authored Feb 4, 2019
1 parent 88cc4b0 commit 67dcd67
Show file tree
Hide file tree
Showing 16 changed files with 292 additions and 194 deletions.
3 changes: 1 addition & 2 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ var _ = Describe("API", func() {

BeforeSuite(func() {
server = common.NewOAuthServer()
server.Start()
})

AfterSuite(func() {
Expand All @@ -55,7 +54,7 @@ var _ = Describe("API", func() {

It("returns no error if creation is successful", func() {
_, err := api.New(context.TODO(), mockedStorage, &api.Settings{
TokenIssuerURL: server.URL,
TokenIssuerURL: server.BaseURL,
ClientID: "sm",
}, nil)
Expect(err).ShouldNot(HaveOccurred())
Expand Down
2 changes: 1 addition & 1 deletion test/auth_test/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("Service Manager Authentication", func() {
)

BeforeSuite(func() {
ctx = common.NewTestContext(nil)
ctx = common.DefaultTestContext()
})

AfterSuite(func() {
Expand Down
18 changes: 10 additions & 8 deletions test/broker_test/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ var _ = test.DescribeTestsFor(test.TestCase{
if brokerWithLabelsServer != nil {
brokerWithLabelsServer.Close()
}

ctx.Cleanup()
})

BeforeEach(func() {
brokerServer = common.NewBrokerServer()
brokerWithLabelsServer = common.NewBrokerServer()
ctx = common.NewTestContext(nil)
ctx = common.DefaultTestContext()
brokerServer.Reset()
brokerWithLabelsServer.Reset()
brokerName := "brokerName"
Expand All @@ -91,7 +93,7 @@ var _ = test.DescribeTestsFor(test.TestCase{

postBrokerRequestWithNoLabels = common.Object{
"name": brokerName,
"broker_url": brokerServer.URL,
"broker_url": brokerServer.URL(),
"description": brokerDescription,
"credentials": common.Object{
"basic": common.Object{
Expand All @@ -102,7 +104,7 @@ var _ = test.DescribeTestsFor(test.TestCase{
}
expectedBrokerResponse = common.Object{
"name": brokerName,
"broker_url": brokerServer.URL,
"broker_url": brokerServer.URL(),
"description": brokerDescription,
}

Expand All @@ -113,7 +115,7 @@ var _ = test.DescribeTestsFor(test.TestCase{

postBrokerRequestWithLabels = common.Object{
"name": brokerWithLabelsName,
"broker_url": brokerWithLabelsServer.URL,
"broker_url": brokerWithLabelsServer.URL(),
"description": brokerWithLabelsDescription,
"credentials": common.Object{
"basic": common.Object{
Expand Down Expand Up @@ -513,7 +515,7 @@ var _ = test.DescribeTestsFor(test.TestCase{
anotherBrokerServer.Password = "password"
anotherTestBroker = common.Object{
"name": "another_name",
"broker_url": anotherBrokerServer.URL,
"broker_url": anotherBrokerServer.URL(),
"description": "another_description",
"credentials": common.Object{
"basic": common.Object{
Expand Down Expand Up @@ -611,7 +613,7 @@ var _ = test.DescribeTestsFor(test.TestCase{
updatedBrokerJSON = common.Object{
"name": "updated_name",
"description": "updated_description",
"broker_url": updatedBrokerServer.URL,
"broker_url": updatedBrokerServer.URL(),
"credentials": common.Object{
"basic": common.Object{
"username": updatedBrokerServer.Username,
Expand Down Expand Up @@ -657,7 +659,7 @@ var _ = test.DescribeTestsFor(test.TestCase{
Context("when broker_url is changed and the credentials are correct", func() {
It("returns 200", func() {
updatedBrokerJSON := common.Object{
"broker_url": updatedBrokerServer.URL,
"broker_url": updatedBrokerServer.URL(),
}
updatedBrokerServer.Username = brokerServer.Username
updatedBrokerServer.Password = brokerServer.Password
Expand Down Expand Up @@ -685,7 +687,7 @@ var _ = test.DescribeTestsFor(test.TestCase{
Context("when broker_url is changed but the credentials are wrong", func() {
It("returns 400", func() {
updatedBrokerJSON := common.Object{
"broker_url": updatedBrokerServer.URL,
"broker_url": updatedBrokerServer.URL(),
}
ctx.SMWithOAuth.PATCH("/v1/service_brokers/"+brokerID).
WithJSON(updatedBrokerJSON).
Expand Down
4 changes: 4 additions & 0 deletions test/common/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ type BrokerServer struct {
router *mux.Router
}

func (b *BrokerServer) URL() string {
return b.Server.URL
}

func JSONToMap(j string) map[string]interface{} {
jsonMap := make(map[string]interface{})
if err := json.Unmarshal([]byte(j), &jsonMap); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion test/common/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (sbc *SBCatalog) AddService(service string) {
}

func (sbc *SBCatalog) AddPlanToService(plan string, serviceIndex int) {
s, err := sjson.Set(string(*sbc), fmt.Sprintf("services.%d.-1", serviceIndex), JSONToMap(plan))
s, err := sjson.Set(string(*sbc), fmt.Sprintf("services.%d.plans.-1", serviceIndex), JSONToMap(plan))
if err != nil {
panic(err)
}
Expand Down
28 changes: 27 additions & 1 deletion test/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package common

import (
"github.com/onsi/ginkgo"
"github.com/spf13/pflag"

"crypto/rand"
"crypto/rsa"
Expand Down Expand Up @@ -45,6 +46,31 @@ import (
type Object = map[string]interface{}
type Array = []interface{}

type closer interface {
Close()
}

type urler interface {
URL() string
}

type FakeServer interface {
closer
urler
}

type FlagValue struct {
pflagValue pflag.Value
}

func (f FlagValue) Set(s string) error {
return f.pflagValue.Set(s)
}

func (f FlagValue) String() string {
return f.pflagValue.String()
}

func RemoveNonNumericArgs(obj Object) Object {
return removeOnCondition(isNotNumeric, obj)
}
Expand Down Expand Up @@ -284,7 +310,7 @@ func GenerateRandomBroker() Object {
}
o = Object{
"name": UUID.String(),
"broker_url": brokerServer.URL,
"broker_url": brokerServer.URL(),
"description": UUID2.String(),
"credentials": Object{
"basic": Object{
Expand Down
15 changes: 10 additions & 5 deletions test/common/oauth_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

type OAuthServer struct {
URL string
BaseURL string

server *httptest.Server
mux *http.ServeMux
Expand All @@ -48,6 +48,7 @@ func NewOAuthServer() *OAuthServer {
os.mux.HandleFunc("/.well-known/openid-configuration", os.getOpenIDConfig)
os.mux.HandleFunc("/oauth/token", os.getToken)
os.mux.HandleFunc("/token_keys", os.getTokenKeys)
os.Start()

return os
}
Expand All @@ -57,7 +58,7 @@ func (os *OAuthServer) Start() {
panic("OAuth server already started")
}
os.server = httptest.NewServer(os.mux)
os.URL = os.server.URL
os.BaseURL = os.server.URL
}

func (os *OAuthServer) Close() {
Expand All @@ -67,11 +68,15 @@ func (os *OAuthServer) Close() {
}
}

func (os *OAuthServer) URL() string {
return os.BaseURL
}

func (os *OAuthServer) getOpenIDConfig(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write([]byte(`{
"issuer": "` + os.URL + `/oauth/token",
"jwks_uri": "` + os.URL + `/token_keys"
"issuer": "` + os.BaseURL + `/oauth/token",
"jwks_uri": "` + os.BaseURL + `/token_keys"
}`))
}

Expand All @@ -88,7 +93,7 @@ func (os *OAuthServer) CreateToken(payload map[string]interface{}) string {
if iss, ok := payload["iss"]; ok {
issuerURL = iss.(string)
} else {
issuerURL = os.URL + "/oauth/token"
issuerURL = os.BaseURL + "/oauth/token"
}
nextYear := time.Now().Add(365 * 24 * time.Hour)
token, err := jwt.Sign(os.signer, &jwt.Options{
Expand Down
Loading

0 comments on commit 67dcd67

Please sign in to comment.