Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Added tests for auth handler plugin #32

Merged
merged 3 commits into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions plugins/auth/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package auth

import (
"context"
"testing"
)

var record = publicKey{
PublicKey: "LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFuenlpczFaamZOQjBiQmdLRk1Tdgp2a1R0d2x2QnNhSnE3UzV3QStremVWT1ZwVld3a1dkVmhhNHMzOFhNL3BhL3lyNDdhdjcrejNWVG12RFJ5QUhjCmFUOTJ3aFJFRnBMdjljajVsVGVKU2lieXIvTXJtL1l0akNaVldnYU9ZSWh3clh3S0xxUHIvMTFpbldzQWtmSXkKdHZIV1R4WllFY1hMZ0FYRnVVdWFTM3VGOWdFaU5Rd3pHVFUxdjBGcWtxVEJyNEI4blczSENONDdYVXUwdDhZMAplK2xmNHM0T3hRYXdXRDc5SjkvNWQzUnkwdmJWM0FtMUZ0R0ppSnZPd1JzSWZWQ2hEcFlTdFRjSFRDTXF0dldiClY2TDExQldrcHpHWFNXNEh2NDNxYStHU1lPRDJRVTY4TWI1OW9TazJPQitCdE9McEpvZm1iR0VHZ3Ztd3lDSTkKTXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t",
RoleKey: "admin",
}

var savePublicKeyTest = []struct {
setup *ServerSetup
rawResp []byte
err string
}{
{
&ServerSetup{
Method: "PUT",
Path: "/test/_doc/_public_key",
Body: `{"public_key":"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFuenlpczFaamZOQjBiQmdLRk1Tdgp2a1R0d2x2QnNhSnE3UzV3QStremVWT1ZwVld3a1dkVmhhNHMzOFhNL3BhL3lyNDdhdjcrejNWVG12RFJ5QUhjCmFUOTJ3aFJFRnBMdjljajVsVGVKU2lieXIvTXJtL1l0akNaVldnYU9ZSWh3clh3S0xxUHIvMTFpbldzQWtmSXkKdHZIV1R4WllFY1hMZ0FYRnVVdWFTM3VGOWdFaU5Rd3pHVFUxdjBGcWtxVEJyNEI4blczSENONDdYVXUwdDhZMAplK2xmNHM0T3hRYXdXRDc5SjkvNWQzUnkwdmJWM0FtMUZ0R0ppSnZPd1JzSWZWQ2hEcFlTdFRjSFRDTXF0dldiClY2TDExQldrcHpHWFNXNEh2NDNxYStHU1lPRDJRVTY4TWI1OW9TazJPQitCdE9McEpvZm1iR0VHZ3Ztd3lDSTkKTXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t","role_key":"admin"}`,
Response: `{"_index":"test","_type":"doc","_id":"user1","_version":1,"found":true,"_source":{"public_key":"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFuenlpczFaamZOQjBiQmdLRk1Tdgp2a1R0d2x2QnNhSnE3UzV3QStremVWT1ZwVld3a1dkVmhhNHMzOFhNL3BhL3lyNDdhdjcrejNWVG12RFJ5QUhjCmFUOTJ3aFJFRnBMdjljajVsVGVKU2lieXIvTXJtL1l0akNaVldnYU9ZSWh3clh3S0xxUHIvMTFpbldzQWtmSXkKdHZIV1R4WllFY1hMZ0FYRnVVdWFTM3VGOWdFaU5Rd3pHVFUxdjBGcWtxVEJyNEI4blczSENONDdYVXUwdDhZMAplK2xmNHM0T3hRYXdXRDc5SjkvNWQzUnkwdmJWM0FtMUZ0R0ppSnZPd1JzSWZWQ2hEcFlTdFRjSFRDTXF0dldiClY2TDExQldrcHpHWFNXNEh2NDNxYStHU1lPRDJRVTY4TWI1OW9TazJPQitCdE9McEpvZm1iR0VHZ3Ztd3lDSTkKTXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t","role_key":"admin"}}`,
},
[]byte(`{"_index":"test","_type":"doc","_id":"user1","_version":1,"found":true,"_source":{"public_key":"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFuenlpczFaamZOQjBiQmdLRk1Tdgp2a1R0d2x2QnNhSnE3UzV3QStremVWT1ZwVld3a1dkVmhhNHMzOFhNL3BhL3lyNDdhdjcrejNWVG12RFJ5QUhjCmFUOTJ3aFJFRnBMdjljajVsVGVKU2lieXIvTXJtL1l0akNaVldnYU9ZSWh3clh3S0xxUHIvMTFpbldzQWtmSXkKdHZIV1R4WllFY1hMZ0FYRnVVdWFTM3VGOWdFaU5Rd3pHVFUxdjBGcWtxVEJyNEI4blczSENONDdYVXUwdDhZMAplK2xmNHM0T3hRYXdXRDc5SjkvNWQzUnkwdmJWM0FtMUZ0R0ppSnZPd1JzSWZWQ2hEcFlTdFRjSFRDTXF0dldiClY2TDExQldrcHpHWFNXNEh2NDNxYStHU1lPRDJRVTY4TWI1OW9TazJPQitCdE9McEpvZm1iR0VHZ3Ztd3lDSTkKTXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t","role_key":"admin"}}`),
"",
},
}

