Skip to content

Commit

Permalink
tests: add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
primalmotion committed Dec 13, 2018
1 parent 100a526 commit 2534876
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 64 deletions.
8 changes: 1 addition & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,11 @@ func newContext(ctx context.Context, request *elemental.Request) *bcontext {
}

func (c *bcontext) Identifier() string {

return c.id
}

func (c *bcontext) Context() context.Context {

if c.ctx != nil {
return c.ctx
}

return context.Background()
return c.ctx
}

func (c *bcontext) Request() *elemental.Request {
Expand Down
2 changes: 1 addition & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

. "github.com/smartystreets/goconvey/convey"
"go.aporeto.io/elemental"
"go.aporeto.io/elemental/test/model"
testmodel "go.aporeto.io/elemental/test/model"
)

func TestContext_NewContext(t *testing.T) {
Expand Down
8 changes: 2 additions & 6 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"net/http"

"github.com/opentracing/opentracing-go"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/log"
"go.aporeto.io/elemental"
"go.uber.org/zap"
Expand Down Expand Up @@ -86,13 +86,9 @@ func makeErrorResponse(ctx context.Context, response *elemental.Response, err er

outError := processError(ctx, err)

if response == nil {
response = elemental.NewResponse(nil)
}

response.StatusCode = outError.Code()
if e := response.Encode(outError); e != nil {
zap.L().Panic("Unable to encode error", zap.Error(err))
zap.L().Panic("Unable to encode error", zap.Error(e))
}

return response
Expand Down
2 changes: 1 addition & 1 deletion handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

. "github.com/smartystreets/goconvey/convey"
"go.aporeto.io/elemental"
"go.aporeto.io/elemental/test/model"
testmodel "go.aporeto.io/elemental/test/model"
)

func TestHandlers_makeResponse(t *testing.T) {
Expand Down
13 changes: 9 additions & 4 deletions opentracing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package bahamut
import (
"bytes"
"context"
"fmt"
"net/http"
"testing"

opentracing "github.com/opentracing/opentracing-go"
. "github.com/smartystreets/goconvey/convey"
"go.aporeto.io/elemental"
"go.aporeto.io/elemental/test/model"
testmodel "go.aporeto.io/elemental/test/model"
)

func TestTracing_extractClaims(t *testing.T) {
Expand Down Expand Up @@ -182,6 +181,7 @@ func TestTracing_traceRequest(t *testing.T) {
}
hreq.Header.Add("Authorization", "secretA")
hreq.Header.Add("Authorization", "secretB")
hreq.Header.Add("NotAuthorization", "notSecretA")

req, err := elemental.NewRequestFromHTTPRequest(hreq, testmodel.Manager())
if err != nil {
Expand All @@ -191,8 +191,10 @@ func TestTracing_traceRequest(t *testing.T) {
req.ExternalTrackingType = "yeah"
req.ClientIP = "127.0.0.1"
req.Namespace = "/a"
req.ObjectID = "id"
// Add the param after calling NewRequestFromHTTPRequest as this is not valid params from specs.
req.Parameters["token"] = elemental.NewParameter(elemental.ParameterTypeString, "1", "2")
req.Parameters["not-token"] = elemental.NewParameter(elemental.ParameterTypeString, "notSecretB")

tracer := &mockTracer{}
ts := newMockSpan(tracer)
Expand Down Expand Up @@ -229,18 +231,20 @@ func TestTracing_traceRequest(t *testing.T) {
So(len(span.fields), ShouldEqual, 8)
So(span.fields[0].String(), ShouldEqual, "req.page.number:3")
So(span.fields[1].String(), ShouldEqual, "req.page.size:30")
So(span.fields[2].String(), ShouldEqual, fmt.Sprintf("req.headers:map[Authorization:%s]", snipSlice))
So(span.fields[2].String(), ShouldContainSubstring, "Notauthorization:[notSecretA]")
So(span.fields[2].String(), ShouldContainSubstring, "Authorization:[[snip]]")
So(span.fields[3].String(), ShouldEqual, "req.claims:{}")
So(span.fields[4].String(), ShouldEqual, "req.client_ip:127.0.0.1")
So(span.fields[5].String(), ShouldNotContainSubstring, "secretA")
So(span.fields[5].String(), ShouldNotContainSubstring, "secretB")
So(span.fields[5].String(), ShouldContainSubstring, "[[snip]]")
So(span.fields[5].String(), ShouldContainSubstring, "notSecretB")
So(span.fields[6].String(), ShouldEqual, "req.order_by:[a b]")
So(span.fields[7].String(), ShouldEqual, "req.payload:the data")
})

Convey("Then the span tags should be correct", func() {
So(len(span.tags), ShouldEqual, 11)
So(len(span.tags), ShouldEqual, 12)
So(span.tags["req.parent.identity"], ShouldEqual, "task")
So(span.tags["req.id"], ShouldEqual, req.RequestID)
So(span.tags["req.recursive"], ShouldBeTrue)
Expand All @@ -252,6 +256,7 @@ func TestTracing_traceRequest(t *testing.T) {
So(span.tags["req.operation"], ShouldEqual, "create")
So(span.tags["req.override_protection"], ShouldBeTrue)
So(span.tags["req.parent.id"], ShouldEqual, "pid")
So(span.tags["req.object.id"], ShouldEqual, "id")
})
})
})
Expand Down
3 changes: 1 addition & 2 deletions options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

. "github.com/smartystreets/goconvey/convey"
"go.aporeto.io/elemental"
"go.aporeto.io/elemental/test/model"
testmodel "go.aporeto.io/elemental/test/model"
"golang.org/x/time/rate"
)

