@@ -6,9 +6,16 @@ import (
6
6
"testing"
7
7
"time"
8
8
9
+ "github.com/google/uuid"
9
10
"github.com/lestrrat-go/jwx/v2/jwk"
11
+ "github.com/raystack/frontier/core/organization"
12
+ "github.com/raystack/frontier/core/permission"
13
+ "github.com/raystack/frontier/core/project"
14
+ "github.com/raystack/frontier/core/relation"
15
+ "github.com/raystack/frontier/core/resource"
10
16
"github.com/raystack/frontier/core/serviceuser"
11
17
"github.com/raystack/frontier/internal/api/v1beta1/mocks"
18
+ "github.com/raystack/frontier/internal/bootstrap/schema"
12
19
"github.com/raystack/frontier/pkg/metadata"
13
20
frontierv1beta1 "github.com/raystack/frontier/proto/v1beta1"
14
21
"github.com/stretchr/testify/assert"
@@ -722,3 +729,217 @@ func TestHandler_CreateServiceUserCredential(t *testing.T) {
722
729
})
723
730
}
724
731
}
732
+
733
+ func TestHandler_ListServiceUserProjects (t * testing.T ) {
734
+ testProjectMap := map [string ]project.Project {
735
+ "ab657ae7-8c9e-45eb-9862-dd9ceb6d5c71" : {
736
+ ID : "ab657ae7-8c9e-45eb-9862-dd9ceb6d5c71" ,
737
+ Name : "prj-1" ,
738
+ Metadata : metadata.Metadata {
739
+
740
+ },
741
+ Organization : organization.Organization {
742
+ ID : testOrgID ,
743
+ },
744
+ CreatedAt : time.Time {},
745
+ UpdatedAt : time.Time {},
746
+ },
747
+ "c7772c63-fca4-4c7c-bf93-c8f85115de4b" : {
748
+ ID : "c7772c63-fca4-4c7c-bf93-c8f85115de4b" ,
749
+ Name : "prj-2" ,
750
+ Metadata : metadata.Metadata {
751
+
752
+ },
753
+ Organization : organization.Organization {
754
+ ID : testOrgID ,
755
+ },
756
+ CreatedAt : time.Time {},
757
+ UpdatedAt : time.Time {},
758
+ },
759
+ }
760
+
761
+ tests := []struct {
762
+ name string
763
+ setup func (projSvc * mocks.ProjectService , permSvc * mocks.PermissionService , resourceSvc * mocks.ResourceService )
764
+ request * frontierv1beta1.ListServiceUserProjectsRequest
765
+ want * frontierv1beta1.ListServiceUserProjectsResponse
766
+ wantErr error
767
+ }{
768
+ {
769
+ name : "should return internal server error when list service user project returns error" ,
770
+ request : & frontierv1beta1.ListServiceUserProjectsRequest {
771
+ Id : "1" ,
772
+ },
773
+ setup : func (projSvc * mocks.ProjectService , permSvc * mocks.PermissionService , resourceSvc * mocks.ResourceService ) {
774
+ projSvc .EXPECT ().ListByUser (mock .AnythingOfType ("context.backgroundCtx" ), "1" , schema .ServiceUserPrincipal , project.Filter {}).Return (nil , errors .New ("test error" ))
775
+ },
776
+ want : nil ,
777
+ wantErr : errors .New ("test error" ),
778
+ },
779
+ {
780
+ name : "should return project list when there is no error" ,
781
+ request : & frontierv1beta1.ListServiceUserProjectsRequest {
782
+ Id : "1" ,
783
+ },
784
+ setup : func (projSvc * mocks.ProjectService , permSvc * mocks.PermissionService , resourceSvc * mocks.ResourceService ) {
785
+ var projects []project.Project
786
+
787
+ for _ , projectID := range testProjectIDList {
788
+ projects = append (projects , testProjectMap [projectID ])
789
+ }
790
+ projSvc .EXPECT ().ListByUser (mock .AnythingOfType ("context.backgroundCtx" ), "1" , schema .ServiceUserPrincipal , project.Filter {}).Return (projects , nil )
791
+ },
792
+ want : & frontierv1beta1.ListServiceUserProjectsResponse {
793
+ Projects : []* frontierv1beta1.Project {{
794
+ Id : "ab657ae7-8c9e-45eb-9862-dd9ceb6d5c71" ,
795
+ Name : "prj-1" ,
796
+ Metadata : & structpb.Struct {
797
+ Fields : map [string ]* structpb.Value {
798
+ "email" :
structpb .
NewStringValue (
"[email protected] " ),
799
+ },
800
+ },
801
+ OrgId : "9f256f86-31a3-11ec-8d3d-0242ac130003" ,
802
+ CreatedAt : timestamppb .New (time.Time {}),
803
+ UpdatedAt : timestamppb .New (time.Time {}),
804
+ },
805
+ {
806
+ Id : "c7772c63-fca4-4c7c-bf93-c8f85115de4b" ,
807
+ Name : "prj-2" ,
808
+ Metadata : & structpb.Struct {
809
+ Fields : map [string ]* structpb.Value {
810
+ "email" :
structpb .
NewStringValue (
"[email protected] " ),
811
+ },
812
+ },
813
+ OrgId : "9f256f86-31a3-11ec-8d3d-0242ac130003" ,
814
+ CreatedAt : timestamppb .New (time.Time {}),
815
+ UpdatedAt : timestamppb .New (time.Time {}),
816
+ },
817
+ },
818
+ },
819
+ wantErr : nil ,
820
+ },
821
+ {
822
+ name : "should return project list with access pairs if withPermission is passed" ,
823
+ request : & frontierv1beta1.ListServiceUserProjectsRequest {
824
+ Id : "1" ,
825
+ WithPermissions : []string {"get" },
826
+ },
827
+ setup : func (projSvc * mocks.ProjectService , permSvc * mocks.PermissionService , resourceSvc * mocks.ResourceService ) {
828
+ var projects []project.Project
829
+
830
+ for _ , projectID := range testProjectIDList {
831
+ projects = append (projects , testProjectMap [projectID ])
832
+ }
833
+
834
+ ctx := mock .AnythingOfType ("context.backgroundCtx" )
835
+ projSvc .EXPECT ().ListByUser (ctx , "1" , schema .ServiceUserPrincipal , project.Filter {}).Return (projects , nil )
836
+
837
+ permSvc .EXPECT ().Get (ctx , "app/project:get" ).Return (
838
+ permission.Permission {
839
+ ID : uuid .New ().String (),
840
+ Name : "get" ,
841
+ NamespaceID : "app/project" ,
842
+ Metadata : map [string ]any {},
843
+ CreatedAt : time.Time {},
844
+ UpdatedAt : time.Time {},
845
+ }, nil )
846
+
847
+ resourceSvc .EXPECT ().BatchCheck (ctx , []resource.Check {
848
+ {
849
+ Object : relation.Object {
850
+ ID : "ab657ae7-8c9e-45eb-9862-dd9ceb6d5c71" ,
851
+ Namespace : "app/project" ,
852
+ },
853
+ Permission : "get" ,
854
+ },
855
+ {
856
+ Object : relation.Object {
857
+ ID : "c7772c63-fca4-4c7c-bf93-c8f85115de4b" ,
858
+ Namespace : "app/project" ,
859
+ },
860
+ Permission : "get" ,
861
+ },
862
+ }).Return ([]relation.CheckPair {
863
+ {
864
+ Relation : relation.Relation {
865
+ Object : relation.Object {
866
+ ID : "ab657ae7-8c9e-45eb-9862-dd9ceb6d5c71" ,
867
+ Namespace : "app/project" ,
868
+ },
869
+ RelationName : "get" ,
870
+ },
871
+ Status : true ,
872
+ },
873
+ {
874
+ Relation : relation.Relation {
875
+ Object : relation.Object {
876
+ ID : "c7772c63-fca4-4c7c-bf93-c8f85115de4b" ,
877
+ Namespace : "app/project" ,
878
+ },
879
+ RelationName : "get" ,
880
+ },
881
+ Status : true ,
882
+ },
883
+ }, nil )
884
+ },
885
+ want : & frontierv1beta1.ListServiceUserProjectsResponse {
886
+ Projects : []* frontierv1beta1.Project {{
887
+ Id : "ab657ae7-8c9e-45eb-9862-dd9ceb6d5c71" ,
888
+ Name : "prj-1" ,
889
+ Metadata : & structpb.Struct {
890
+ Fields : map [string ]* structpb.Value {
891
+ "email" :
structpb .
NewStringValue (
"[email protected] " ),
892
+ },
893
+ },
894
+ OrgId : "9f256f86-31a3-11ec-8d3d-0242ac130003" ,
895
+ CreatedAt : timestamppb .New (time.Time {}),
896
+ UpdatedAt : timestamppb .New (time.Time {}),
897
+ },
898
+ {
899
+ Id : "c7772c63-fca4-4c7c-bf93-c8f85115de4b" ,
900
+ Name : "prj-2" ,
901
+ Metadata : & structpb.Struct {
902
+ Fields : map [string ]* structpb.Value {
903
+ "email" :
structpb .
NewStringValue (
"[email protected] " ),
904
+ },
905
+ },
906
+ OrgId : "9f256f86-31a3-11ec-8d3d-0242ac130003" ,
907
+ CreatedAt : timestamppb .New (time.Time {}),
908
+ UpdatedAt : timestamppb .New (time.Time {}),
909
+ },
910
+ },
911
+ AccessPairs : []* frontierv1beta1.ListServiceUserProjectsResponse_AccessPair {
912
+ {
913
+ ProjectId : "ab657ae7-8c9e-45eb-9862-dd9ceb6d5c71" ,
914
+ Permissions : []string {"get" },
915
+ },
916
+ {
917
+ ProjectId : "c7772c63-fca4-4c7c-bf93-c8f85115de4b" ,
918
+ Permissions : []string {"get" },
919
+ },
920
+ },
921
+ },
922
+ wantErr : nil ,
923
+ },
924
+ }
925
+
926
+ for _ , tt := range tests {
927
+ t .Run (tt .name , func (t * testing.T ) {
928
+ mockProjectSvc := new (mocks.ProjectService )
929
+ mockPermssionSvc := new (mocks.PermissionService )
930
+ mockResourceSvc := new (mocks.ResourceService )
931
+
932
+ if tt .setup != nil {
933
+ tt .setup (mockProjectSvc , mockPermssionSvc , mockResourceSvc )
934
+ }
935
+ h := Handler {
936
+ projectService : mockProjectSvc ,
937
+ permissionService : mockPermssionSvc ,
938
+ resourceService : mockResourceSvc ,
939
+ }
940
+ got , err := h .ListServiceUserProjects (context .Background (), tt .request )
941
+ assert .EqualValues (t , tt .want , got )
942
+ assert .EqualValues (t , tt .wantErr , err )
943
+ })
944
+ }
945
+ }
0 commit comments