Skip to content

Commit

Permalink
[Test_case][add_new] : Test authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaskarhc committed Aug 25, 2020
1 parent 3e26640 commit 332c009
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: test install

install:
go get -t -v ./...

test: install
go test -race -cover ./...
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/mayadata-io/kubera-api-testing

go 1.12

require github.com/go-resty/resty/v2 v2.3.0
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/go-resty/resty/v2 v2.3.0 h1:JOOeAvjSlapTT92p8xiS19Zxev1neGikoHsXJeOq8So=
github.com/go-resty/resty/v2 v2.3.0/go.mod h1:UpN9CgLZNsv4e9XG50UU8xdI0F43UQ4HmxLBDwaroHU=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120 h1:EZ3cVSzKOlJxAd8e8YAJ7no8nNypTxexh/YE/xW3ZEY=
golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
111 changes: 111 additions & 0 deletions kubera/authentication/authentication.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package authentication

import (
"encoding/json"
"fmt"

"github.com/go-resty/resty/v2"
conn "github.com/mayadata-io/kubera-api-testing/kubera/connection"
)

type Token struct {
JWT string `json:"jwt"`
AccountID string `json:"accountId"`
}

func (t Token) String() string {
raw, err := json.MarshalIndent(
t,
" ",
".",
)
if err != nil {
panic(err)
}
return string(raw)
}

type APIError struct {
Err error `json:"err"`
Message string `json:"message"`
StatusCode int `json:"statusCode"`
Status string `json:"status"`
}

func (e *APIError) Error() string {
raw, err := json.MarshalIndent(
e,
" ",
".",
)
if err != nil {
panic(err)
}
return string(raw)
}

type KuberaError struct {
Message string `json:"message"`
}
type TokenFetching struct {
conn.Connection `json:"connection"`
}

func (t TokenFetching) String() string {
if t.Password != "" {
t.Password = "XXXX"
}
raw, err := json.MarshalIndent(
t,
" ",
".",
)
if err != nil {
panic(err)
}
return string(raw)
}

type TokenFetchingConfig struct {
Connection conn.Connection
}

// NewTokenFetcher returns a new instance of TokenFetching
func NewTokenFetcher(config TokenFetchingConfig) *TokenFetching {
return &TokenFetching{
Connection: config.Connection,
}
}

// Fetch returns authenticated token
func (f *TokenFetching) Fetch() (Token, error) {
client := resty.New()
url := f.HostName + "v3/token"
token := Token{}
kErr := KuberaError{}
bodyData := map[string]interface{}{
"code": fmt.Sprintf("%s:%s", f.UserName, f.Password),
"authProvider": "localAuthConfig",
}
resp, err := client.R().
SetHeader("Content-Type", "application/json").
SetBody(bodyData).
//SetError method automatic unmarshalling if response status code >= 399
// and content type either JSON or XML.
SetError(&kErr).
//SetResult automatic unmarshalling for the request,
// if response status code is between 200 and 299
SetResult(&token).
Post(url)

if err != nil || kErr.Message != "" || !resp.IsSuccess() {
return Token{}, &APIError{
Err: err,
Message: kErr.Message,
StatusCode: resp.StatusCode(),
Status: resp.Status(),
}
}

return token, nil
}
77 changes: 77 additions & 0 deletions kubera/authentication/authentication_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package authentication

import (
"testing"

conn "github.com/mayadata-io/kubera-api-testing/kubera/connection"
)

func TestFetchToken(t *testing.T) {
connection := conn.Get()
var tests = map[string]struct {
Hostname string
Username string
Password string
IsError bool
}{
"No host": {
Username: connection.UserName,
Password: connection.Password,
IsError: true,
},
"No username": {
Hostname: connection.HostName,
Password: connection.Password,
IsError: true,
},
"No password": {
Hostname: connection.HostName,
Username: connection.UserName,
IsError: true,
},
"With host, username & password": {
Hostname: connection.HostName,
Username: connection.UserName,
Password: connection.Password,
},
"With host, username & invalid password": {
Hostname: connection.HostName,
Username: connection.UserName,
Password: "invalid",
IsError: true,
},
"With invalid host, username & password": {
Hostname: "invalid",
Username: connection.UserName,
Password: connection.Password,
IsError: true,
},
"With host, invalid username & password": {
Hostname: connection.HostName,
Username: "invalid",
Password: connection.Password,
IsError: true,
},
}
for name, mock := range tests {
name := name // Pin it
mock := mock // Pin it
t.Run(name, func(t *testing.T) {
t.Parallel()
fetcher := NewTokenFetcher(TokenFetchingConfig{
Connection: conn.Connection{
HostName: mock.Hostname,
UserName: mock.Username,
Password: mock.Password,
},
})
tkn, err := fetcher.Fetch()
if !mock.IsError && err != nil {
t.Fatalf("Expected no error got=%s: \nFetcher=%s: \nToken=%s", err, fetcher, tkn)
}
if mock.IsError && err == nil {
t.Fatalf("Expected error got none: \nFetcher=%s: \nToken=%s", fetcher, tkn)
}
})
}
}
56 changes: 56 additions & 0 deletions kubera/connection/connection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package connection

import (
"encoding/json"
"os"
"sync"
)

const (
kuberaHost = "KUBERA_HOST" //KUBERA_HOST is Env variable contains value of Kubera's Domain/IP
kuberaUser = "KUBERA_USER" // Kubera Username
kuberaPass = "KUBERA_PASS" //Kubera password
)

// Connection defines the structure of userCerdentials
type Connection struct {
HostName string
UserName string
Password string
}

func (c Connection) String() string {
if c.Password != "" {
c.Password = "XXXX"
}
raw, err := json.MarshalIndent(
c,
" ",
".",
)
if err != nil {
panic(err)
}
return string(raw)
}

var connection Connection

// Get return the credentials of kubera user ,pass and host
func Get() Connection {

if connection.HostName != "" {
return connection
}
var once sync.Once

once.Do(func() {
connection = Connection{
HostName: os.Getenv(kuberaHost),
UserName: os.Getenv(kuberaUser),
Password: os.Getenv(kuberaPass),
}
})
return connection

}

0 comments on commit 332c009

Please sign in to comment.