gitlab.com/picnic-app/backend/role-api@v0.0.0-20230614140944-06a76ff3696d/internal/controller/remove_permissions_test.go (about)

     1  package controller_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/google/uuid"
     8  	"github.com/stretchr/testify/require"
     9  	"google.golang.org/grpc/codes"
    10  
    11  	v1 "gitlab.com/picnic-app/backend/libs/golang/protobuf-registry/gen/role-api/role/v1"
    12  	"gitlab.com/picnic-app/backend/role-api/internal/errors"
    13  )
    14  
    15  func TestController_RemovePermissions_Validation(t *testing.T) {
    16  	t.Parallel()
    17  
    18  	container := initContainer(t)
    19  
    20  	for _, test := range []struct {
    21  		name string
    22  		ctx  context.Context
    23  		req  *v1.RemovePermissionsRequest
    24  		want codes.Code
    25  	}{
    26  		{
    27  			name: "no request",
    28  			ctx:  context.Background(),
    29  			req:  nil,
    30  			want: codes.InvalidArgument,
    31  		},
    32  		{
    33  			name: "no role ID",
    34  			ctx:  context.Background(),
    35  			req:  &v1.RemovePermissionsRequest{},
    36  			want: codes.InvalidArgument,
    37  		},
    38  		{
    39  			name: "no permission",
    40  			ctx:  context.Background(),
    41  			req: &v1.RemovePermissionsRequest{
    42  				RoleId: uuid.NewString(),
    43  			},
    44  			want: codes.InvalidArgument,
    45  		},
    46  		{
    47  			name: "role does not exist",
    48  			ctx:  context.Background(),
    49  			req: &v1.RemovePermissionsRequest{
    50  				RoleId: uuid.NewString(),
    51  				Permissions: []*v1.Permission{
    52  					{Service: "Content", Operation: "CreatePost"},
    53  				},
    54  			},
    55  			want: codes.NotFound,
    56  		},
    57  	} {
    58  		test := test
    59  		t.Run(test.name, func(t *testing.T) {
    60  			_, err := container.controller.RemovePermissions(test.ctx, test.req)
    61  			got := errors.GetCode(err)
    62  			require.Equal(t, test.want, got, err)
    63  		})
    64  	}
    65  }
    66  
    67  func TestController_RemovePermissions(t *testing.T) {
    68  	t.Parallel()
    69  
    70  	container := initContainer(t)
    71  
    72  	role, err := setUpRole(container)
    73  	require.NoError(t, err)
    74  
    75  	ctx := context.Background()
    76  
    77  	// remove permissions
    78  	removed := role.Permissions[0:1]
    79  	removeResponse, err := container.controller.RemovePermissions(
    80  		ctx,
    81  		&v1.RemovePermissionsRequest{
    82  			RoleId:      role.Id,
    83  			Permissions: removed,
    84  		},
    85  	)
    86  	require.NoError(t, err)
    87  	require.NotNil(t, removeResponse)
    88  
    89  	// get role
    90  	getRoleResponse, err := container.controller.GetRole(
    91  		ctx,
    92  		&v1.GetRoleRequest{
    93  			Id: role.Id,
    94  		},
    95  	)
    96  	require.NoError(t, err)
    97  	role = getRoleResponse.Role
    98  
    99  	for _, permission := range removed {
   100  		found := false
   101  		for _, got := range role.Permissions {
   102  			if got.Service != permission.Service || got.Operation != permission.Operation {
   103  				continue
   104  			}
   105  			found = true
   106  		}
   107  		require.False(t, found)
   108  	}
   109  }