Skip to content

Commit

Permalink
Merge pull request #140 from rusenask/feature/multiple_imports
Browse files Browse the repository at this point in the history
Feature/multiple imports
  • Loading branch information
Karolis Rusenas committed Mar 4, 2016
2 parents ca99b9b + 585ee19 commit 885e0d7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 66 deletions.
115 changes: 57 additions & 58 deletions authentication/jwt_backend_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package authentication

import (
"testing"
"os"
"os"
"testing"

"github.com/SpectoLabs/hoverfly/authentication/backends"
"github.com/dgrijalva/jwt-go"
)


// TestMain prepares database for testing and then performs a cleanup
func TestMain(m *testing.M) {
setup()
Expand All @@ -20,69 +19,69 @@ func TestMain(m *testing.M) {
}

func TestGenerateToken(t *testing.T) {
ab := backends.NewBoltDBAuthBackend(TestDB, []byte(backends.TokenBucketName), []byte(backends.UserBucketName))
jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
token, err := jwtBackend.GenerateToken("userUUIDhereVeryLong", "userx")
expect(t, err, nil)
expect(t, len(token) > 0, true)
ab := backends.NewBoltDBAuthBackend(TestDB, []byte(backends.TokenBucketName), []byte(backends.UserBucketName))
jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)

token, err := jwtBackend.GenerateToken("userUUIDhereVeryLong", "userx")
expect(t, err, nil)
expect(t, len(token) > 0, true)
}

func TestAuthenticate(t *testing.T) {
ab := backends.NewBoltDBAuthBackend(TestDB, []byte(backends.TokenBucketName), []byte(backends.UserBucketName))
username := []byte("beloveduser")
passw := []byte("12345")
ab.AddUser(username, passw, true)
jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
user := &backends.User{
Username: string(username),
Password: string(passw),
UUID: "uuid_here",
IsAdmin: true}
success := jwtBackend.Authenticate(user)
expect(t, success, true)
ab := backends.NewBoltDBAuthBackend(TestDB, []byte(backends.TokenBucketName), []byte(backends.UserBucketName))
username := []byte("beloveduser")
passw := []byte("12345")
ab.AddUser(username, passw, true)

jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
user := &backends.User{
Username: string(username),
Password: string(passw),
UUID: "uuid_here",
IsAdmin: true}

success := jwtBackend.Authenticate(user)
expect(t, success, true)
}

func TestAuthenticateFail(t *testing.T) {
ab := backends.NewBoltDBAuthBackend(TestDB, []byte(backends.TokenBucketName), GetRandomName(10))
jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
user := &backends.User{
Username: "shouldntbehere",
Password: "secret",
UUID: "uuid_here",
IsAdmin: true}
success := jwtBackend.Authenticate(user)
expect(t, success, false)
ab := backends.NewBoltDBAuthBackend(TestDB, []byte(backends.TokenBucketName), GetRandomName(10))

jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
user := &backends.User{
Username: "shouldntbehere",
Password: "secret",
UUID: "uuid_here",
IsAdmin: true}

success := jwtBackend.Authenticate(user)
expect(t, success, false)
}

func TestLogout(t *testing.T) {
ab := backends.NewBoltDBAuthBackend(TestDB, GetRandomName(10), GetRandomName(10))
jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
tokenString := "exampletokenstring"
token := jwt.New(jwt.SigningMethodHS512)
err := jwtBackend.Logout(tokenString, token)
expect(t, err, nil)
// checking whether token is in blacklist
blacklisted := jwtBackend.IsInBlacklist(tokenString)
expect(t, blacklisted, true)
ab := backends.NewBoltDBAuthBackend(TestDB, GetRandomName(10), GetRandomName(10))

jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)

tokenString := "exampletokenstring"
token := jwt.New(jwt.SigningMethodHS512)

err := jwtBackend.Logout(tokenString, token)
expect(t, err, nil)

// checking whether token is in blacklist

blacklisted := jwtBackend.IsInBlacklist(tokenString)
expect(t, blacklisted, true)
}

func TestNotBlacklisted(t *testing.T) {
ab := backends.NewBoltDBAuthBackend(TestDB, GetRandomName(10), GetRandomName(10))
jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)
tokenString := "exampleTokenStringThatIsNotBlacklisted"
blacklisted := jwtBackend.IsInBlacklist(tokenString)
expect(t, blacklisted, false)
}
ab := backends.NewBoltDBAuthBackend(TestDB, GetRandomName(10), GetRandomName(10))

jwtBackend := InitJWTAuthenticationBackend(ab, []byte("verysecret"), 100)

tokenString := "exampleTokenStringThatIsNotBlacklisted"

blacklisted := jwtBackend.IsInBlacklist(tokenString)
expect(t, blacklisted, false)
}
36 changes: 28 additions & 8 deletions cmd/hoverfly/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ import (
"os"
)

type arrayFlags []string

func (i *arrayFlags) String() string {
return "my string representation"
}

func (i *arrayFlags) Set(value string) error {
*i = append(*i, value)
return nil
}

var importFlags arrayFlags

func main() {
log.SetFormatter(&log.JSONFormatter{})

Expand Down Expand Up @@ -61,7 +74,7 @@ func main() {
dev := flag.Bool("dev", false, "supply -dev flag to serve directly from ./static/dist instead from statik binary")

// import flag
imp := flag.String("import", "", "import from file or from URL (i.e. '-import my_service.json' or '-import http://mypage.com/service_x.json'")
flag.Var(&importFlags, "import", "import from file or from URL (i.e. '-import my_service.json' or '-import http://mypage.com/service_x.json'")

// adding new user
addNew := flag.Bool("add", false, "add new user '-add -username hfadmin -password hfpass'")
Expand Down Expand Up @@ -182,13 +195,20 @@ func main() {
}

// importing stuff
if *imp != "" {
err := dbClient.Import(*imp)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"import": *imp,
}).Fatal("Failed to import given resource")
if len(importFlags) > 0 {
for _, v := range importFlags {
if v != "" {
log.WithFields(log.Fields{
"import": v,
}).Debug("Importing given resource")
err := dbClient.Import(v)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
"import": v,
}).Fatal("Failed to import given resource")
}
}
}
}

Expand Down

0 comments on commit 885e0d7

Please sign in to comment.