Skip to content
This repository has been archived by the owner on Nov 24, 2023. It is now read-only.

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
Updated README.md
  • Loading branch information
dpattmann committed Sep 28, 2021
1 parent 4dc001a commit 812863c
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 7 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# What is Furby?

Furby is an easy and simple to use OAuth2 Token cache.
Furby is an easy and simple to use OAuth2 Token cache. Run Furby in your microservice
infrastructure to move the OAuth2.0 Token management out of your services.

## Build

```bash
go build cmd/furby/furby.go
```

## Configuration

Expand All @@ -18,8 +25,8 @@ Furby is an easy and simple to use OAuth2 Token cache.
```bash
go test ./...
```
### Generate mocks

```bash
# Generate mock interfaces
mockery --all
```
51 changes: 51 additions & 0 deletions auth/noop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package auth

import (
"net/http"
"reflect"
"testing"
)

func TestNewNoOpAuthorizer(t *testing.T) {
tests := []struct {
name string
want *NoOp
}{
{
"Empty NoOp Authorizer",
&NoOp{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewNoOpAuthorizer(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewNoOpAuthorizer() = %v, want %v", got, tt.want)
}
})
}
}

func TestNoOp_IsAuthorized(t *testing.T) {
type args struct {
in0 *http.Request
}
tests := []struct {
name string
args args
want bool
}{
{
"Return always true",
args{in0: nil},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
a := NoOp{}
if got := a.IsAuthorized(tt.args.in0); got != tt.want {
t.Errorf("IsAuthorized() = %v, want %v", got, tt.want)
}
})
}
}
10 changes: 8 additions & 2 deletions auth/user-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ type UserAgentAuthorizer struct {
}

func NewUserAgentAuthorizer(userAgents []string) *UserAgentAuthorizer {
var lowerCaseUserAgens []string

for _, ua := range userAgents {
lowerCaseUserAgens = append(lowerCaseUserAgens, strings.ToLower(ua))
}

return &UserAgentAuthorizer{
userAgents: userAgents,
userAgents: lowerCaseUserAgens,
}
}

Expand All @@ -23,7 +29,7 @@ func (u UserAgentAuthorizer) IsAuthorized(r *http.Request) bool {
}

for _, ua := range u.userAgents {
if strings.Contains(userAgentHeader, strings.ToLower(ua)) {
if strings.Contains(userAgentHeader, ua) {
return true
}
}
Expand Down
31 changes: 28 additions & 3 deletions auth/user-agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package auth
import (
"net/http"
"net/http/httptest"
"reflect"
"testing"
)

Expand Down Expand Up @@ -30,19 +31,19 @@ func TestUserAgentAuthorizer_IsAuthorized(t *testing.T) {
},
{
name: "Find no UserAgent",
fields: fields{userAgents: []string{"Chrome"}},
fields: fields{userAgents: []string{"chrome"}},
args: args{r: req},
want: false,
},
{
name: "Find one UserAgent",
fields: fields{userAgents: []string{"Chrome", "Firefox"}},
fields: fields{userAgents: []string{"edge", "firefox"}},
args: args{r: req},
want: true,
},
{
name: "Find without UserAgent header",
fields: fields{userAgents: []string{"Chrome", "Firefox"}},
fields: fields{userAgents: []string{"chrome", "firefox"}},
args: args{r: &http.Request{}},
want: false,
},
Expand All @@ -58,3 +59,27 @@ func TestUserAgentAuthorizer_IsAuthorized(t *testing.T) {
})
}
}

func TestNewUserAgentAuthorizer(t *testing.T) {
type args struct {
userAgents []string
}
tests := []struct {
name string
args args
want *UserAgentAuthorizer
}{
{
name: "Generate new user agent authorizer",
args: args{userAgents: []string{"Firefox", "Chrome"}},
want: &UserAgentAuthorizer{userAgents: []string{"firefox", "chrome"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewUserAgentAuthorizer(tt.args.userAgents); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewUserAgentAuthorizer() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 812863c

Please sign in to comment.