2017-03-10 19:24:48 +00:00
package server
import (
2017-10-26 23:02:57 +00:00
"bytes"
2017-03-10 19:24:48 +00:00
"context"
2017-10-26 23:02:57 +00:00
"encoding/json"
2017-03-10 19:24:48 +00:00
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
2018-07-19 20:52:14 +00:00
"github.com/influxdata/platform/chronograf"
"github.com/influxdata/platform/chronograf/mocks"
"github.com/influxdata/platform/chronograf/oauth2"
"github.com/influxdata/platform/chronograf/roles"
2017-03-10 19:24:48 +00:00
)
func TestService_Me ( t * testing . T ) {
type fields struct {
2018-03-23 20:12:18 +00:00
UsersStore chronograf . UsersStore
OrganizationsStore chronograf . OrganizationsStore
MappingsStore chronograf . MappingsStore
ConfigStore chronograf . ConfigStore
SuperAdminProviderGroups superAdminProviderGroups
Logger chronograf . Logger
UseAuth bool
2017-03-10 19:24:48 +00:00
}
type args struct {
w * httptest . ResponseRecorder
r * http . Request
}
tests := [ ] struct {
name string
fields fields
args args
principal oauth2 . Principal
wantStatus int
wantContentType string
wantBody string
} {
2017-11-14 03:28:15 +00:00
{
name : "Existing user - not member of any organization" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
2017-12-13 19:55:36 +00:00
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-12-13 19:55:36 +00:00
ConfigStore : & mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
2017-12-13 22:49:49 +00:00
SuperAdminNewUsers : false ,
2017-12-13 19:55:36 +00:00
} ,
} ,
} ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
2018-02-07 02:33:27 +00:00
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
2018-02-05 19:54:39 +00:00
} ,
} , nil
} ,
} ,
2017-11-14 03:28:15 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 03:28:15 +00:00
Name : "Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
} ,
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
switch * q . ID {
2017-12-18 22:07:40 +00:00
case "0" :
2017-11-14 03:28:15 +00:00
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 03:28:15 +00:00
Name : "Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
2017-12-18 22:07:40 +00:00
case "1" :
2017-11-14 03:28:15 +00:00
return & chronograf . Organization {
2018-02-10 00:00:27 +00:00
ID : "1" ,
Name : "The Bad Place" ,
2017-11-14 03:28:15 +00:00
} , nil
}
return nil , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
2017-11-30 17:56:13 +00:00
NumF : func ( ctx context . Context ) ( int , error ) {
2017-11-14 03:28:15 +00:00
// This function gets to verify that there is at least one first user
2017-11-30 17:56:13 +00:00
return 1 , nil
2017-11-14 03:28:15 +00:00
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-11-14 03:28:15 +00:00
}
return & chronograf . User {
Name : "me" ,
Provider : "github" ,
Scheme : "oauth2" ,
} , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "me" ,
Issuer : "github" ,
} ,
2018-02-09 19:42:07 +00:00
wantStatus : http . StatusOK ,
2017-11-14 03:28:15 +00:00
wantContentType : "application/json" ,
2018-02-10 00:00:27 +00:00
wantBody : ` { "name":"me","roles":null,"provider":"github","scheme":"oauth2","links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[],"currentOrganization": { "id":"0","name":"Default","defaultRole":"viewer"}} ` ,
2017-11-14 03:28:15 +00:00
} ,
2017-12-14 01:38:57 +00:00
{
2018-02-09 19:42:07 +00:00
name : "Existing superadmin - not member of any organization" ,
2017-12-14 01:38:57 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping { } , nil
} ,
} ,
2017-12-14 01:38:57 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-12-14 01:38:57 +00:00
Name : "Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
} ,
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
switch * q . ID {
2017-12-18 22:07:40 +00:00
case "0" :
2017-12-14 01:38:57 +00:00
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-12-14 01:38:57 +00:00
Name : "Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
2017-12-18 22:07:40 +00:00
case "1" :
2017-12-14 01:38:57 +00:00
return & chronograf . Organization {
2018-02-10 00:00:27 +00:00
ID : "1" ,
Name : "The Bad Place" ,
2017-12-14 01:38:57 +00:00
} , nil
}
return nil , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-12-14 01:38:57 +00:00
}
return & chronograf . User {
Name : "me" ,
Provider : "github" ,
Scheme : "oauth2" ,
SuperAdmin : true ,
} , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "me" ,
Issuer : "github" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-02-10 00:00:27 +00:00
wantBody : ` { "name":"me","roles":null,"provider":"github","scheme":"oauth2","superAdmin":true,"links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[],"currentOrganization": { "id":"0","name":"Default","defaultRole":"viewer"}} ` ,
2017-03-10 19:24:48 +00:00
} ,
2017-11-21 23:53:42 +00:00
{
name : "Existing user - organization doesn't exist" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping { } , nil
} ,
} ,
2017-11-21 23:53:42 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-21 23:53:42 +00:00
Name : "Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
} ,
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
switch * q . ID {
2017-12-18 22:07:40 +00:00
case "0" :
2017-11-21 23:53:42 +00:00
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-21 23:53:42 +00:00
Name : "Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
}
return nil , chronograf . ErrOrganizationNotFound
} ,
} ,
UsersStore : & mocks . UsersStore {
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-11-21 23:53:42 +00:00
}
return & chronograf . User {
Name : "me" ,
Provider : "github" ,
Scheme : "oauth2" ,
} , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "me" ,
Issuer : "github" ,
Organization : "1" ,
} ,
wantStatus : http . StatusForbidden ,
wantContentType : "application/json" ,
wantBody : ` { "code":403,"message":"user's current organization was not found"} ` ,
} ,
2017-03-10 19:24:48 +00:00
{
2018-02-10 00:00:27 +00:00
name : "default mapping applies to new user" ,
2017-03-10 19:24:48 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
2017-12-13 19:55:36 +00:00
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-12-13 19:55:36 +00:00
ConfigStore : & mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
2017-12-13 22:49:49 +00:00
SuperAdminNewUsers : true ,
2017-12-13 19:55:36 +00:00
} ,
} ,
} ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
2018-02-07 02:33:27 +00:00
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
2018-02-05 19:54:39 +00:00
} ,
} , nil
} ,
} ,
2017-11-02 15:59:53 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
2017-11-02 20:47:45 +00:00
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 02:08:50 +00:00
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
2017-11-02 20:47:45 +00:00
} , nil
} ,
2017-11-02 15:59:53 +00:00
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 02:08:50 +00:00
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
2017-11-02 15:59:53 +00:00
} , nil
} ,
2018-01-11 21:10:27 +00:00
AllF : func ( ctx context . Context ) ( [ ] chronograf . Organization , error ) {
return [ ] chronograf . Organization {
chronograf . Organization {
ID : "0" ,
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
} ,
} , nil
} ,
2017-11-02 15:59:53 +00:00
} ,
2017-03-10 19:24:48 +00:00
UsersStore : & mocks . UsersStore {
2017-11-30 17:56:13 +00:00
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
2017-11-09 20:44:26 +00:00
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-11-09 20:44:26 +00:00
}
return nil , chronograf . ErrUserNotFound
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-02-10 00:00:27 +00:00
wantBody : ` { "name":"secret","superAdmin":true,"roles":[ { "name":"viewer","organization":"0"}],"provider":"auth0","scheme":"oauth2","links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Gnarly Default","defaultRole":"viewer"}],"currentOrganization": { "id":"0","name":"The Gnarly Default","defaultRole":"viewer"}} ` ,
2017-11-09 20:44:26 +00:00
} ,
{
2017-11-30 17:56:13 +00:00
name : "New user - New users not super admin, not first user" ,
2017-11-09 20:44:26 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
2017-12-13 19:55:36 +00:00
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-12-13 19:55:36 +00:00
ConfigStore : & mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
2017-12-13 22:49:49 +00:00
SuperAdminNewUsers : false ,
2017-12-13 19:55:36 +00:00
} ,
} ,
} ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
2018-02-07 02:33:27 +00:00
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
2018-02-05 19:54:39 +00:00
} ,
} , nil
} ,
} ,
2017-11-09 20:44:26 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 07:10:26 +00:00
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
2017-11-09 20:44:26 +00:00
} , nil
2017-11-01 13:12:19 +00:00
} ,
2017-11-09 20:44:26 +00:00
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 07:10:26 +00:00
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
2017-11-09 20:44:26 +00:00
} , nil
} ,
2018-01-11 21:10:27 +00:00
AllF : func ( ctx context . Context ) ( [ ] chronograf . Organization , error ) {
return [ ] chronograf . Organization {
chronograf . Organization {
ID : "0" ,
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
} ,
} , nil
} ,
2017-11-09 20:44:26 +00:00
} ,
UsersStore : & mocks . UsersStore {
2017-11-30 17:56:13 +00:00
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
2017-10-18 18:17:42 +00:00
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
2017-10-19 18:17:40 +00:00
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-10-19 18:17:40 +00:00
}
2017-10-18 18:45:33 +00:00
return nil , chronograf . ErrUserNotFound
2017-03-10 19:24:48 +00:00
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
2017-11-02 23:13:51 +00:00
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
2017-03-10 19:24:48 +00:00
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
2017-10-24 23:17:59 +00:00
Issuer : "auth0" ,
2017-03-10 19:24:48 +00:00
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-02-10 00:00:27 +00:00
wantBody : ` { "name":"secret","roles":[ { "name":"viewer","organization":"0"}],"provider":"auth0","scheme":"oauth2","links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Gnarly Default","defaultRole":"viewer"}],"currentOrganization": { "id":"0","name":"The Gnarly Default","defaultRole":"viewer"}} ` ,
2017-11-30 17:56:13 +00:00
} ,
{
name : "New user - New users not super admin, first user" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
2017-12-13 19:55:36 +00:00
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-12-13 19:55:36 +00:00
ConfigStore : & mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
2017-12-13 22:49:49 +00:00
SuperAdminNewUsers : false ,
2017-12-13 19:55:36 +00:00
} ,
} ,
} ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
2018-02-07 02:33:27 +00:00
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
2018-02-05 19:54:39 +00:00
} ,
} , nil
} ,
} ,
2017-11-30 17:56:13 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-30 17:56:13 +00:00
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
} ,
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-30 17:56:13 +00:00
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
} , nil
} ,
2018-01-11 21:10:27 +00:00
AllF : func ( ctx context . Context ) ( [ ] chronograf . Organization , error ) {
return [ ] chronograf . Organization {
chronograf . Organization {
ID : "0" ,
Name : "The Gnarly Default" ,
DefaultRole : roles . ViewerRoleName ,
} ,
} , nil
} ,
2017-11-30 17:56:13 +00:00
} ,
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 0 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-11-30 17:56:13 +00:00
}
return nil , chronograf . ErrUserNotFound
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-02-10 00:00:27 +00:00
wantBody : ` { "name":"secret","superAdmin":true,"roles":[ { "name":"viewer","organization":"0"}],"provider":"auth0","scheme":"oauth2","links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Gnarly Default","defaultRole":"viewer"}],"currentOrganization": { "id":"0","name":"The Gnarly Default","defaultRole":"viewer"}} ` ,
2017-03-10 19:24:48 +00:00
} ,
{
name : "Error adding user" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
2017-12-13 19:55:36 +00:00
UseAuth : true ,
ConfigStore : & mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
2017-12-13 22:49:49 +00:00
SuperAdminNewUsers : false ,
2017-12-13 19:55:36 +00:00
} ,
} ,
} ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping { } , nil
} ,
} ,
2017-11-02 15:59:53 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
2017-11-02 20:47:45 +00:00
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2018-02-10 00:00:27 +00:00
ID : "0" ,
Name : "The Bad Place" ,
2017-11-02 20:47:45 +00:00
} , nil
} ,
2017-11-02 15:59:53 +00:00
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2018-02-10 00:00:27 +00:00
ID : "0" ,
Name : "The Bad Place" ,
2017-11-02 15:59:53 +00:00
} , nil
} ,
2018-01-11 21:10:27 +00:00
AllF : func ( ctx context . Context ) ( [ ] chronograf . Organization , error ) {
return [ ] chronograf . Organization {
chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . ViewerRoleName ,
} ,
} , nil
} ,
2017-11-02 15:59:53 +00:00
} ,
2017-03-10 19:24:48 +00:00
UsersStore : & mocks . UsersStore {
2017-11-30 17:56:13 +00:00
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
2017-10-18 18:17:42 +00:00
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
2017-10-18 18:45:33 +00:00
return nil , chronograf . ErrUserNotFound
2017-03-10 19:24:48 +00:00
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "why Heavy?" )
2017-03-10 19:24:48 +00:00
} ,
2017-11-02 23:13:51 +00:00
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
2017-03-10 19:24:48 +00:00
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-03-10 19:24:48 +00:00
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
2017-10-24 23:17:59 +00:00
Issuer : "heroku" ,
2017-03-10 19:24:48 +00:00
} ,
2018-02-09 19:42:07 +00:00
wantStatus : http . StatusForbidden ,
2017-03-10 19:24:48 +00:00
wantContentType : "application/json" ,
2018-02-09 19:42:07 +00:00
wantBody : ` { "code":403,"message":"This Chronograf is private. To gain access, you must be explicitly added by an administrator."} ` ,
2017-03-10 19:24:48 +00:00
} ,
{
name : "No Auth" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
2017-12-13 19:55:36 +00:00
UseAuth : false ,
ConfigStore : & mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
2017-12-13 22:49:49 +00:00
SuperAdminNewUsers : false ,
2017-12-13 19:55:36 +00:00
} ,
} ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-03-10 19:24:48 +00:00
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-02-09 19:42:07 +00:00
wantBody : ` { "links": { "self":"/chronograf/v1/me"}} ` ,
2017-03-10 19:24:48 +00:00
} ,
{
name : "Empty Principal" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
2017-12-13 19:55:36 +00:00
UseAuth : true ,
ConfigStore : & mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
2017-12-13 22:49:49 +00:00
SuperAdminNewUsers : false ,
2017-12-13 19:55:36 +00:00
} ,
} ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-03-10 19:24:48 +00:00
} ,
wantStatus : http . StatusUnprocessableEntity ,
principal : oauth2 . Principal {
Subject : "" ,
2017-10-17 00:30:23 +00:00
Issuer : "" ,
2017-03-10 19:24:48 +00:00
} ,
} ,
2017-11-10 21:15:29 +00:00
{
2018-03-23 22:45:11 +00:00
name : "new user - Chronograf is private" ,
2017-11-10 21:15:29 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-02-09 19:42:07 +00:00
ConfigStore : mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
SuperAdminNewUsers : false ,
} ,
} ,
} ,
2018-02-05 19:54:39 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping { } , nil
} ,
} ,
2017-11-10 21:15:29 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 02:08:50 +00:00
Name : "The Bad Place" ,
2017-11-11 02:23:41 +00:00
DefaultRole : roles . MemberRoleName ,
2017-11-10 21:15:29 +00:00
} , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
2017-11-30 17:56:13 +00:00
NumF : func ( ctx context . Context ) ( int , error ) {
2017-11-10 21:15:29 +00:00
// This function gets to verify that there is at least one first user
2017-11-30 17:56:13 +00:00
return 1 , nil
2017-11-10 21:15:29 +00:00
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-11-10 21:15:29 +00:00
}
return nil , chronograf . ErrUserNotFound
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
} ,
wantStatus : http . StatusForbidden ,
wantContentType : "application/json" ,
2018-02-09 19:42:07 +00:00
wantBody : ` { "code":403,"message":"This Chronograf is private. To gain access, you must be explicitly added by an administrator."} ` ,
2017-11-10 21:15:29 +00:00
} ,
2018-03-23 20:12:18 +00:00
{
2018-03-23 22:45:11 +00:00
name : "new user - Chronograf is private, user is in auth0 superadmin group" ,
2018-03-23 20:12:18 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
SuperAdminProviderGroups : superAdminProviderGroups {
auth0 : "example" ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-03-23 20:12:18 +00:00
ConfigStore : mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
SuperAdminNewUsers : false ,
} ,
} ,
} ,
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping { } , nil
} ,
} ,
OrganizationsStore : & mocks . OrganizationsStore {
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2018-03-23 22:54:46 +00:00
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
2018-03-23 20:12:18 +00:00
} , nil
} ,
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2018-03-23 20:12:18 +00:00
}
return nil , chronograf . ErrUserNotFound
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
2018-03-23 22:00:09 +00:00
Group : "not_example,example" ,
2018-03-23 20:12:18 +00:00
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-03-23 22:54:46 +00:00
wantBody : ` { "name":"secret","roles":[ { "name":"member","organization":"0"}],"provider":"auth0","scheme":"oauth2","superAdmin":true,"links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Bad Place","defaultRole":"member"}],"currentOrganization": { "id":"0","name":"The Bad Place","defaultRole":"member"}} ` ,
2018-03-23 20:12:18 +00:00
} ,
{
2018-03-23 22:45:11 +00:00
name : "new user - Chronograf is private, user is not in auth0 superadmin group" ,
2018-03-23 20:12:18 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
SuperAdminProviderGroups : superAdminProviderGroups {
auth0 : "example" ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-03-23 20:12:18 +00:00
ConfigStore : mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
SuperAdminNewUsers : false ,
} ,
} ,
} ,
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping { } , nil
} ,
} ,
OrganizationsStore : & mocks . OrganizationsStore {
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2018-03-23 22:54:46 +00:00
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
2018-03-23 20:12:18 +00:00
} , nil
} ,
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2018-03-23 20:12:18 +00:00
}
return nil , chronograf . ErrUserNotFound
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
Group : "not_example" ,
} ,
wantStatus : http . StatusForbidden ,
wantContentType : "application/json" ,
wantBody : ` { "code":403,"message":"This Chronograf is private. To gain access, you must be explicitly added by an administrator."} ` ,
} ,
2018-03-23 23:26:20 +00:00
{
name : "new user - Chronograf is not private, user is in auth0 superadmin group" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
SuperAdminProviderGroups : superAdminProviderGroups {
auth0 : "example" ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-03-23 23:26:20 +00:00
ConfigStore : mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
SuperAdminNewUsers : false ,
} ,
} ,
} ,
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
} ,
} , nil
} ,
} ,
OrganizationsStore : & mocks . OrganizationsStore {
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2018-03-23 23:26:20 +00:00
}
return nil , chronograf . ErrUserNotFound
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
Group : "example" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
wantBody : ` { "name":"secret","roles":[ { "name":"member","organization":"0"}],"provider":"auth0","scheme":"oauth2","superAdmin":true,"links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Bad Place","defaultRole":"member"}],"currentOrganization": { "id":"0","name":"The Bad Place","defaultRole":"member"}} ` ,
} ,
2018-03-23 20:12:18 +00:00
{
2018-03-23 22:54:46 +00:00
name : "new user - Chronograf is not private (has a fully open wildcard mapping to an org), user is not in auth0 superadmin group" ,
2018-03-23 20:12:18 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
SuperAdminProviderGroups : superAdminProviderGroups {
auth0 : "example" ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-03-23 20:12:18 +00:00
ConfigStore : mocks . ConfigStore {
Config : & chronograf . Config {
Auth : chronograf . AuthConfig {
SuperAdminNewUsers : false ,
} ,
} ,
} ,
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
} ,
} , nil
} ,
} ,
OrganizationsStore : & mocks . OrganizationsStore {
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2018-03-23 22:54:46 +00:00
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
2018-03-23 20:12:18 +00:00
} , nil
} ,
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2018-03-23 20:12:18 +00:00
}
return nil , chronograf . ErrUserNotFound
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
Group : "not_example" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-03-23 22:54:46 +00:00
wantBody : ` { "name":"secret","roles":[ { "name":"member","organization":"0"}],"provider":"auth0","scheme":"oauth2","links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Bad Place","defaultRole":"member"}],"currentOrganization": { "id":"0","name":"The Bad Place","defaultRole":"member"}} ` ,
2018-03-23 23:53:08 +00:00
} ,
{
2018-03-23 23:58:09 +00:00
name : "Existing user - Chronograf is not private, user doesn't have SuperAdmin status, user is in auth0 superadmin group" ,
2018-03-23 23:53:08 +00:00
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
SuperAdminProviderGroups : superAdminProviderGroups {
auth0 : "example" ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-03-23 23:53:08 +00:00
ConfigStore : mocks . ConfigStore {
2018-03-23 23:58:09 +00:00
Config : & chronograf . Config { } ,
} ,
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
} ,
} , nil
} ,
} ,
OrganizationsStore : & mocks . OrganizationsStore {
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
2018-03-23 23:53:08 +00:00
} ,
} ,
2018-03-23 23:58:09 +00:00
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2018-03-23 23:58:09 +00:00
}
return & chronograf . User {
Name : "secret" ,
Provider : "auth0" ,
Scheme : "oauth2" ,
Roles : [ ] chronograf . Role {
{
Name : roles . MemberRoleName ,
Organization : "0" ,
} ,
} ,
} , nil
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
Group : "example" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
wantBody : ` { "name":"secret","roles":[ { "name":"member","organization":"0"}],"provider":"auth0","scheme":"oauth2","superAdmin":true,"links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Bad Place","defaultRole":"member"}],"currentOrganization": { "id":"0","name":"The Bad Place","defaultRole":"member"}} ` ,
} ,
{
name : "Existing user - Chronograf is not private, user has SuperAdmin status, user is in auth0 superadmin group" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
} ,
fields : fields {
UseAuth : true ,
SuperAdminProviderGroups : superAdminProviderGroups {
auth0 : "example" ,
} ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2018-03-23 23:58:09 +00:00
ConfigStore : mocks . ConfigStore {
Config : & chronograf . Config { } ,
} ,
2018-03-23 23:53:08 +00:00
MappingsStore : & mocks . MappingsStore {
AllF : func ( ctx context . Context ) ( [ ] chronograf . Mapping , error ) {
return [ ] chronograf . Mapping {
{
Organization : "0" ,
Provider : chronograf . MappingWildcard ,
Scheme : chronograf . MappingWildcard ,
ProviderOrganization : chronograf . MappingWildcard ,
} ,
} , nil
} ,
} ,
OrganizationsStore : & mocks . OrganizationsStore {
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
ID : "0" ,
Name : "The Bad Place" ,
DefaultRole : roles . MemberRoleName ,
} , nil
} ,
} ,
UsersStore : & mocks . UsersStore {
NumF : func ( ctx context . Context ) ( int , error ) {
// This function gets to verify that there is at least one first user
return 1 , nil
} ,
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2018-03-23 23:53:08 +00:00
}
return & chronograf . User {
Name : "secret" ,
Provider : "auth0" ,
Scheme : "oauth2" ,
Roles : [ ] chronograf . Role {
{
Name : roles . MemberRoleName ,
Organization : "0" ,
} ,
} ,
2018-03-23 23:58:09 +00:00
SuperAdmin : true ,
2018-03-23 23:53:08 +00:00
} , nil
} ,
AddF : func ( ctx context . Context , u * chronograf . User ) ( * chronograf . User , error ) {
return u , nil
} ,
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "secret" ,
Issuer : "auth0" ,
Group : "example" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
wantBody : ` { "name":"secret","roles":[ { "name":"member","organization":"0"}],"provider":"auth0","scheme":"oauth2","superAdmin":true,"links": { "self":"/chronograf/v1/organizations/0/users/0"},"organizations":[ { "id":"0","name":"The Bad Place","defaultRole":"member"}],"currentOrganization": { "id":"0","name":"The Bad Place","defaultRole":"member"}} ` ,
2018-03-23 20:12:18 +00:00
} ,
2017-03-10 19:24:48 +00:00
}
for _ , tt := range tests {
tt . args . r = tt . args . r . WithContext ( context . WithValue ( context . Background ( ) , oauth2 . PrincipalKey , tt . principal ) )
2017-10-26 22:01:20 +00:00
s := & Service {
2017-10-31 20:41:17 +00:00
Store : & mocks . Store {
2017-11-02 15:59:53 +00:00
UsersStore : tt . fields . UsersStore ,
OrganizationsStore : tt . fields . OrganizationsStore ,
2018-02-05 19:54:39 +00:00
MappingsStore : tt . fields . MappingsStore ,
2017-12-13 19:55:36 +00:00
ConfigStore : tt . fields . ConfigStore ,
2017-10-31 20:41:17 +00:00
} ,
2018-03-23 20:12:18 +00:00
Logger : tt . fields . Logger ,
UseAuth : tt . fields . UseAuth ,
SuperAdminProviderGroups : tt . fields . SuperAdminProviderGroups ,
2017-03-10 19:24:48 +00:00
}
2017-10-26 22:01:20 +00:00
s . Me ( tt . args . w , tt . args . r )
2017-03-10 19:24:48 +00:00
resp := tt . args . w . Result ( )
content := resp . Header . Get ( "Content-Type" )
body , _ := ioutil . ReadAll ( resp . Body )
if resp . StatusCode != tt . wantStatus {
t . Errorf ( "%q. Me() = %v, want %v" , tt . name , resp . StatusCode , tt . wantStatus )
}
if tt . wantContentType != "" && content != tt . wantContentType {
t . Errorf ( "%q. Me() = %v, want %v" , tt . name , content , tt . wantContentType )
}
2017-11-02 23:13:51 +00:00
if tt . wantBody == "" {
continue
}
if eq , err := jsonEqual ( tt . wantBody , string ( body ) ) ; err != nil || ! eq {
2017-03-10 19:24:48 +00:00
t . Errorf ( "%q. Me() = \n***%v***\n,\nwant\n***%v***" , tt . name , string ( body ) , tt . wantBody )
}
}
}
2017-10-26 23:02:57 +00:00
2017-11-10 21:17:46 +00:00
func TestService_UpdateMe ( t * testing . T ) {
2017-10-26 23:02:57 +00:00
type fields struct {
2017-10-31 20:41:17 +00:00
UsersStore chronograf . UsersStore
OrganizationsStore chronograf . OrganizationsStore
Logger chronograf . Logger
UseAuth bool
2017-10-26 23:02:57 +00:00
}
type args struct {
2017-11-14 02:08:50 +00:00
w * httptest . ResponseRecorder
r * http . Request
meRequest * meRequest
auth mocks . Authenticator
2017-10-26 23:02:57 +00:00
}
tests := [ ] struct {
name string
fields fields
args args
principal oauth2 . Principal
wantStatus int
wantContentType string
wantBody string
} {
{
name : "Set the current User's organization" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
2017-11-14 02:08:50 +00:00
meRequest : & meRequest {
2017-11-01 14:37:32 +00:00
Organization : "1337" ,
2017-10-26 23:02:57 +00:00
} ,
auth : mocks . Authenticator { } ,
} ,
fields : fields {
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-10-26 23:02:57 +00:00
UsersStore : & mocks . UsersStore {
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-10-26 23:02:57 +00:00
}
return & chronograf . User {
Name : "me" ,
Provider : "github" ,
Scheme : "oauth2" ,
2017-10-31 20:41:17 +00:00
Roles : [ ] chronograf . Role {
{
2017-11-03 20:32:05 +00:00
Name : roles . AdminRoleName ,
2017-10-31 20:41:17 +00:00
Organization : "1337" ,
} ,
} ,
2017-10-26 23:02:57 +00:00
} , nil
} ,
2017-11-02 23:13:51 +00:00
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
2017-10-26 23:02:57 +00:00
} ,
2017-10-27 17:02:02 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
2017-11-02 20:47:45 +00:00
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 03:28:15 +00:00
Name : "Default" ,
DefaultRole : roles . AdminRoleName ,
2017-11-02 20:47:45 +00:00
} , nil
} ,
2017-10-27 17:02:02 +00:00
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
if q . ID == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid organization query: missing ID" )
2017-10-27 17:02:02 +00:00
}
2017-11-08 21:56:34 +00:00
switch * q . ID {
2017-12-18 22:07:40 +00:00
case "0" :
2017-11-08 21:56:34 +00:00
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 03:28:15 +00:00
Name : "Default" ,
DefaultRole : roles . AdminRoleName ,
2017-11-08 21:56:34 +00:00
} , nil
2017-12-18 22:07:40 +00:00
case "1337" :
2017-11-08 21:56:34 +00:00
return & chronograf . Organization {
2018-02-10 00:00:27 +00:00
ID : "1337" ,
Name : "The ShillBillThrilliettas" ,
2017-11-08 21:56:34 +00:00
} , nil
}
return nil , nil
2017-10-27 17:02:02 +00:00
} ,
} ,
2017-10-26 23:02:57 +00:00
} ,
principal : oauth2 . Principal {
Subject : "me" ,
Issuer : "github" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-02-10 00:00:27 +00:00
wantBody : ` { "name":"me","roles":[ { "name":"admin","organization":"1337"}],"provider":"github","scheme":"oauth2","links": { "self":"/chronograf/v1/organizations/1337/users/0"},"organizations":[ { "id":"1337","name":"The ShillBillThrilliettas"}],"currentOrganization": { "id":"1337","name":"The ShillBillThrilliettas"}} ` ,
2017-10-26 23:02:57 +00:00
} ,
{
name : "Change the current User's organization" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
2017-11-14 02:08:50 +00:00
meRequest : & meRequest {
2017-11-01 14:37:32 +00:00
Organization : "1337" ,
2017-10-26 23:02:57 +00:00
} ,
auth : mocks . Authenticator { } ,
} ,
fields : fields {
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-10-26 23:02:57 +00:00
UsersStore : & mocks . UsersStore {
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-10-26 23:02:57 +00:00
}
return & chronograf . User {
Name : "me" ,
Provider : "github" ,
Scheme : "oauth2" ,
2017-10-31 20:41:17 +00:00
Roles : [ ] chronograf . Role {
{
2017-11-03 20:32:05 +00:00
Name : roles . AdminRoleName ,
2017-10-31 20:41:17 +00:00
Organization : "1337" ,
} ,
} ,
2017-10-26 23:02:57 +00:00
} , nil
} ,
2017-11-02 23:13:51 +00:00
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
2017-10-26 23:02:57 +00:00
} ,
2017-10-27 17:02:02 +00:00
OrganizationsStore : & mocks . OrganizationsStore {
2017-11-02 20:47:45 +00:00
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 03:28:15 +00:00
Name : "Default" ,
DefaultRole : roles . EditorRoleName ,
2017-11-02 20:47:45 +00:00
} , nil
} ,
2017-10-27 17:02:02 +00:00
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
if q . ID == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid organization query: missing ID" )
2017-10-27 17:02:02 +00:00
}
2017-11-08 21:56:34 +00:00
switch * q . ID {
2017-12-18 22:07:40 +00:00
case "1337" :
2017-11-08 21:56:34 +00:00
return & chronograf . Organization {
2018-02-10 00:00:27 +00:00
ID : "1337" ,
Name : "The ThrillShilliettos" ,
2017-11-08 21:56:34 +00:00
} , nil
2017-12-18 22:07:40 +00:00
case "0" :
2017-11-08 21:56:34 +00:00
return & chronograf . Organization {
2017-12-18 22:07:40 +00:00
ID : "0" ,
2017-11-14 03:28:15 +00:00
Name : "Default" ,
DefaultRole : roles . EditorRoleName ,
2017-11-08 21:56:34 +00:00
} , nil
}
return nil , nil
2017-10-27 17:02:02 +00:00
} ,
} ,
2017-10-26 23:02:57 +00:00
} ,
principal : oauth2 . Principal {
Subject : "me" ,
Issuer : "github" ,
Organization : "1338" ,
} ,
wantStatus : http . StatusOK ,
wantContentType : "application/json" ,
2018-02-10 00:00:27 +00:00
wantBody : ` { "name":"me","roles":[ { "name":"admin","organization":"1337"}],"provider":"github","scheme":"oauth2","links": { "self":"/chronograf/v1/organizations/1337/users/0"},"organizations":[ { "id":"1337","name":"The ThrillShilliettos"}],"currentOrganization": { "id":"1337","name":"The ThrillShilliettos"}} ` ,
2017-10-26 23:02:57 +00:00
} ,
2017-10-27 17:14:14 +00:00
{
name : "Unable to find requested user in valid organization" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
2017-11-14 02:08:50 +00:00
meRequest : & meRequest {
2017-11-01 14:37:32 +00:00
Organization : "1337" ,
2017-10-27 17:14:14 +00:00
} ,
auth : mocks . Authenticator { } ,
} ,
fields : fields {
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-10-27 17:14:14 +00:00
UsersStore : & mocks . UsersStore {
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-10-27 17:14:14 +00:00
}
2017-11-08 21:56:34 +00:00
return & chronograf . User {
Name : "me" ,
Provider : "github" ,
Scheme : "oauth2" ,
Roles : [ ] chronograf . Role {
{
Name : roles . AdminRoleName ,
Organization : "1338" ,
} ,
} ,
} , nil
2017-10-27 17:14:14 +00:00
} ,
2017-11-02 23:13:51 +00:00
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
2017-10-27 17:14:14 +00:00
} ,
OrganizationsStore : & mocks . OrganizationsStore {
2017-11-02 20:47:45 +00:00
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2018-02-05 19:54:39 +00:00
ID : "0" ,
2017-11-02 20:47:45 +00:00
} , nil
} ,
2017-10-27 17:14:14 +00:00
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
if q . ID == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid organization query: missing ID" )
2017-10-27 17:14:14 +00:00
}
return & chronograf . Organization {
2018-02-10 00:00:27 +00:00
ID : "1337" ,
Name : "The ShillBillThrilliettas" ,
2017-10-27 17:14:14 +00:00
} , nil
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "me" ,
Issuer : "github" ,
Organization : "1338" ,
} ,
2017-11-10 21:17:46 +00:00
wantStatus : http . StatusForbidden ,
2017-10-27 17:14:14 +00:00
wantContentType : "application/json" ,
2017-11-10 21:17:46 +00:00
wantBody : ` { "code":403,"message":"user not found"} ` ,
2017-10-27 17:14:14 +00:00
} ,
{
name : "Unable to find requested organization" ,
args : args {
w : httptest . NewRecorder ( ) ,
r : httptest . NewRequest ( "GET" , "http://example.com/foo" , nil ) ,
2017-11-14 02:08:50 +00:00
meRequest : & meRequest {
2017-11-01 14:37:32 +00:00
Organization : "1337" ,
2017-10-27 17:14:14 +00:00
} ,
auth : mocks . Authenticator { } ,
} ,
fields : fields {
UseAuth : true ,
2018-11-16 16:45:00 +00:00
Logger : & chronograf . NoopLogger { } ,
2017-10-27 17:14:14 +00:00
UsersStore : & mocks . UsersStore {
GetF : func ( ctx context . Context , q chronograf . UserQuery ) ( * chronograf . User , error ) {
if q . Name == nil || q . Provider == nil || q . Scheme == nil {
2018-11-21 14:22:35 +00:00
return nil , fmt . Errorf ( "invalid user query: missing Name, Provider, and/or Scheme" )
2017-10-27 17:14:14 +00:00
}
return & chronograf . User {
Name : "me" ,
Provider : "github" ,
Scheme : "oauth2" ,
2017-10-31 20:41:17 +00:00
Roles : [ ] chronograf . Role {
{
2017-11-03 20:32:05 +00:00
Name : roles . AdminRoleName ,
2017-10-31 20:41:17 +00:00
Organization : "1337" ,
} ,
} ,
2017-10-27 17:14:14 +00:00
} , nil
} ,
2017-11-02 23:13:51 +00:00
UpdateF : func ( ctx context . Context , u * chronograf . User ) error {
return nil
} ,
2017-10-27 17:14:14 +00:00
} ,
OrganizationsStore : & mocks . OrganizationsStore {
2017-11-02 20:47:45 +00:00
DefaultOrganizationF : func ( ctx context . Context ) ( * chronograf . Organization , error ) {
return & chronograf . Organization {
2018-02-05 19:54:39 +00:00
ID : "0" ,
2017-11-02 20:47:45 +00:00
} , nil
} ,
2017-10-27 17:14:14 +00:00
GetF : func ( ctx context . Context , q chronograf . OrganizationQuery ) ( * chronograf . Organization , error ) {
return nil , chronograf . ErrOrganizationNotFound
} ,
} ,
} ,
principal : oauth2 . Principal {
Subject : "me" ,
Issuer : "github" ,
Organization : "1338" ,
} ,
wantStatus : http . StatusBadRequest ,
wantContentType : "application/json" ,
wantBody : ` { "code":400,"message":"organization not found"} ` ,
} ,
2017-10-26 23:02:57 +00:00
}
for _ , tt := range tests {
tt . args . r = tt . args . r . WithContext ( context . WithValue ( context . Background ( ) , oauth2 . PrincipalKey , tt . principal ) )
s := & Service {
2017-10-31 20:41:17 +00:00
Store : & Store {
UsersStore : tt . fields . UsersStore ,
OrganizationsStore : tt . fields . OrganizationsStore ,
} ,
Logger : tt . fields . Logger ,
UseAuth : tt . fields . UseAuth ,
2017-10-26 23:02:57 +00:00
}
2017-11-14 02:08:50 +00:00
buf , _ := json . Marshal ( tt . args . meRequest )
2017-10-26 23:02:57 +00:00
tt . args . r . Body = ioutil . NopCloser ( bytes . NewReader ( buf ) )
tt . args . auth . Principal = tt . principal
2017-11-10 21:17:46 +00:00
s . UpdateMe ( & tt . args . auth ) ( tt . args . w , tt . args . r )
2017-10-26 23:02:57 +00:00
resp := tt . args . w . Result ( )
content := resp . Header . Get ( "Content-Type" )
body , _ := ioutil . ReadAll ( resp . Body )
if resp . StatusCode != tt . wantStatus {
2017-11-10 21:17:46 +00:00
t . Errorf ( "%q. UpdateMe() = %v, want %v" , tt . name , resp . StatusCode , tt . wantStatus )
2017-10-26 23:02:57 +00:00
}
if tt . wantContentType != "" && content != tt . wantContentType {
2017-11-10 21:17:46 +00:00
t . Errorf ( "%q. UpdateMe() = %v, want %v" , tt . name , content , tt . wantContentType )
2017-10-26 23:02:57 +00:00
}
if eq , err := jsonEqual ( tt . wantBody , string ( body ) ) ; err != nil || ! eq {
2017-11-10 21:17:46 +00:00
t . Errorf ( "%q. UpdateMe() = \n***%v***\n,\nwant\n***%v***" , tt . name , string ( body ) , tt . wantBody )
2017-10-26 23:02:57 +00:00
}
}
}