Skip to content

Commit

Permalink
Issue #228: Fix authorization in GRPC calls
Browse files Browse the repository at this point in the history
	--Fixed breaking UTs
  • Loading branch information
razorao committed Aug 17, 2021
1 parent d81f454 commit 48ab022
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions internal/interceptors/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,45 +15,37 @@ import (
"google.golang.org/grpc/metadata"
)

func Test_extractProjectIDFromURI(t *testing.T) {
assert.Equal(t, "project1", extractProjectIDFromURI("/v1/projects/project1/topics/t123"))
assert.Equal(t, "project7", extractProjectIDFromURI("/v1/projects/project7/subscriptions/s123"))
assert.Equal(t, "", extractProjectIDFromURI("/v1/admin/topic/t987"))
assert.Equal(t, "", extractProjectIDFromURI(""))
}

func Test_secureCompare(t *testing.T) {
assert.True(t, secureCompare("correct", "correct"))
assert.False(t, secureCompare("correct", "wrong"))
}

func Test_getUserPasswordProjectID_Success(t *testing.T) {
func Test_getUserPassword_Success(t *testing.T) {

ctx := context.Background()
// dummy12__13a011 2L9J4A0rdzcIO722089L
pairs := []string{authorizationHeaderKey, "Basic ZHVtbXkxMl9fMTNhMDExOjJMOUo0QTByZHpjSU83MjIwODlM", "uri", "/v1/projects/dummy12/topics/t123"}
md := metadata.Pairs(pairs...)
newCtx := metadata.NewIncomingContext(ctx, md)

user, pwd, projectID, err := getUserPasswordProjectID(newCtx)
user, pwd, err := getUserPassword(newCtx)
assert.Equal(t, "dummy12__13a011", user)
assert.Equal(t, "2L9J4A0rdzcIO722089L", string(pwd))
assert.Equal(t, "dummy12", projectID)
assert.Nil(t, err)
}

func Test_getUserPasswordProjectID_Failure1(t *testing.T) {
_, _, _, err := getUserPasswordProjectID(context.Background()) // empty metadata
_, _, err := getUserPassword(context.Background()) // empty metadata
assert.NotNil(t, err)
}

func Test_getUserPasswordProjectID_Failure2(t *testing.T) {
func Test_getUserPassword_Failure2(t *testing.T) {
ctx := context.Background()
pairs := []string{authorizationHeaderKey, "", "uri", "/v1/projects/dummy12/topics/t123"}
md := metadata.Pairs(pairs...)
newCtx := metadata.NewIncomingContext(ctx, md)

_, _, _, err := getUserPasswordProjectID(newCtx) // empty authorization header
_, _, err := getUserPassword(newCtx) // empty authorization header
assert.NotNil(t, err)
}

Expand All @@ -63,7 +55,7 @@ func Test_getUserPasswordProjectID_Failure3(t *testing.T) {
md := metadata.Pairs(pairs...)
newCtx := metadata.NewIncomingContext(ctx, md)

_, _, _, err := getUserPasswordProjectID(newCtx) // wrong authorization header
_, _, err := getUserPassword(newCtx) // wrong authorization header
assert.NotNil(t, err)
}

Expand Down Expand Up @@ -123,13 +115,13 @@ func Test_AppAuth_Success(t *testing.T) {
mockCore := mocks1.NewMockICore(ctrl)
mockCore.EXPECT().Get(gomock.Any(), project, username).Return(dummyCreds, nil)

ctx, err := AppAuth(newCtx, mockCore)
ctx, err := AppAuth(newCtx, mockCore, project)
assert.Nil(t, err)
}

func Test_AppAuth_Failure1(t *testing.T) {
ctx := context.Background()
ctx, err := AppAuth(ctx, nil) // missing auth header
ctx, err := AppAuth(ctx, nil, "") // missing auth header
assert.NotNil(t, err)
}

Expand All @@ -139,7 +131,7 @@ func Test_AppAuth_Failure2(t *testing.T) {
md := metadata.Pairs(pairs...)
newCtx := metadata.NewIncomingContext(ctx, md)

ctx, err := AppAuth(newCtx, nil)
ctx, err := AppAuth(newCtx, nil, "")
assert.NotNil(t, err)
}

Expand All @@ -153,7 +145,7 @@ func Test_AppAuth_Failure3(t *testing.T) {
mockCore := mocks1.NewMockICore(ctrl)
mockCore.EXPECT().Get(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("some error")) // credential core error

ctx, err := AppAuth(newCtx, mockCore)
ctx, err := AppAuth(newCtx, mockCore, "")
assert.NotNil(t, err)
}

Expand All @@ -174,7 +166,7 @@ func Test_AppAuth_Failure4(t *testing.T) {
mockCore.EXPECT().Get(gomock.Any(), project, username).Return(dummyCreds, nil)

// uri projectID and credential projectID mismatch
ctx, err := AppAuth(newCtx, mockCore)
ctx, err := AppAuth(newCtx, mockCore, "")
assert.NotNil(t, err)
}

Expand All @@ -194,6 +186,6 @@ func Test_AppAuth_Failure5(t *testing.T) {
mockCore := mocks1.NewMockICore(ctrl)
mockCore.EXPECT().Get(gomock.Any(), project, username).Return(dummyCreds, nil)

ctx, err := AppAuth(newCtx, mockCore)
ctx, err := AppAuth(newCtx, mockCore, "")
assert.NotNil(t, err)
}

0 comments on commit 48ab022

Please sign in to comment.