Skip to content

Commit

Permalink
Added negetive cases
Browse files Browse the repository at this point in the history
Signed-off-by: Sanskarzz <sanskar.gur@gmail.com>
  • Loading branch information
Sanskarzz authored and eddycharly committed Apr 2, 2024
1 parent 2691d22 commit cb12da4
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
55 changes: 29 additions & 26 deletions pkg/functions/functions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package functions
import (
"fmt"
"reflect"
"sort"

"testing"
)

Expand All @@ -21,6 +21,7 @@ func Test_jwt_decode(t *testing.T) {
wantErr bool
}{
{
name: "Positive case , function returns what we expected",
args: args{[]any{token, secret}},
want: map[string]interface{}{
"header": map[string]interface{}{
Expand All @@ -37,6 +38,28 @@ func Test_jwt_decode(t *testing.T) {
},
wantErr: false,
},
// Negative test case: passing incorrect arguments (invalid token)
{
name: "negative case - invalid token",
args: args{[]any{"invalid_jwt_token", secret}},
want: map[string]interface{}{
"header": nil,
"payload": nil,
"sig": nil,
},
wantErr: true,
},
// Negative test case: passing incorrect arguments (invalid secret)
{
name: "negative case - invalid secret",
args: args{[]any{token, "invalid_secret"}},
want: map[string]interface{}{
"header": nil,
"payload": nil,
"sig": nil,
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -45,35 +68,15 @@ func Test_jwt_decode(t *testing.T) {
t.Errorf("jwt_decode() error = %v, wantErr %v", err, tt.wantErr)
return
}
gotMap := got.(map[string]any)
wantSorted := sortMap(tt.want)
gotSorted := sortMap(gotMap)

fmt.Println("Got type:", gotSorted) // To check
fmt.Println("Want type:", wantSorted) // To check
if !tt.wantErr {
gotValue := reflect.ValueOf(got)
wantValue := reflect.ValueOf(tt.want)

for key, value := range wantSorted {
gotValue, exists := gotSorted[key]
if !exists || !reflect.DeepEqual(gotValue, value) {
t.Errorf("jwt_decode() = %v, want %v", gotSorted, wantSorted)
return
if !reflect.DeepEqual(gotValue.Interface(), wantValue.Interface()) {
t.Errorf("jwt_decode() = %v, want %v", gotValue.Interface(), wantValue.Interface())
}
}

})
}
}

func sortMap(m map[string]interface{}) map[string]interface{} {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)

result := make(map[string]interface{}, len(m))
for _, k := range keys {
result[k] = m[k]
}
return result
}
18 changes: 18 additions & 0 deletions pkg/scratch/scratch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestGetFormJWTToken(t *testing.T) {
wantErr bool
}{
{
name: "positive case - passing correct arguement",
args: args{[]any{"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjIyNDEwODE1MzksIm5iZiI6MTUxNDg1MTEzOSwicm9sZSI6Imd1ZXN0Iiwic3ViIjoiWVd4cFkyVT0ifQ.ja1bgvIt47393ba_WbSBm35NrUhdxM4mOVQN8iXz8lk", "c2VjcmV0"}},
want: map[string]interface{}{
"header": map[string]interface{}{
Expand All @@ -61,6 +62,14 @@ func TestGetFormJWTToken(t *testing.T) {
},
wantErr: false,
},
// Negative test case: passing incorrect arguments
{
name: "negative case - incorrect arguments",
args: args{[]any{"invalid_jwt_token", "c2VjcmV0"}},
want: nil,
// Expecting an error because of the invalid JWT token
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -87,6 +96,7 @@ func TestGetFormJWTTokenPayload(t *testing.T) {
wantErr bool
}{
{
name: "Positive case",
args: args{[]any{"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjIyNDEwODE1MzksIm5iZiI6MTUxNDg1MTEzOSwicm9sZSI6Imd1ZXN0Iiwic3ViIjoiWVd4cFkyVT0ifQ.ja1bgvIt47393ba_WbSBm35NrUhdxM4mOVQN8iXz8lk", "c2VjcmV0"}},
want: map[string]interface{}{
"exp": 2.241081539e+09,
Expand All @@ -96,6 +106,14 @@ func TestGetFormJWTTokenPayload(t *testing.T) {
},
wantErr: false,
},
// Negative test case: passing incorrect arguments
{
name: "negative case - incorrect arguments",
args: args{[]any{"invalid_jwt_token", "c2VjcmV0"}},
want: nil,
// Expecting an error because of the invalid JWT token
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit cb12da4

Please sign in to comment.