2020-06-09 11:20:59 -07:00
package users_test
import (
"context"
2020-09-23 08:10:27 -07:00
"errors"
2020-06-09 11:20:59 -07:00
"fmt"
"strings"
"testing"
"github.com/golang/protobuf/ptypes"
2020-08-05 10:19:55 -07:00
"github.com/google/go-cmp/cmp"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/auth"
2020-09-25 20:44:55 -07:00
"github.com/hashicorp/boundary/internal/auth/password"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/db"
"github.com/hashicorp/boundary/internal/gen/controller/api/resources/scopes"
pb "github.com/hashicorp/boundary/internal/gen/controller/api/resources/users"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"github.com/hashicorp/boundary/internal/iam"
2020-09-23 08:10:27 -07:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers"
2020-08-10 17:15:02 -04:00
"github.com/hashicorp/boundary/internal/servers/controller/handlers/users"
"github.com/hashicorp/boundary/internal/types/scope"
2020-06-09 11:20:59 -07:00
"google.golang.org/genproto/protobuf/field_mask"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/proto"
2020-08-05 10:19:55 -07:00
"google.golang.org/protobuf/testing/protocmp"
2020-06-09 11:20:59 -07:00
"google.golang.org/protobuf/types/known/wrapperspb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func createDefaultUserAndRepo ( t * testing . T ) ( * iam . User , func () ( * iam . Repository , error )) {
t . Helper ()
2020-07-06 10:15:01 -07:00
conn , _ := db . TestSetup ( t , "postgres" )
2020-06-09 11:20:59 -07:00
wrap := db . TestWrapper ( t )
2020-08-19 16:47:18 -04:00
repo := iam . TestRepo ( t , conn , wrap )
2020-06-09 11:20:59 -07:00
repoFn := func () ( * iam . Repository , error ) {
2020-08-19 16:47:18 -04:00
return repo , nil
2020-06-09 11:20:59 -07:00
}
2020-08-19 16:47:18 -04:00
o , _ := iam . TestScopes ( t , repo )
u := iam . TestUser ( t , repo , o . GetPublicId (), iam . WithDescription ( "default" ), iam . WithName ( "default" ))
2020-06-09 11:20:59 -07:00
return u , repoFn
}
func TestGet ( t * testing . T ) {
2020-08-19 16:47:18 -04:00
u , repoFn := createDefaultUserAndRepo ( t )
2020-06-09 11:20:59 -07:00
toMerge := & pbs . GetUserRequest {
2020-07-22 00:14:28 -04:00
Id : u . GetPublicId (),
2020-06-09 11:20:59 -07:00
}
wantU := & pb . User {
Id : u . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : u . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : u . ScopeId , Type : scope . Org . String ()},
2020-06-09 11:20:59 -07:00
Name : & wrapperspb . StringValue { Value : u . GetName ()},
Description : & wrapperspb . StringValue { Value : u . GetDescription ()},
CreatedTime : u . CreateTime . GetTimestamp (),
UpdatedTime : u . UpdateTime . GetTimestamp (),
2020-08-07 12:55:02 -04:00
Version : 1 ,
2020-06-09 11:20:59 -07:00
}
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . GetUserRequest
res * pbs . GetUserResponse
err error
2020-06-09 11:20:59 -07:00
}{
{
2020-09-23 08:10:27 -07:00
name : "Get an Existing User" ,
req : & pbs . GetUserRequest { Id : u . GetPublicId ()},
res : & pbs . GetUserResponse { Item : wantU },
2020-06-09 11:20:59 -07:00
},
{
2020-09-23 08:10:27 -07:00
name : "Get a non existant User" ,
req : & pbs . GetUserRequest { Id : iam . UserPrefix + "_DoesntExis" },
res : nil ,
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-06-09 11:20:59 -07:00
},
{
2020-09-23 08:10:27 -07:00
name : "Wrong id prefix" ,
req : & pbs . GetUserRequest { Id : "j_1234567890" },
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
2020-09-23 08:10:27 -07:00
name : "space in id" ,
req : & pbs . GetUserRequest { Id : iam . UserPrefix + "_1 23456789" },
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-25 20:44:55 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-06-09 11:20:59 -07:00
req := proto . Clone ( toMerge ).( * pbs . GetUserRequest )
proto . Merge ( req , tc . req )
2020-08-19 16:47:18 -04:00
s , err := users . NewService ( repoFn )
2020-06-09 11:20:59 -07:00
require . NoError ( err , "Couldn't create new user service." )
2020-07-22 15:10:44 -04:00
got , gErr := s . GetUser ( auth . DisabledAuthTestContext ( auth . WithScopeId ( u . GetScopeId ())), req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "GetUser(%+v) got error %v, wanted %v" , req , gErr , tc . err )
}
2020-08-05 10:19:55 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "GetUser(%q) got response %q, wanted %q" , req , got , tc . res )
2020-06-09 11:20:59 -07:00
})
}
}
2020-07-02 08:17:52 -07:00
func TestList ( t * testing . T ) {
2020-07-06 10:15:01 -07:00
conn , _ := db . TestSetup ( t , "postgres" )
2020-07-02 08:17:52 -07:00
wrap := db . TestWrapper ( t )
2020-08-19 16:47:18 -04:00
iamRepo := iam . TestRepo ( t , conn , wrap )
2020-07-02 08:17:52 -07:00
repoFn := func () ( * iam . Repository , error ) {
2020-08-19 16:47:18 -04:00
return iamRepo , nil
2020-07-02 08:17:52 -07:00
}
repo , err := repoFn ()
2020-09-25 20:44:55 -07:00
require . NoError ( t , err )
2020-07-02 08:17:52 -07:00
2020-08-19 16:47:18 -04:00
oNoUsers , _ := iam . TestScopes ( t , repo )
oWithUsers , _ := iam . TestScopes ( t , repo )
2020-07-02 08:17:52 -07:00
var wantUsers [] * pb . User
for i := 0 ; i < 10 ; i ++ {
newU , err := iam . NewUser ( oWithUsers . GetPublicId ())
2020-09-25 20:44:55 -07:00
require . NoError ( t , err )
2020-07-02 08:17:52 -07:00
u , err := repo . CreateUser ( context . Background (), newU )
2020-09-25 20:44:55 -07:00
require . NoError ( t , err )
2020-07-02 08:17:52 -07:00
wantUsers = append ( wantUsers , & pb . User {
Id : u . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : u . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : u . GetScopeId (), Type : scope . Org . String ()},
2020-07-02 08:17:52 -07:00
CreatedTime : u . GetCreateTime (). GetTimestamp (),
UpdatedTime : u . GetUpdateTime (). GetTimestamp (),
2020-08-07 12:55:02 -04:00
Version : 1 ,
2020-07-02 08:17:52 -07:00
})
}
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . ListUsersRequest
res * pbs . ListUsersResponse
err error
2020-07-02 08:17:52 -07:00
}{
{
2020-09-23 08:10:27 -07:00
name : "List Many Users" ,
req : & pbs . ListUsersRequest { ScopeId : oWithUsers . GetPublicId ()},
res : & pbs . ListUsersResponse { Items : wantUsers },
2020-07-02 08:17:52 -07:00
},
{
2020-09-23 08:10:27 -07:00
name : "List No Users" ,
req : & pbs . ListUsersRequest { ScopeId : oNoUsers . GetPublicId ()},
res : & pbs . ListUsersResponse {},
2020-07-02 08:17:52 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-25 20:44:55 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-07-02 08:17:52 -07:00
s , err := users . NewService ( repoFn )
require . NoError ( err , "Couldn't create new user service." )
2020-09-02 19:43:41 -07:00
got , gErr := s . ListUsers ( auth . DisabledAuthTestContext ( auth . WithScopeId ( tc . req . GetScopeId ())), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "ListUsers(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-08-05 10:19:55 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "ListUsers(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2020-07-02 08:17:52 -07:00
})
}
}
2020-06-09 11:20:59 -07:00
func TestDelete ( t * testing . T ) {
u , repo := createDefaultUserAndRepo ( t )
s , err := users . NewService ( repo )
2020-09-23 08:10:27 -07:00
require . NoError ( t , err , "Error when getting new user service." )
2020-06-09 11:20:59 -07:00
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . DeleteUserRequest
res * pbs . DeleteUserResponse
err error
2020-06-09 11:20:59 -07:00
}{
{
name : "Delete an Existing User" ,
req : & pbs . DeleteUserRequest {
2020-07-22 00:14:28 -04:00
Id : u . GetPublicId (),
2020-06-09 11:20:59 -07:00
},
},
{
name : "Delete bad user id" ,
req : & pbs . DeleteUserRequest {
2020-07-22 00:14:28 -04:00
Id : iam . UserPrefix + "_doesntexis" ,
2020-06-09 11:20:59 -07:00
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-06-09 11:20:59 -07:00
},
{
name : "Bad User Id formatting" ,
req : & pbs . DeleteUserRequest {
2020-07-22 00:14:28 -04:00
Id : "bad_format" ,
2020-06-09 11:20:59 -07:00
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-07-22 15:10:44 -04:00
got , gErr := s . DeleteUser ( auth . DisabledAuthTestContext ( auth . WithScopeId ( u . GetScopeId ())), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "DeleteUser(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-06-09 11:20:59 -07:00
assert . EqualValuesf ( tc . res , got , "DeleteUser(%q) got response %q, wanted %q" , tc . req , got , tc . res )
})
}
}
func TestDelete_twice ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-06-09 11:20:59 -07:00
u , repo := createDefaultUserAndRepo ( t )
s , err := users . NewService ( repo )
require . NoError ( err , "Error when getting new user service" )
req := & pbs . DeleteUserRequest {
2020-07-22 00:14:28 -04:00
Id : u . GetPublicId (),
2020-06-09 11:20:59 -07:00
}
2020-07-22 15:10:44 -04:00
ctx := auth . DisabledAuthTestContext ( auth . WithScopeId ( u . GetScopeId ()))
2020-09-08 23:57:26 -04:00
_ , gErr := s . DeleteUser ( ctx , req )
2020-06-09 11:20:59 -07:00
assert . NoError ( gErr , "First attempt" )
2020-09-08 23:57:26 -04:00
_ , gErr = s . DeleteUser ( ctx , req )
2020-09-04 14:48:11 -07:00
assert . Error ( gErr , "Second attempt" )
2020-09-23 08:10:27 -07:00
assert . True ( errors . Is ( gErr , handlers . ApiErrorWithCode ( codes . NotFound )), "Expected permission denied for the second delete." )
2020-06-09 11:20:59 -07:00
}
func TestCreate ( t * testing . T ) {
defaultUser , repo := createDefaultUserAndRepo ( t )
defaultCreated , err := ptypes . Timestamp ( defaultUser . GetCreateTime (). GetTimestamp ())
2020-09-23 08:10:27 -07:00
require . NoError ( t , err , "Error converting proto to timestamp." )
2020-06-09 11:20:59 -07:00
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . CreateUserRequest
res * pbs . CreateUserResponse
err error
2020-06-09 11:20:59 -07:00
}{
{
name : "Create a valid User" ,
req : & pbs . CreateUserRequest { Item : & pb . User {
2020-09-02 19:43:41 -07:00
ScopeId : defaultUser . GetScopeId (),
2020-06-09 11:20:59 -07:00
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
}},
res : & pbs . CreateUserResponse {
2020-09-02 19:43:41 -07:00
Uri : fmt . Sprintf ( "users/%s_" , iam . UserPrefix ),
2020-06-09 11:20:59 -07:00
Item : & pb . User {
2020-09-02 19:43:41 -07:00
ScopeId : defaultUser . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : defaultUser . GetScopeId (), Type : scope . Org . String ()},
2020-06-09 11:20:59 -07:00
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
2020-08-07 12:55:02 -04:00
Version : 1 ,
2020-06-09 11:20:59 -07:00
},
},
},
2020-09-03 08:02:30 -07:00
{
name : "Create a valid Global User" ,
req : & pbs . CreateUserRequest { Item : & pb . User {
ScopeId : scope . Global . String (),
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
}},
res : & pbs . CreateUserResponse {
Uri : fmt . Sprintf ( "users/%s_" , iam . UserPrefix ),
Item : & pb . User {
ScopeId : scope . Global . String (),
Scope : & scopes . ScopeInfo { Id : scope . Global . String (), Type : scope . Global . String ()},
Name : & wrapperspb . StringValue { Value : "name" },
Description : & wrapperspb . StringValue { Value : "desc" },
Version : 1 ,
},
},
},
2020-06-09 11:20:59 -07:00
{
name : "Can't specify Id" ,
req : & pbs . CreateUserRequest { Item : & pb . User {
2020-09-02 19:43:41 -07:00
ScopeId : defaultUser . GetScopeId (),
Id : iam . UserPrefix + "_notallowed" ,
2020-06-09 11:20:59 -07:00
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
name : "Can't specify Created Time" ,
req : & pbs . CreateUserRequest { Item : & pb . User {
2020-09-02 19:43:41 -07:00
ScopeId : defaultUser . GetScopeId (),
2020-06-09 11:20:59 -07:00
CreatedTime : ptypes . TimestampNow (),
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
name : "Can't specify Update Time" ,
req : & pbs . CreateUserRequest { Item : & pb . User {
2020-09-02 19:43:41 -07:00
ScopeId : defaultUser . GetScopeId (),
2020-06-09 11:20:59 -07:00
UpdatedTime : ptypes . TimestampNow (),
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-09-23 08:10:27 -07:00
assert , require := assert . New ( t ), require . New ( t )
2020-06-09 11:20:59 -07:00
s , err := users . NewService ( repo )
require . NoError ( err , "Error when getting new user service." )
2020-09-03 08:02:30 -07:00
got , gErr := s . CreateUser ( auth . DisabledAuthTestContext ( auth . WithScopeId ( tc . req . GetItem (). GetScopeId ())), tc . req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "CreateUser(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
2020-06-09 11:20:59 -07:00
if got != nil {
2020-08-05 10:19:55 -07:00
assert . Contains ( got . GetUri (), tc . res . Uri )
2020-06-09 11:20:59 -07:00
assert . True ( strings . HasPrefix ( got . GetItem (). GetId (), iam . UserPrefix + "_" ))
gotCreateTime , err := ptypes . Timestamp ( got . GetItem (). GetCreatedTime ())
require . NoError ( err , "Error converting proto to timestamp." )
gotUpdateTime , err := ptypes . Timestamp ( got . GetItem (). GetUpdatedTime ())
require . NoError ( err , "Error converting proto to timestamp." )
// Verify it is a user created after the test setup's default user
assert . True ( gotCreateTime . After ( defaultCreated ), "New user should have been created after default user. Was created %v, which is after %v" , gotCreateTime , defaultCreated )
assert . True ( gotUpdateTime . After ( defaultCreated ), "New user should have been updated after default user. Was updated %v, which is after %v" , gotUpdateTime , defaultCreated )
// Clear all values which are hard to compare against.
got . Uri , tc . res . Uri = "" , ""
got . Item . Id , tc . res . Item . Id = "" , ""
got . Item . CreatedTime , got . Item . UpdatedTime , tc . res . Item . CreatedTime , tc . res . Item . UpdatedTime = nil , nil , nil , nil
}
2020-09-02 19:43:41 -07:00
assert . Empty ( cmp . Diff ( got , tc . res , protocmp . Transform ()), "CreateUser(%q) got response %q, wanted %q" , tc . req , got , tc . res )
2020-06-09 11:20:59 -07:00
})
}
}
func TestUpdate ( t * testing . T ) {
u , repoFn := createDefaultUserAndRepo ( t )
tested , err := users . NewService ( repoFn )
2020-08-07 12:55:02 -04:00
require . NoError ( t , err , "Error when getting new user service." )
2020-06-09 11:20:59 -07:00
created , err := ptypes . Timestamp ( u . GetCreateTime (). GetTimestamp ())
2020-08-07 12:55:02 -04:00
require . NoError ( t , err , "Error converting proto to timestamp" )
2020-06-09 11:20:59 -07:00
toMerge := & pbs . UpdateUserRequest {
2020-07-22 00:14:28 -04:00
Id : u . GetPublicId (),
2020-06-09 11:20:59 -07:00
}
2020-08-07 12:55:02 -04:00
var version uint32 = 1
resetUser := func () {
repo , err := repoFn ()
require . NoError ( t , err , "Couldn't get a new repo" )
version ++ // From the test case that resulted in calling this
2020-09-25 13:35:32 -04:00
u , _ , _ , err = repo . UpdateUser ( context . Background (), u , version , [] string { "Name" , "Description" })
2020-08-07 12:55:02 -04:00
require . NoError ( t , err , "Failed to reset the user" )
version ++
}
2020-06-09 11:20:59 -07:00
cases := [] struct {
2020-09-23 08:10:27 -07:00
name string
req * pbs . UpdateUserRequest
res * pbs . UpdateUserResponse
err error
2020-06-09 11:20:59 -07:00
}{
{
name : "Update an Existing User" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" , "description" },
},
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
},
},
res : & pbs . UpdateUserResponse {
Item : & pb . User {
Id : u . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : u . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : u . GetScopeId (), Type : scope . Org . String ()},
2020-06-09 11:20:59 -07:00
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
CreatedTime : u . GetCreateTime (). GetTimestamp (),
},
},
},
{
name : "Multiple Paths in single string" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name,description" },
},
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
},
},
res : & pbs . UpdateUserResponse {
Item : & pb . User {
Id : u . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : u . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : u . GetScopeId (), Type : scope . Org . String ()},
2020-06-09 11:20:59 -07:00
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
CreatedTime : u . GetCreateTime (). GetTimestamp (),
},
},
},
{
name : "No Update Mask" ,
req : & pbs . UpdateUserRequest {
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "updated name" },
Description : & wrapperspb . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
name : "No Paths in Mask" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask { Paths : [] string {}},
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "updated name" },
Description : & wrapperspb . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
name : "Only non-existant paths in Mask" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask { Paths : [] string { "nonexistant_field" }},
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "updated name" },
Description : & wrapperspb . StringValue { Value : "updated desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
name : "Unset Name" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" },
},
Item : & pb . User {
Description : & wrapperspb . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateUserResponse {
Item : & pb . User {
Id : u . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : u . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : u . GetScopeId (), Type : scope . Org . String ()},
2020-06-09 11:20:59 -07:00
Description : & wrapperspb . StringValue { Value : "default" },
CreatedTime : u . GetCreateTime (). GetTimestamp (),
},
},
},
{
name : "Update Only Name" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "name" },
},
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "updated" },
Description : & wrapperspb . StringValue { Value : "ignored" },
},
},
res : & pbs . UpdateUserResponse {
Item : & pb . User {
Id : u . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : u . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : u . GetScopeId (), Type : scope . Org . String ()},
2020-06-09 11:20:59 -07:00
Name : & wrapperspb . StringValue { Value : "updated" },
Description : & wrapperspb . StringValue { Value : "default" },
CreatedTime : u . GetCreateTime (). GetTimestamp (),
},
},
},
{
name : "Update Only Description" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "ignored" },
Description : & wrapperspb . StringValue { Value : "notignored" },
},
},
res : & pbs . UpdateUserResponse {
Item : & pb . User {
Id : u . GetPublicId (),
2020-09-02 19:43:41 -07:00
ScopeId : u . GetScopeId (),
2020-07-22 00:14:28 -04:00
Scope : & scopes . ScopeInfo { Id : u . GetScopeId (), Type : scope . Org . String ()},
2020-06-09 11:20:59 -07:00
Name : & wrapperspb . StringValue { Value : "default" },
Description : & wrapperspb . StringValue { Value : "notignored" },
CreatedTime : u . GetCreateTime (). GetTimestamp (),
},
},
},
{
name : "Update a Non Existing User" ,
req : & pbs . UpdateUserRequest {
Id : iam . UserPrefix + "_DoesntExis" ,
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "description" },
},
Item : & pb . User {
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "desc" },
},
},
2020-09-23 08:10:27 -07:00
err : handlers . ApiErrorWithCode ( codes . NotFound ),
2020-06-09 11:20:59 -07:00
},
{
name : "Cant change Id" ,
req : & pbs . UpdateUserRequest {
Id : u . GetPublicId (),
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "id" },
},
Item : & pb . User {
Id : iam . UserPrefix + "_somethinge" ,
Name : & wrapperspb . StringValue { Value : "new" },
Description : & wrapperspb . StringValue { Value : "new desc" },
}},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
name : "Cant specify Created Time" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "created_time" },
},
Item : & pb . User {
CreatedTime : ptypes . TimestampNow (),
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
{
name : "Cant specify Updated Time" ,
req : & pbs . UpdateUserRequest {
UpdateMask : & field_mask . FieldMask {
Paths : [] string { "updated_time" },
},
Item : & pb . User {
UpdatedTime : ptypes . TimestampNow (),
},
},
2020-09-23 08:10:27 -07:00
res : nil ,
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
2020-06-09 11:20:59 -07:00
},
}
for _ , tc := range cases {
t . Run ( tc . name , func ( t * testing . T ) {
2020-08-18 17:38:38 -07:00
tc . req . Item . Version = version
2020-08-07 12:55:02 -04:00
assert , require := assert . New ( t ), require . New ( t )
2020-06-09 11:20:59 -07:00
req := proto . Clone ( toMerge ).( * pbs . UpdateUserRequest )
proto . Merge ( req , tc . req )
2020-08-07 12:55:02 -04:00
// Test with bad version (too high, too low)
2020-08-18 17:38:38 -07:00
req . Item . Version = version + 2
2020-08-07 12:55:02 -04:00
_ , gErr := tested . UpdateUser ( auth . DisabledAuthTestContext ( auth . WithScopeId ( u . GetScopeId ())), req )
require . Error ( gErr )
2020-08-18 17:38:38 -07:00
req . Item . Version = version - 1
2020-08-07 12:55:02 -04:00
_ , gErr = tested . UpdateUser ( auth . DisabledAuthTestContext ( auth . WithScopeId ( u . GetScopeId ())), req )
require . Error ( gErr )
2020-08-18 17:38:38 -07:00
req . Item . Version = version
2020-08-07 12:55:02 -04:00
2020-07-22 15:10:44 -04:00
got , gErr := tested . UpdateUser ( auth . DisabledAuthTestContext ( auth . WithScopeId ( u . GetScopeId ())), req )
2020-09-23 08:10:27 -07:00
if tc . err != nil {
require . Error ( gErr )
require . True ( errors . Is ( gErr , tc . err ), "UpdateUser(%+v) got error %v, wanted %v" , req , gErr , tc . err )
} else {
2020-08-07 12:55:02 -04:00
defer resetUser ()
}
2020-06-09 11:20:59 -07:00
if got != nil {
assert . NotNilf ( tc . res , "Expected UpdateUser response to be nil, but was %v" , got )
gotUpdateTime , err := ptypes . Timestamp ( got . GetItem (). GetUpdatedTime ())
require . NoError ( err , "Error converting proto to timestamp" )
// Verify it is a user updated after it was created
assert . True ( gotUpdateTime . After ( created ), "Updated user should have been updated after it's creation. Was updated %v, which is after %v" , gotUpdateTime , created )
// Clear all values which are hard to compare against.
got . Item . UpdatedTime , tc . res . Item . UpdatedTime = nil , nil
2020-08-07 12:55:02 -04:00
assert . Equal ( version + 1 , got . GetItem (). GetVersion ())
tc . res . Item . Version = version + 1
2020-06-09 11:20:59 -07:00
}
2020-08-07 12:55:02 -04:00
assert . Empty ( cmp . Diff ( tc . res , got , protocmp . Transform ()), "UpdateUser(%q) got response %q, wanted %q" , req , got , tc . res )
2020-06-09 11:20:59 -07:00
})
}
}
2020-09-25 20:44:55 -07:00
func TestAddAccount ( t * testing . T ) {
conn , _ := db . TestSetup ( t , "postgres" )
wrap := db . TestWrapper ( t )
iamRepo := iam . TestRepo ( t , conn , wrap )
repoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
s , err := users . NewService ( repoFn )
require . NoError ( t , err , "Error when getting new user service." )
o , _ := iam . TestScopes ( t , iamRepo )
amId := password . TestAuthMethods ( t , conn , o . GetPublicId (), 1 )[ 0 ]. GetPublicId ()
accts := password . TestAccounts ( t , conn , amId , 3 )
addCases := [] struct {
2020-09-28 15:39:10 -04:00
name string
setup func ( * iam . User )
addAccounts [] string
resultAccounts [] string
wantErr bool
2020-09-25 20:44:55 -07:00
}{
{
2020-09-28 15:39:10 -04:00
name : "Add account on empty user" ,
setup : func ( u * iam . User ) {},
addAccounts : [] string { accts [ 1 ]. GetPublicId ()},
2020-09-25 20:44:55 -07:00
resultAccounts : [] string { accts [ 1 ]. GetPublicId ()},
},
{
name : "Add account on populated user" ,
setup : func ( u * iam . User ) {
2020-09-28 15:39:10 -04:00
_ , err := iamRepo . SetUserAccounts ( context . Background (), u . GetPublicId (), u . GetVersion (),
2020-09-25 20:44:55 -07:00
[] string { accts [ 0 ]. GetPublicId ()})
require . NoError ( t , err )
u . Version = u . Version + 1
},
2020-09-28 15:39:10 -04:00
addAccounts : [] string { accts [ 1 ]. GetPublicId ()},
2020-09-25 20:44:55 -07:00
resultAccounts : [] string { accts [ 0 ]. GetPublicId (), accts [ 1 ]. GetPublicId ()},
},
{
name : "Add empty on populated user" ,
setup : func ( u * iam . User ) {
2020-09-28 15:39:10 -04:00
iamRepo . SetUserAccounts ( context . Background (), u . GetPublicId (), u . GetVersion (),
2020-09-25 20:44:55 -07:00
[] string { accts [ 0 ]. GetPublicId (), accts [ 1 ]. GetPublicId ()})
require . NoError ( t , err )
u . Version = u . Version + 1
},
wantErr : true ,
},
}
for _ , tc := range addCases {
t . Run ( tc . name , func ( t * testing . T ) {
usr := iam . TestUser ( t , iamRepo , o . GetPublicId ())
defer func () {
_ , err := iamRepo . DeleteUser ( context . Background (), usr . GetPublicId ())
require . NoError ( t , err )
}()
tc . setup ( usr )
req := & pbs . AddUserAccountsRequest {
Id : usr . GetPublicId (),
Version : usr . GetVersion (),
AccountIds : tc . addAccounts ,
}
got , err := s . AddUserAccounts ( auth . DisabledAuthTestContext ( auth . WithScopeId ( o . GetPublicId ())), req )
if tc . wantErr {
assert . Error ( t , err )
return
}
require . NoError ( t , err , "Got error: %v" , err )
assert . ElementsMatch ( t , got . GetItem (). GetAccountIds (), tc . resultAccounts )
})
}
usr := iam . TestUser ( t , iamRepo , o . GetPublicId ())
failCases := [] struct {
name string
req * pbs . AddUserAccountsRequest
err error
}{
{
name : "Bad user Id" ,
req : & pbs . AddUserAccountsRequest {
Id : "bad id" ,
Version : usr . GetVersion (),
},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
},
}
for _ , tc := range failCases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
_ , gErr := s . AddUserAccounts ( auth . DisabledAuthTestContext ( auth . WithScopeId ( usr . GetScopeId ())), tc . req )
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "AddUserAccounts(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
})
}
}
func TestSetAccount ( t * testing . T ) {
conn , _ := db . TestSetup ( t , "postgres" )
wrap := db . TestWrapper ( t )
iamRepo := iam . TestRepo ( t , conn , wrap )
repoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
s , err := users . NewService ( repoFn )
require . NoError ( t , err , "Error when getting new user service." )
o , _ := iam . TestScopes ( t , iamRepo )
amId := password . TestAuthMethods ( t , conn , o . GetPublicId (), 1 )[ 0 ]. GetPublicId ()
accts := password . TestAccounts ( t , conn , amId , 3 )
setCases := [] struct {
2020-09-28 15:39:10 -04:00
name string
setup func ( * iam . User )
setAccounts [] string
2020-09-25 20:44:55 -07:00
resultAccounts [] string
2020-09-28 15:39:10 -04:00
wantErr bool
2020-09-25 20:44:55 -07:00
}{
{
2020-09-28 15:39:10 -04:00
name : "Set account on empty user" ,
setup : func ( u * iam . User ) {},
2020-09-25 20:44:55 -07:00
setAccounts : [] string { accts [ 1 ]. GetPublicId ()},
resultAccounts : [] string { accts [ 1 ]. GetPublicId ()},
},
{
name : "Set account on populated user" ,
setup : func ( u * iam . User ) {
2020-09-28 15:39:10 -04:00
iamRepo . AddUserAccounts ( context . Background (), u . GetPublicId (), u . GetVersion (),
2020-09-25 20:44:55 -07:00
[] string { accts [ 0 ]. GetPublicId ()})
require . NoError ( t , err )
u . Version = u . Version + 1
},
setAccounts : [] string { accts [ 1 ]. GetPublicId ()},
resultAccounts : [] string { accts [ 1 ]. GetPublicId ()},
},
{
name : "Set empty on populated user" ,
setup : func ( u * iam . User ) {
2020-09-28 15:39:10 -04:00
iamRepo . AddUserAccounts ( context . Background (), u . GetPublicId (), u . GetVersion (),
2020-09-25 20:44:55 -07:00
[] string { accts [ 0 ]. GetPublicId (), accts [ 1 ]. GetPublicId ()})
require . NoError ( t , err )
u . Version = u . Version + 1
},
setAccounts : [] string {},
resultAccounts : nil ,
},
}
for _ , tc := range setCases {
2020-09-28 15:39:10 -04:00
t . Run ( tc . name , func ( t * testing . T ) {
usr := iam . TestUser ( t , iamRepo , o . GetPublicId ())
defer func () {
iamRepo . DeleteUser ( context . Background (), usr . GetPublicId ())
}()
tc . setup ( usr )
req := & pbs . SetUserAccountsRequest {
Id : usr . GetPublicId (),
Version : usr . GetVersion (),
AccountIds : tc . setAccounts ,
}
got , err := s . SetUserAccounts ( auth . DisabledAuthTestContext ( auth . WithScopeId ( o . GetPublicId ())), req )
if tc . wantErr {
assert . Error ( t , err )
return
}
require . NoError ( t , err , "Got error: %v" , err )
assert . ElementsMatch ( t , got . GetItem (). GetAccountIds (), tc . resultAccounts )
})
2020-09-25 20:44:55 -07:00
}
usr := iam . TestUser ( t , iamRepo , o . GetPublicId ())
failCases := [] struct {
name string
req * pbs . SetUserAccountsRequest
err error
}{
{
name : "Bad User Id" ,
req : & pbs . SetUserAccountsRequest {
Id : "bad id" ,
Version : usr . GetVersion (),
},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
},
}
for _ , tc := range failCases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
_ , gErr := s . SetUserAccounts ( auth . DisabledAuthTestContext ( auth . WithScopeId ( usr . GetScopeId ())), tc . req )
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "SetUserAccounts(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
})
}
}
func TestRemoveAccount ( t * testing . T ) {
conn , _ := db . TestSetup ( t , "postgres" )
wrap := db . TestWrapper ( t )
iamRepo := iam . TestRepo ( t , conn , wrap )
repoFn := func () ( * iam . Repository , error ) {
return iamRepo , nil
}
s , err := users . NewService ( repoFn )
require . NoError ( t , err , "Error when getting new user service." )
o , _ := iam . TestScopes ( t , iamRepo )
amId := password . TestAuthMethods ( t , conn , o . GetPublicId (), 1 )[ 0 ]. GetPublicId ()
accts := password . TestAccounts ( t , conn , amId , 3 )
addCases := [] struct {
2020-09-28 15:39:10 -04:00
name string
setup func ( * iam . User )
2020-09-25 20:44:55 -07:00
removeAccounts [] string
resultAccounts [] string
2020-09-28 15:39:10 -04:00
wantErr bool
2020-09-25 20:44:55 -07:00
}{
{
2020-09-28 15:39:10 -04:00
name : "Remove account on empty user" ,
setup : func ( * iam . User ) {},
2020-09-25 20:44:55 -07:00
removeAccounts : [] string { accts [ 1 ]. GetPublicId ()},
2020-09-28 15:39:10 -04:00
wantErr : true ,
2020-09-25 20:44:55 -07:00
},
{
name : "Remove 1 of 2 accounts from user" ,
setup : func ( u * iam . User ) {
2020-09-28 15:39:10 -04:00
_ , err := iamRepo . SetUserAccounts ( context . Background (), u . GetPublicId (), u . GetVersion (),
2020-09-25 20:44:55 -07:00
[] string { accts [ 0 ]. GetPublicId (), accts [ 1 ]. GetPublicId ()})
require . NoError ( t , err )
u . Version = u . Version + 1
},
removeAccounts : [] string { accts [ 1 ]. GetPublicId ()},
resultAccounts : [] string { accts [ 0 ]. GetPublicId ()},
},
{
name : "Remove all accounts from user" ,
setup : func ( u * iam . User ) {
2020-09-28 15:39:10 -04:00
_ , err := iamRepo . SetUserAccounts ( context . Background (), u . GetPublicId (), u . GetVersion (),
2020-09-25 20:44:55 -07:00
[] string { accts [ 0 ]. GetPublicId (), accts [ 1 ]. GetPublicId ()})
require . NoError ( t , err )
u . Version = u . Version + 1
},
removeAccounts : [] string { accts [ 0 ]. GetPublicId (), accts [ 1 ]. GetPublicId ()},
resultAccounts : [] string {},
},
{
name : "Remove empty on populated user" ,
setup : func ( u * iam . User ) {
2020-09-28 15:39:10 -04:00
_ , err := iamRepo . SetUserAccounts ( context . Background (), u . GetPublicId (), u . GetVersion (),
2020-09-25 20:44:55 -07:00
[] string { accts [ 0 ]. GetPublicId ()})
require . NoError ( t , err )
u . Version = u . Version + 1
},
wantErr : true ,
},
}
for _ , tc := range addCases {
2020-09-28 15:39:10 -04:00
t . Run ( tc . name , func ( t * testing . T ) {
usr := iam . TestUser ( t , iamRepo , o . GetPublicId ())
defer func () {
iamRepo . DeleteUser ( context . Background (), usr . GetPublicId ())
}()
tc . setup ( usr )
req := & pbs . RemoveUserAccountsRequest {
Id : usr . GetPublicId (),
Version : usr . GetVersion (),
AccountIds : tc . removeAccounts ,
}
got , err := s . RemoveUserAccounts ( auth . DisabledAuthTestContext ( auth . WithScopeId ( o . GetPublicId ())), req )
if tc . wantErr {
assert . Error ( t , err )
return
}
require . NoError ( t , err , "Got error: %v" , err )
assert . ElementsMatch ( t , got . GetItem (). GetAccountIds (), tc . resultAccounts )
})
2020-09-25 20:44:55 -07:00
}
usr := iam . TestUser ( t , iamRepo , o . GetPublicId ())
failCases := [] struct {
name string
req * pbs . AddUserAccountsRequest
err error
}{
{
name : "Bad User Id" ,
req : & pbs . AddUserAccountsRequest {
Id : "bad id" ,
Version : usr . GetVersion (),
},
err : handlers . ApiErrorWithCode ( codes . InvalidArgument ),
},
}
for _ , tc := range failCases {
t . Run ( tc . name , func ( t * testing . T ) {
assert , require := assert . New ( t ), require . New ( t )
_ , gErr := s . AddUserAccounts ( auth . DisabledAuthTestContext ( auth . WithScopeId ( usr . GetScopeId ())), tc . req )
if tc . err != nil {
require . Error ( gErr )
assert . True ( errors . Is ( gErr , tc . err ), "AddUserAccounts(%+v) got error %v, wanted %v" , tc . req , gErr , tc . err )
}
})
}
}