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

fix: Tests for plugins #25

Closed
wants to merge 11 commits into from
8 changes: 4 additions & 4 deletions plugins/auth/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var getUserTest = []struct {

func TestGetUser(t *testing.T) {
for _, tt := range getUserTest {
t.Run("getPermissionTest", func(t *testing.T) {
t.Run("getUserTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, "test", "test")
Expand Down Expand Up @@ -171,7 +171,7 @@ var putUserTest = []struct {

func TestPutUser(t *testing.T) {
for _, tt := range putUserTest {
t.Run("getPermissionTest", func(t *testing.T) {
t.Run("putUserTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, "test", "perm1")
Expand Down Expand Up @@ -234,7 +234,7 @@ var putPermissionTest = []struct {

func TestPutPermission(t *testing.T) {
for _, tt := range putPermissionTest {
t.Run("getPermissionTest", func(t *testing.T) {
t.Run("putPermissionTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, "test", "perm")
Expand Down Expand Up @@ -291,7 +291,7 @@ var getCredentialTest = []struct {

func TestGetCredential(t *testing.T) {
for _, tt := range getCredentialTest {
t.Run("getPermissionTest", func(t *testing.T) {
t.Run("getCredentialTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, "test", "perm")
Expand Down
16 changes: 8 additions & 8 deletions plugins/logs/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var getTotalNodesTest = []struct {
{
&ServerSetup{
Method: "GET",
Path: "/_nodes/_all/_all%2Cnodes",
Path: "/_nodes/_all/nodes",
Body: "",
Response: `{"cluster_name": "elasticsearch","nodes":{"kxyUith0T3yui4gn6PBbJQ":{"name": "kxyUith","transport_address": "127.0.0.1:9300","host": "127.0.0.1","ip": "127.0.0.1","version": "6.2.4","build_hash": "ccec39f","roles":["master","data","ingest"]}}}`,
},
Expand All @@ -31,7 +31,7 @@ var getTotalNodesTest = []struct {
{
&ServerSetup{
Method: "GET",
Path: "/_nodes/_all/_all%2Cnodes",
Path: "/_nodes/_all/nodes",
Body: "",
Response: `{"cluster_name": "elasticsearch","nodes:{"kxyUith0T3yui4gn6PBbJQ":{"name": "kxyUith","transport_address": "127.0.0.1:9300","host": "127.0.0.1","ip": "127.0.0.1","version": "6.2.4","build_hash": "ccec39f","roles":["master","data","ingest"]}}}`,
},
Expand Down Expand Up @@ -145,8 +145,8 @@ var getRawLogsTest = []struct {
&ServerSetup{
Method: "POST",
Path: "/test1/_search",
Body: `{"from":0,"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Response: `{"took":90,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
Body: `{"from":0,"query":{"bool":{"filter":{"match_all":{}}}},"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Response: `{"took":90,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":{"value": 2, "relation": "eq"},"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
},
"test1",
"0",
Expand All @@ -158,7 +158,7 @@ var getRawLogsTest = []struct {
&ServerSetup{
Method: "POST",
Path: "/test1/_search",
Body: `{"from":0,"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Body: `{"from":0,"query":{"bool":{"filter":{"match_all":{}}}},"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Response: `{"took":90,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
},
"test1",
Expand All @@ -171,7 +171,7 @@ var getRawLogsTest = []struct {
&ServerSetup{
Method: "POST",
Path: "/test1/_search",
Body: `{"from":0,"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Body: `{"from":0,"query":{"bool":{"filter":{"match_all":{}}}},"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Response: `{"took":90,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
},
"test1",
Expand All @@ -184,7 +184,7 @@ var getRawLogsTest = []struct {
&ServerSetup{
Method: "POST",
Path: "/test1/_search",
Body: `{"from":0,"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Body: `{"from":0,"query":{"bool":{"filter":{"match_all":{}}}},"size":10,"sort":[{"timestamp":{"order":"desc"}}]}`,
Response: `{took":90,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
},
"test1",
Expand All @@ -196,7 +196,7 @@ var getRawLogsTest = []struct {

func TestGetRawLogs(t *testing.T) {
for _, tt := range getRawLogsTest {
t.Run("getTotalNodesTest", func(t *testing.T) {
t.Run("getRawLogsTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, tt.index)
Expand Down
24 changes: 12 additions & 12 deletions plugins/permissions/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var getTotalNodesTest = []struct {
{
&ServerSetup{
Method: "GET",
Path: "/_nodes/_all/_all%2Cnodes",
Path: "/_nodes/_all/nodes",
Body: "",
Response: `{"cluster_name": "elasticsearch","nodes":{"kxyUith0T3yui4gn6PBbJQ":{"name": "kxyUith","transport_address": "127.0.0.1:9300","host": "127.0.0.1","ip": "127.0.0.1","version": "6.2.4","build_hash": "ccec39f","roles":["master","data","ingest"]}}}`,
},
Expand All @@ -34,7 +34,7 @@ var getTotalNodesTest = []struct {
{
&ServerSetup{
Method: "GET",
Path: "/_nodes/_all/_all%2Cnodes",
Path: "/_nodes/_all/nodes",
Body: "",
Response: `{cluster_name": "elasticsearch","nodes":{"kxyUith0T3yui4gn6PBbJQ":{"name": "kxyUith","transport_address": "127.0.0.1:9300","host": "127.0.0.1","ip": "127.0.0.1","version": "6.2.4","build_hash": "ccec39f","roles":["master","data","ingest"]}}}`,
},
Expand Down Expand Up @@ -97,7 +97,7 @@ var getPermissionTest = []struct {

func TestGetPermission(t *testing.T) {
for _, tt := range getPermissionTest {
t.Run("getTotalNodesTest", func(t *testing.T) {
t.Run("getPermissionTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, tt.index, "mapping")
Expand Down Expand Up @@ -146,7 +146,7 @@ var deletePermissionTest = []struct {

func TestDeletePermission(t *testing.T) {
for _, tt := range deletePermissionTest {
t.Run("getUserTest", func(t *testing.T) {
t.Run("deletePermissionTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, tt.indexName, "mapping")
Expand Down Expand Up @@ -221,7 +221,7 @@ func TestPatchPermission(t *testing.T) {
_, err := es.patchPermission(context.Background(), "user1", tt.patchMap)

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

/*if !reflect.DeepEqual(response, tt.response) {
Expand Down Expand Up @@ -292,14 +292,14 @@ var postPermissionTest = []struct {

func TestPostPermission(t *testing.T) {
for _, tt := range postPermissionTest {
t.Run("patchPermissionTest", func(t *testing.T) {
t.Run("postPermissionTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, tt.indexName, "mapping")
_, err := es.postPermission(context.Background(), tt.payload)

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

/*if !reflect.DeepEqual(response, tt.response) {
Expand All @@ -317,9 +317,9 @@ var getRawOwnerPermissionsTest = []struct {
{
&ServerSetup{
Method: "POST",
Path: "/test1/_doc/_search",
Path: "/test1/_search",
Body: `{"query":{"term":{"owner.keyword":"owner1"}}}`,
Response: `{"took":107,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
Response: `{"took":107,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":{"value": 2, "relation": "eq"},"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
},
"test1",
"",
Expand All @@ -328,7 +328,7 @@ var getRawOwnerPermissionsTest = []struct {
{
&ServerSetup{
Method: "POST",
Path: "/test1/_doc/_search",
Path: "/test1/_search",
Body: `{"query":{"term":{"owner.keyword":"owner1"}}}`,
Response: `{took":107,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.0,"hits":[{"_index":"test","_type":"_doc","_id":"1","_score":1.0,"_source":{"counter":5,"tags":["red","blue","blue","blue","blue","blue"]}},{"_index":"test","_type":"_doc","_id":"3","_score":1.0,"_source":{ "field1" : "value3" }}]}}`,
},
Expand All @@ -339,14 +339,14 @@ var getRawOwnerPermissionsTest = []struct {

func TestGetRawOwnerPermissions(t *testing.T) {
for _, tt := range getRawOwnerPermissionsTest {
t.Run("patchPermissionTest", func(t *testing.T) {
t.Run("getRawOwnerPermissionTest", func(t *testing.T) {
ts := buildTestServer(t, []*ServerSetup{tt.setup})
defer ts.Close()
es, _ := newStubClient(ts.URL, tt.indexName, "mapping")
_, err := es.getRawOwnerPermissions(context.Background(), "owner1")

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

/*if !reflect.DeepEqual(response, tt.response) {
Expand Down
2 changes: 1 addition & 1 deletion plugins/users/dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (es *elasticsearch) getRawUsers(ctx context.Context) ([]byte, error) {
func (es *elasticsearch) getRawUser(ctx context.Context, username string) ([]byte, error) {
response, err := es.client.Get().
Index(es.indexName).
//Type(es.typeName).
Type(es.typeName).
Id(username).
FetchSource(true).
Do(ctx)
Expand Down
15 changes: 2 additions & 13 deletions plugins/users/dao_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var getTotalNodesTest = []struct {
{
&ServerSetup{
Method: "GET",
Path: "/_nodes/_all/_all%2Cnodes",
Path: "/_nodes/_all/nodes",
Body: "",
Response: `{"cluster_name": "elasticsearch","nodes":{"kxyUith0T3yui4gn6PBbJQ":{"name": "kxyUith","transport_address": "127.0.0.1:9300","host": "127.0.0.1","ip": "127.0.0.1","version": "6.2.4","build_hash": "ccec39f","roles":["master","data","ingest"]}}}`,
},
Expand Down Expand Up @@ -178,22 +178,11 @@ var deleteUserTest = []struct {
"_doc",
"",
},
{
&ServerSetup{
Method: "DELETE",
Path: "/test1/_doc/user1",
Body: "",
Response: `{"_index":"test1","_type":"_doc","_id":"user1","_version":2,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}`,
},
false,
"",
"missing required fields: [Type]",
},
}

func TestDeleteUser(t *testing.T) {
for _, tt := range deleteUserTest {
t.Run("getUserTest", func(t *testing.T) {
t.Run("deleteUserTest", func(t *testing.T) {
ts := buildTestServer(t, tt.setup)
defer ts.Close()
es, _ := newStubClient(ts.URL, "test1", tt.typeName)
Expand Down