2017-11-03 19:32:59 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
"github.com/influxdata/chronograf"
|
|
|
|
"github.com/influxdata/chronograf/mocks"
|
|
|
|
"github.com/influxdata/chronograf/organizations"
|
|
|
|
"github.com/influxdata/chronograf/roles"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestStore_SourcesGet(t *testing.T) {
|
|
|
|
type fields struct {
|
|
|
|
SourcesStore chronograf.SourcesStore
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
organization string
|
|
|
|
role string
|
|
|
|
id int
|
|
|
|
}
|
|
|
|
type wants struct {
|
|
|
|
source chronograf.Source
|
|
|
|
err bool
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wants wants
|
|
|
|
}{
|
|
|
|
{
|
2017-11-07 18:59:40 +00:00
|
|
|
name: "Get viewer source as viewer",
|
2017-11-03 19:32:59 +00:00
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "viewer",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-11-07 18:59:40 +00:00
|
|
|
name: "Get viewer source as editor",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "editor",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get viewer source as admin",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "admin",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get admin source as viewer",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "admin",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "viewer",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get editor source as viewer",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "editor",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "viewer",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get editor source as editor",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "editor",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "editor",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "editor",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get editor source as admin",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "editor",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "admin",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "editor",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get editor source as viewer",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "editor",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "viewer",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Get admin source as admin",
|
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "admin",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
|
|
|
organization: "0",
|
|
|
|
role: "admin",
|
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
source: chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "admin",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-11-07 19:57:37 +00:00
|
|
|
name: "No organization or role set on context",
|
2017-11-07 18:59:40 +00:00
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2017-11-07 19:57:37 +00:00
|
|
|
args: args{},
|
2017-11-07 18:59:40 +00:00
|
|
|
wants: wants{
|
2017-11-07 19:57:37 +00:00
|
|
|
err: true,
|
2017-11-07 18:59:40 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-11-07 19:57:37 +00:00
|
|
|
name: "Get source as viewer - no organization specified on context",
|
2017-11-03 19:32:59 +00:00
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2017-11-07 19:57:37 +00:00
|
|
|
role: "viewer",
|
2017-11-03 19:32:59 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-11-07 19:57:37 +00:00
|
|
|
name: "Get source as editor - no organization specified on context",
|
2017-11-07 18:59:40 +00:00
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2017-11-07 19:57:37 +00:00
|
|
|
role: "editor",
|
2017-11-07 18:59:40 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2017-11-07 19:57:37 +00:00
|
|
|
name: "Get source as admin - no organization specified on context",
|
2017-11-03 19:32:59 +00:00
|
|
|
fields: fields{
|
|
|
|
SourcesStore: &mocks.SourcesStore{
|
|
|
|
GetF: func(ctx context.Context, id int) (chronograf.Source, error) {
|
|
|
|
return chronograf.Source{
|
|
|
|
ID: 1,
|
|
|
|
Name: "my sweet name",
|
|
|
|
Organization: "0",
|
|
|
|
Role: "viewer",
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
args: args{
|
2017-11-07 19:57:37 +00:00
|
|
|
role: "admin",
|
2017-11-03 19:32:59 +00:00
|
|
|
},
|
|
|
|
wants: wants{
|
|
|
|
err: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
store := &Store{
|
|
|
|
SourcesStore: tt.fields.SourcesStore,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
if tt.args.organization != "" {
|
|
|
|
ctx = context.WithValue(ctx, organizations.ContextKey, tt.args.organization)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tt.args.role != "" {
|
|
|
|
ctx = context.WithValue(ctx, roles.ContextKey, tt.args.role)
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := store.Sources(ctx).Get(ctx, tt.args.id)
|
|
|
|
if (err != nil) != tt.wants.err {
|
|
|
|
t.Errorf("%q. Store.Sources().Get() error = %v, wantErr %v", tt.name, err, tt.wants.err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if diff := cmp.Diff(source, tt.wants.source); diff != "" {
|
|
|
|
t.Errorf("%q. Store.Sources().Get():\n-got/+want\ndiff %s", tt.name, diff)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|