Skip to content

Commit

Permalink
feat: improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
1995parham committed Nov 11, 2023
1 parent 325b788 commit 79c67e2
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 50 deletions.
65 changes: 65 additions & 0 deletions internal/authenticator/admin_authenticator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package authenticator_test

import (
"testing"

"github.com/golang-jwt/jwt/v5"
"github.com/snapp-incubator/soteria/internal/authenticator"
"github.com/snapp-incubator/soteria/internal/config"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)

type AdminAuthenticatorTestSuite struct {
suite.Suite

AdminToken string

Authenticator authenticator.Authenticator
}

func TestAdminAuthenticator_suite(t *testing.T) {
t.Parallel()

st := new(AdminAuthenticatorTestSuite)

pkey0, err := getPublicKey("admin")
require.NoError(t, err)

st.Authenticator = authenticator.AdminAuthenticator{
Key: pkey0,
Company: "snapp-admin",
Parser: jwt.NewParser(),
JwtConfig: config.Jwt{
IssName: "iss",
SubName: "sub",
SigningMethod: "rsa256",
},
}

suite.Run(t, st)
}

func (suite *AdminAuthenticatorTestSuite) SetupSuite() {
require := suite.Require()

key, err := getPrivateKey("admin")
require.NoError(err)

adminToken, err := getSampleToken("admin", key)
require.NoError(err)

suite.AdminToken = adminToken
}

