From 79c67e207a013754c56e0fed1d5431e0159427de Mon Sep 17 00:00:00 2001 From: Parham Alvani Date: Sat, 11 Nov 2023 23:33:26 +0000 Subject: [PATCH] feat: improve test coverage --- .../authenticator/admin_authenticator_test.go | 65 ++++++++++++++++ internal/authenticator/authenticator_test.go | 77 ++++++++----------- .../manual_authenticator_test.go | 31 ++++++-- test/snapp-admin.pem | 9 +++ test/snapp-admin.private.pem | 28 +++++++ 5 files changed, 160 insertions(+), 50 deletions(-) create mode 100644 internal/authenticator/admin_authenticator_test.go create mode 100644 test/snapp-admin.pem create mode 100644 test/snapp-admin.private.pem diff --git a/internal/authenticator/admin_authenticator_test.go b/internal/authenticator/admin_authenticator_test.go new file mode 100644 index 0000000..6386338 --- /dev/null +++ b/internal/authenticator/admin_authenticator_test.go @@ -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)) + }) +} diff --git a/internal/authenticator/authenticator_test.go b/internal/authenticator/authenticator_test.go index 6dd8345..ba4bd27 100644 --- a/internal/authenticator/authenticator_test.go +++ b/internal/authenticator/authenticator_test.go @@ -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 ( @@ -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() { @@ -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() @@ -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 } @@ -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 } @@ -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" @@ -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 } diff --git a/internal/authenticator/manual_authenticator_test.go b/internal/authenticator/manual_authenticator_test.go index 7dbd551..3c0e989 100644 --- a/internal/authenticator/manual_authenticator_test.go +++ b/internal/authenticator/manual_authenticator_test.go @@ -16,12 +16,10 @@ 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() @@ -29,7 +27,8 @@ func TestManualAuthenticator_suite(t *testing.T) { 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, @@ -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) + }) +} diff --git a/test/snapp-admin.pem b/test/snapp-admin.pem new file mode 100644 index 0000000..c73d608 --- /dev/null +++ b/test/snapp-admin.pem @@ -0,0 +1,9 @@ +-----BEGIN PUBLIC KEY----- +MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1lNRwyNsDieWs6LvHOJ+ +GyehhRC4Pn5yL5edKP3565F3LtRDMrkzwDRsQbqnUtTea9HCdTdBv+lI8vE17qRi +RQn10IMaIH6e4Aa3OWNClFhuqNOag7VmffsjTOgxHgHpfGAKVF/4BwqOHrdHFbAD +VOiWB1hv9Uc0C5laffGAub7fj+EAI02zlrsNDxYW8vyF2H47N7VWcvgd3RhZpxlG +8bq9phl7Ja55YmQiT2Ic3/K5tsazg5z9lz6OTrx+JvWbefHFlJpjCLz5yefEaRmX +9L/zyDMi4jgFTZEWNXC2vIrxwZMFwFhBXEp0PcCbuHJgJIucbRrbwukQC16uHJwP +zQIDAQAB +-----END PUBLIC KEY----- \ No newline at end of file diff --git a/test/snapp-admin.private.pem b/test/snapp-admin.private.pem new file mode 100644 index 0000000..bffadbc --- /dev/null +++ b/test/snapp-admin.private.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDWU1HDI2wOJ5az +ou8c4n4bJ6GFELg+fnIvl50o/fnrkXcu1EMyuTPANGxBuqdS1N5r0cJ1N0G/6Ujy +8TXupGJFCfXQgxogfp7gBrc5Y0KUWG6o05qDtWZ9+yNM6DEeAel8YApUX/gHCo4e +t0cVsANU6JYHWG/1RzQLmVp98YC5vt+P4QAjTbOWuw0PFhby/IXYfjs3tVZy+B3d +GFmnGUbxur2mGXslrnliZCJPYhzf8rm2xrODnP2XPo5OvH4m9Zt58cWUmmMIvPnJ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