Skip to content

Commit

Permalink
fix(coverage): Add more tests on pkce package
Browse files Browse the repository at this point in the history
  • Loading branch information
NORMAND, Thibault committed Jan 11, 2018
1 parent 2a908a1 commit d724a36
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkce/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pkce

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsValid(t *testing.T) {
for k, c := range []struct {
given string
valid bool
}{
{
given: "",
valid: false,
},
{ // Too short
given: "tFQvz4e1umkzHgeWAR4lGOy7mI4dbCrzPwvYlqlxpx",
valid: false,
},
{
given: "tFQvz4e1umkzHgeWAR4lGOy7mI4dbCrzPwvYlqlxpx6",
valid: true,
},
{
given: "pq6eJXUdXhEDNqwDHbFOwx5BGtYgKWXdhyMLEHsOLPBvwLhrRCyBmVWfB4x6Nkd4CXyxLAr1tnfCjs9boH4UrQeFkkoNKXLllJhKNs9mvleFWw6TUsF04WDrLA23xBod",
valid: true,
},
{ // To long
given: "pq6eJXUdXhEDNqwDHbFOwx5BGtYgKWXdhyMLEHsOLPBvwLhrRCyBmVWfB4x6Nkd4CXyxLAr1tnfCjs9boH4UrQeFkkoNKXLllJhKNs9mvleFWw6TUsF04WDrLA23xBod0",
valid: false,
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
valid := IsValid(c.given)
assert.Equal(t, c.valid, valid, fmt.Sprintf("Should be as expected, result: %t, expected: %t", valid, c.valid))
})
}
}
49 changes: 49 additions & 0 deletions pkce/verifier_plain_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package pkce

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPlainVerfier(t *testing.T) {
// Instanciate the verifier
v := &plainVerifier{}
require.NotNil(t, v)
require.Equal(t, Plain, v.String())

for k, c := range []struct {
given string
expected string
valid bool
}{
{
given: "123456",
expected: "123456",
valid: true,
},
{
given: "123456",
expected: "12345",
valid: false,
},
{
given: "12345",
expected: "123456",
valid: false,
},
{ // Useless test, but codeverifier is not size limited by the verifier
given: "",
expected: "",
valid: true,
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
valid := v.Compare(c.given, c.expected)
assert.Equal(t, c.valid, valid, fmt.Sprintf("Should be as expected, result: %t, expected: %t", valid, c.valid))
})
}

}
34 changes: 34 additions & 0 deletions pkce/verifier_s256_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pkce

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestSha256Verfier(t *testing.T) {
// Instanciate the verifier
v := &s256Verifier{}
require.NotNil(t, v)
require.Equal(t, S256, v.String())

for k, c := range []struct {
given string
expected string
valid bool
}{
{
given: "12345678",
expected: "73l8gRjwLftklgfdXT-MdiMEjJwGPVMsyVxe16iYpk8",
valid: true,
},
} {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
valid := v.Compare(c.given, c.expected)
assert.Equal(t, c.valid, valid, fmt.Sprintf("Should be as expected, result: %t, expected: %t", valid, c.valid))
})
}

}
20 changes: 20 additions & 0 deletions pkce/verifier_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pkce

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestVerifier(t *testing.T) {
require.NotNil(t, GetVerifier(Plain))
require.NotNil(t, GetVerifier(S256))
require.Nil(t, GetVerifier("invalid-verifier-method"))
}

func TestVerifierRegistration(t *testing.T) {
require.NotNil(t, GetVerifier(Plain))
require.Panics(t, func() {
RegisterVerifier(Plain, &plainVerifier{})
})
}

0 comments on commit d724a36

Please sign in to comment.