func (suite *AdminAuthenticatorTestSuite) TestAuth() {
require := suite.Require()

suite.Run("testing admin token auth", func() {
require.NoError(suite.Authenticator.Auth(suite.AdminToken))
})

suite.Run("testing invalid token auth", func() {
require.Error(suite.Authenticator.Auth(invalidToken))
})
}
77 changes: 32 additions & 45 deletions internal/authenticator/authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ import (

"github.com/golang-jwt/jwt/v5"
"github.com/snapp-incubator/soteria/internal/authenticator"
"github.com/snapp-incubator/soteria/internal/config"
"github.com/snapp-incubator/soteria/internal/topics"
"github.com/snapp-incubator/soteria/pkg/acl"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -83,23 +79,33 @@ type AuthenticatorTestSuite struct {
func (suite *AuthenticatorTestSuite) SetupSuite() {
require := suite.Require()

driverToken := suite.getSampleToken(topics.DriverIss)
pkey0, err := getPublicKey("0")
require.NoError(err)

suite.Tokens.Driver = driverToken
suite.PublicKeys.Driver = pkey0

passengerToken := suite.getSampleToken(topics.PassengerIss)
pkey1, err := getPublicKey("1")
require.NoError(err)

suite.Tokens.Passenger = passengerToken
suite.PublicKeys.Passenger = pkey1

pkey0, err := suite.getPublicKey(topics.DriverIss)
key0, err := getPrivateKey("0")
require.NoError(err)

suite.PublicKeys.Driver = pkey0

pkey1, err := suite.getPublicKey(topics.PassengerIss)
key1, err := getPrivateKey("1")
require.NoError(err)

suite.PublicKeys.Passenger = pkey1
driverToken, err := getSampleToken("0", key0)
require.NoError(err)

suite.Tokens.Driver = driverToken

passengerToken, err := getSampleToken("1", key1)
require.NoError(err)

suite.Tokens.Passenger = passengerToken
}

func (suite *AuthenticatorTestSuite) TestAuth() {
Expand Down Expand Up @@ -317,28 +323,6 @@ func (suite *AuthenticatorTestSuite) TestACL_Driver() {
})
}

func TestManualAuthenticator_ValidateTopicBySender(t *testing.T) {
t.Parallel()

cfg := config.SnappVendor()

hid, err := topics.NewHashIDManager(cfg.HashIDMap)
require.NoError(t, err)

// nolint: exhaustruct
authenticator := authenticator.ManualAuthenticator{
AllowedAccessTypes: []acl.AccessType{acl.Pub, acl.Sub},
Company: "snapp",
TopicManager: topics.NewTopicManager(cfg.Topics, hid, "snapp", cfg.IssEntityMap, cfg.IssPeerMap, zap.NewNop()),
}

t.Run("testing valid driver cab event", func(t *testing.T) {
t.Parallel()
topicTemplate := authenticator.TopicManager.ParseTopic(validDriverCabEventTopic, topics.DriverIss, "DXKgaNQa7N5Y7bo")
require.NotNil(t, topicTemplate)
})
}

// nolint: funlen
func TestManualAuthenticator_validateAccessType(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -429,14 +413,16 @@ func TestManualAuthenticator_validateAccessType(t *testing.T) {
}
}

func (suite *AuthenticatorTestSuite) getPublicKey(u string) (*rsa.PublicKey, error) {
func getPublicKey(u string) (*rsa.PublicKey, error) {
var fileName string

switch u {
case topics.PassengerIss:
case "1":
fileName = "../../test/snapp-1.pem"
case topics.DriverIss:
case "0":
fileName = "../../test/snapp-0.pem"
case "admin":
fileName = "../../test/snapp-admin.pem"
default:
return nil, ErrPublicKeyNotFound
}
Expand All @@ -454,14 +440,16 @@ func (suite *AuthenticatorTestSuite) getPublicKey(u string) (*rsa.PublicKey, err
return publicKey, nil
}

func (suite *AuthenticatorTestSuite) getPrivateKey(u string) (*rsa.PrivateKey, error) {
func getPrivateKey(u string) (*rsa.PrivateKey, error) {
var fileName string

switch u {
case topics.DriverIss:
case "0":
fileName = "../../test/snapp-0.private.pem"
case topics.PassengerIss:
case "1":
fileName = "../../test/snapp-1.private.pem"
case "admin":
fileName = "../../test/snapp-admin.private.pem"
default:
return nil, ErrPrivateKeyNotFound
}
Expand All @@ -479,10 +467,7 @@ func (suite *AuthenticatorTestSuite) getPrivateKey(u string) (*rsa.PrivateKey, e
return privateKey, nil
}

func (suite *AuthenticatorTestSuite) getSampleToken(issuer string) string {
key, err := suite.getPrivateKey(issuer)
suite.Require().NoError(err)

func getSampleToken(issuer string, key *rsa.PrivateKey) (string, error) {
exp := time.Now().Add(time.Hour * 24 * 365 * 10)
sub := "DXKgaNQa7N5Y7bo"

Expand All @@ -495,7 +480,9 @@ func (suite *AuthenticatorTestSuite) getSampleToken(issuer string) string {
token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)

tokenString, err := token.SignedString(key)
suite.Require().NoError(err)
if err != nil {
return "", fmt.Errorf("cannot generate a signed string %w", err)
}

return tokenString
return tokenString, nil
}
31 changes: 26 additions & 5 deletions internal/authenticator/manual_authenticator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,19 @@ import (
func TestManualAuthenticator_suite(t *testing.T) {
t.Parallel()

st := new(AuthenticatorTestSuite)

pkey0, err := st.getPublicKey(topics.DriverIss)
pkey0, err := getPublicKey("0")
require.NoError(t, err)

pkey1, err := st.getPublicKey(topics.PassengerIss)
pkey1, err := getPublicKey("1")
require.NoError(t, err)

cfg := config.SnappVendor()

hid, err := topics.NewHashIDManager(cfg.HashIDMap)
require.NoError(t, err)

// nolint: exhaustruct
st := new(AuthenticatorTestSuite)

st.Authenticator = authenticator.ManualAuthenticator{
Keys: map[string]any{
topics.DriverIss: pkey0,
Expand All @@ -48,3 +47,25 @@ func TestManualAuthenticator_suite(t *testing.T) {

suite.Run(t, st)
}

func TestManualAuthenticator_ValidateTopicBySender(t *testing.T) {
t.Parallel()

cfg := config.SnappVendor()

hid, err := topics.NewHashIDManager(cfg.HashIDMap)
require.NoError(t, err)

// nolint: exhaustruct
authenticator := authenticator.ManualAuthenticator{
AllowedAccessTypes: []acl.AccessType{acl.Pub, acl.Sub},
Company: "snapp",
TopicManager: topics.NewTopicManager(cfg.Topics, hid, "snapp", cfg.IssEntityMap, cfg.IssPeerMap, zap.NewNop()),
}

t.Run("testing valid driver cab event", func(t *testing.T) {
t.Parallel()
topicTemplate := authenticator.TopicManager.ParseTopic(validDriverCabEventTopic, topics.DriverIss, "DXKgaNQa7N5Y7bo")
require.NotNil(t, topicTemplate)
})
}
9 changes: 9 additions & 0 deletions test/snapp-admin.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1lNRwyNsDieWs6LvHOJ+
GyehhRC4Pn5yL5edKP3565F3LtRDMrkzwDRsQbqnUtTea9HCdTdBv+lI8vE17qRi
RQn10IMaIH6e4Aa3OWNClFhuqNOag7VmffsjTOgxHgHpfGAKVF/4BwqOHrdHFbAD
VOiWB1hv9Uc0C5laffGAub7fj+EAI02zlrsNDxYW8vyF2H47N7VWcvgd3RhZpxlG
8bq9phl7Ja55YmQiT2Ic3/K5tsazg5z9lz6OTrx+JvWbefHFlJpjCLz5yefEaRmX
9L/zyDMi4jgFTZEWNXC2vIrxwZMFwFhBXEp0PcCbuHJgJIucbRrbwukQC16uHJwP
zQIDAQAB
-----END PUBLIC KEY-----
28 changes: 28 additions & 0 deletions test/snapp-admin.private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDWU1HDI2wOJ5az
ou8c4n4bJ6GFELg+fnIvl50o/fnrkXcu1EMyuTPANGxBuqdS1N5r0cJ1N0G/6Ujy
8TXupGJFCfXQgxogfp7gBrc5Y0KUWG6o05qDtWZ9+yNM6DEeAel8YApUX/gHCo4e
t0cVsANU6JYHWG/1RzQLmVp98YC5vt+P4QAjTbOWuw0PFhby/IXYfjs3tVZy+B3d
GFmnGUbxur2mGXslrnliZCJPYhzf8rm2xrODnP2XPo5OvH4m9Zt58cWUmmMIvPnJ
58RpGZf0v/PIMyLiOAVNkRY1cLa8ivHBkwXAWEFcSnQ9wJu4cmAki5xtGtvC6RAL
Xq4cnA/NAgMBAAECggEBAKC93JR9/IyhJGWVzb/cHrg/AOTLpWM5cmo/S59y2/3R
G5IDoGJqhGWi645gbx2wiSBUMYO37ZgBXrTWM5zHrOwajEHWEcJNNNrQLprb1xNr
P5NfIIOniTbzI4aKnkvjIzokzZp6X4roX66pxqZ9XAJSbjMUIOPUgzQvz66lceXb
4aKXSEPdBIRQRmwEUhOV3W8aXNDULR+LwPRt1PYxRm1+W0OPtOwnjfSxBdTbFf8+
Lqsg92/kI/qfPnFq0LsbIdCF9yS/0GhmmJZNErwGl320SF2oXJXKiK2PXKj0mbPV
LLXOMvEkbw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