func TestSavePublicKey(t *testing.T) {
for _, tt := range savePublicKeyTest {
t.Run("savePublicKeyTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, "test", "test")
_, err := es.savePublicKey(context.Background(), "test",record)

if !compareErrs(tt.err, err) {
t.Fatalf("Cat aliases should have failed with error: %v got: %v instead\n", tt.err, err)
}
})
}
}

var getPublicKeyTest = []struct {
setup *ServerSetup
rawResp []byte
err string
}{
{
&ServerSetup{
Method: "GET",
Path: "/.publickey/_doc/_public_key",
Body: "",
Response: `{"_index":".publickey","_type":"doc","_id":"user1","_version":1,"found":true,"_source":{"public_key":"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFuenlpczFaamZOQjBiQmdLRk1Tdgp2a1R0d2x2QnNhSnE3UzV3QStremVWT1ZwVld3a1dkVmhhNHMzOFhNL3BhL3lyNDdhdjcrejNWVG12RFJ5QUhjCmFUOTJ3aFJFRnBMdjljajVsVGVKU2lieXIvTXJtL1l0akNaVldnYU9ZSWh3clh3S0xxUHIvMTFpbldzQWtmSXkKdHZIV1R4WllFY1hMZ0FYRnVVdWFTM3VGOWdFaU5Rd3pHVFUxdjBGcWtxVEJyNEI4blczSENONDdYVXUwdDhZMAplK2xmNHM0T3hRYXdXRDc5SjkvNWQzUnkwdmJWM0FtMUZ0R0ppSnZPd1JzSWZWQ2hEcFlTdFRjSFRDTXF0dldiClY2TDExQldrcHpHWFNXNEh2NDNxYStHU1lPRDJRVTY4TWI1OW9TazJPQitCdE9McEpvZm1iR0VHZ3Ztd3lDSTkKTXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t","role_key":"admin"}}`,
},
[]byte(`{"_index":".publickey","_type":"doc","_id":"user1","_version":1,"found":true,"_source":{"public_key":"LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUFuenlpczFaamZOQjBiQmdLRk1Tdgp2a1R0d2x2QnNhSnE3UzV3QStremVWT1ZwVld3a1dkVmhhNHMzOFhNL3BhL3lyNDdhdjcrejNWVG12RFJ5QUhjCmFUOTJ3aFJFRnBMdjljajVsVGVKU2lieXIvTXJtL1l0akNaVldnYU9ZSWh3clh3S0xxUHIvMTFpbldzQWtmSXkKdHZIV1R4WllFY1hMZ0FYRnVVdWFTM3VGOWdFaU5Rd3pHVFUxdjBGcWtxVEJyNEI4blczSENONDdYVXUwdDhZMAplK2xmNHM0T3hRYXdXRDc5SjkvNWQzUnkwdmJWM0FtMUZ0R0ppSnZPd1JzSWZWQ2hEcFlTdFRjSFRDTXF0dldiClY2TDExQldrcHpHWFNXNEh2NDNxYStHU1lPRDJRVTY4TWI1OW9TazJPQitCdE9McEpvZm1iR0VHZ3Ztd3lDSTkKTXdJREFRQUIKLS0tLS1FTkQgUFVCTElDIEtFWS0tLS0t","role_key":"admin"}}`),
"",
},
}

func TestGetPublicKey(t *testing.T) {
for _, tt := range getPublicKeyTest {
t.Run("getPublicKeyTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, ".publickey", ".publickey")
_, err := es.getPublicKey(context.Background())
if !compareErrs(tt.err, err) {
t.Fatalf("Cat aliases should have failed with error: %v got: %v instead\n", tt.err, err)
}
})
}
}
13 changes: 12 additions & 1 deletion plugins/auth/middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@ func (m *mockAuthService) getCredential(ctx context.Context, username string) (c
}
}

func (m *mockAuthService) createIndex(indexName, mapping string) (bool, error) {
args := m.Called(indexName, mapping)
return args.Bool(0), args.Error(1)
}
func (m *mockAuthService) putUser(ctx context.Context, u user.User) (bool, error) {
args := m.Called(ctx, u)
return args.Bool(0), args.Error(1)
}

func (m *mockAuthService) getUser(ctx context.Context, username string) (*user.User, error){
args := m.Called(ctx, username)
return args.Get(0).(*user.User), args.Error(1)
Expand All @@ -50,6 +53,10 @@ func (m *mockAuthService) putPermission(ctx context.Context, p permission.Permis
args := m.Called(ctx, p)
return args.Bool(0), args.Error(1)
}
func (m *mockAuthService) getPublicKey(ctx context.Context) (publicKey, error) {
args := m.Called(ctx)
return args.Get(0).(publicKey),args.Error(1)
}
func (m *mockAuthService) getPermission(ctx context.Context, username string) (*permission.Permission, error) {
args := m.Called(ctx, username)
return args.Get(0).(*permission.Permission), args.Error(1)
Expand All @@ -62,6 +69,10 @@ func (m *mockAuthService) getRawPermission(ctx context.Context, username string)
args := m.Called(ctx, username)
return args.Get(0).([]byte), args.Error(1)
}
func (m *mockAuthService) savePublicKey(ctx context.Context, indexName string, record publicKey) (interface{}, error) {
args := m.Called(ctx, indexName,record)
return args.Get(0).(interface{}),args.Error(1)
}

func TestBasicAuthWithUserPasswordBasic(t *testing.T) {
hashedPassword, _ := bcrypt.GenerateFromPassword([]byte("bar"), bcrypt.DefaultCost)
Expand Down