Expand Down Expand Up @@ -242,5 +242,4 @@ func TestBahamut_Options(t *testing.T) {
OptPreStopHook(f)(&c)
So(c.hooks.preStop, ShouldEqual, f)
})

}
16 changes: 16 additions & 0 deletions processor_helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,21 @@ func TestProcessorHelpers_checkAuthorized(t *testing.T) {
So(err, ShouldNotBeNil)
})
})

Convey("When I check the authorization with two registered authorizers, first one continue, second one continue", func() {

auth.action = AuthActionContinue
auth.errored = false

auth2 := &mockAuth{}
auth2.action = AuthActionContinue
auth2.errored = false

err := CheckAuthorization([]Authorizer{auth, auth2}, ctx)

Convey("Then it should not be authenticated", func() {
So(err, ShouldBeNil)
})
})
})
}
16 changes: 9 additions & 7 deletions pubsub_nats_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"crypto/tls"
"fmt"

"github.com/nats-io/go-nats"
nats "github.com/nats-io/go-nats"
)

// A NATSOption represents an option to the pubsub backed by nats
Expand Down Expand Up @@ -104,12 +104,14 @@ func NATSOptPublishReplyValidator(ctx context.Context, validator func(msg *nats.
// the option NATSOptPublishReplyValidator.
func NATSOptPublishRequireAck(ctx context.Context) PubSubOptPublish {
return func(c interface{}) {
c.(*natsPublishConfig).replyValidator = func(msg *nats.Msg) error {
if !bytes.Equal(msg.Data, ackMessage) {
return fmt.Errorf("invalid ack: %s", string(msg.Data))
}
return nil
}
c.(*natsPublishConfig).replyValidator = ackValidator
c.(*natsPublishConfig).ctx = ctx
}
}

func ackValidator(msg *nats.Msg) error {
if !bytes.Equal(msg.Data, ackMessage) {
return fmt.Errorf("invalid ack: %s", string(msg.Data))
}
return nil
}
72 changes: 72 additions & 0 deletions pubsub_nats_options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package bahamut

import (
"context"
"crypto/tls"
"testing"

nats "github.com/nats-io/go-nats"
. "github.com/smartystreets/goconvey/convey"
)

func TestBahamut_NATSOption(t *testing.T) {

n := &natsPubSub{}

Convey("Calling NATSOptCredentials should work", t, func() {
NATSOptCredentials("user", "pass")(n)
So(n.username, ShouldEqual, "user")
So(n.password, ShouldEqual, "pass")
})

Convey("Calling NATSOptClusterID should work", t, func() {
NATSOptClusterID("cid")(n)
So(n.clusterID, ShouldEqual, "cid")
})

Convey("Calling NATSOptClientID should work", t, func() {
NATSOptClientID("cid")(n)
So(n.clientID, ShouldEqual, "cid")
})

Convey("Calling NATSOptTLS should work", t, func() {
tlscfg := &tls.Config{}
NATSOptTLS(tlscfg)(n)
So(n.tlsConfig, ShouldEqual, tlscfg)
})
}

func TestBahamut_PubSubNatsOptionsSubscribe(t *testing.T) {

c := natsSubscribeConfig{}

Convey("Calling NATSOptSubscribeQueue should work", t, func() {
NATSOptSubscribeQueue("queueGroup")(&c)
So(c.queueGroup, ShouldEqual, "queueGroup")
})

Convey("Calling NATSOptSubscribeReplyer should work", t, func() {
r := func(msg *nats.Msg) []byte { return nil }
NATSOptSubscribeReplyer(r)(&c)
So(c.replier, ShouldEqual, r)
})
}

func TestBahamut_PubSubNatsOptionsPublish(t *testing.T) {

c := natsPublishConfig{}

Convey("Calling NATSOptPublishReplyValidator should work", t, func() {
v := func(msg *nats.Msg) error { return nil }
NATSOptPublishReplyValidator(context.TODO(), v)(&c)
So(c.ctx, ShouldEqual, context.TODO())
So(c.replyValidator, ShouldEqual, v)
})

Convey("Calling NATSOptPublishRequireAck should work", t, func() {
NATSOptPublishRequireAck(context.TODO())(&c)
So(c.ctx, ShouldEqual, context.TODO())
So(c.replyValidator(&nats.Msg{Data: ackMessage}), ShouldEqual, nil)
So(c.replyValidator(&nats.Msg{Data: []byte("hello")}).Error(), ShouldEqual, "invalid ack: hello")
})
}
43 changes: 43 additions & 0 deletions pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package bahamut

import (
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
)
Expand All @@ -17,3 +18,45 @@ func TestPubsub_NewServer(t *testing.T) {
})
})
}

func TestPubSub_connectionWaiter(t *testing.T) {

Convey("Given I have a waiter", t, func() {

w := connectionWaiter{
abort: make(chan struct{}),
ok: make(chan bool),
}

Convey("When I call Wait and it returns true", func() {

go func() { w.ok <- true }()

ok := w.Wait(10 * time.Second)

Convey("Then ok should be true", func() {
So(ok, ShouldBeTrue)
})
})

Convey("When I call Wait and it returns false", func() {

go func() { w.ok <- false }()

ok := w.Wait(10 * time.Second)

Convey("Then ok should be true", func() {
So(ok, ShouldBeFalse)
})
})

Convey("When I call Wait but it timeouts", func() {

ok := w.Wait(300 * time.Millisecond)

Convey("Then ok should be true", func() {
So(ok, ShouldBeFalse)
})
})
})
}
Loading

0 comments on commit 2534876

Please sign in to comment.