|
7 | 7 | "time" |
8 | 8 |
|
9 | 9 | "connectrpc.com/connect" |
| 10 | + "github.com/raystack/frontier/core/namespace" |
10 | 11 | "github.com/raystack/frontier/core/policy" |
11 | 12 | "github.com/raystack/frontier/core/role" |
12 | 13 | "github.com/raystack/frontier/internal/api/v1beta1connect/mocks" |
@@ -625,3 +626,137 @@ func TestConnectHandler_UpdatePolicy(t *testing.T) { |
625 | 626 | }) |
626 | 627 | } |
627 | 628 | } |
| 629 | + |
| 630 | +func TestConnectHandler_DeletePolicy(t *testing.T) { |
| 631 | + testPolicyID := utils.NewString() |
| 632 | + |
| 633 | + tests := []struct { |
| 634 | + name string |
| 635 | + setup func(ps *mocks.PolicyService) |
| 636 | + request *connect.Request[frontierv1beta1.DeletePolicyRequest] |
| 637 | + want *connect.Response[frontierv1beta1.DeletePolicyResponse] |
| 638 | + wantErr error |
| 639 | + errCode connect.Code |
| 640 | + }{ |
| 641 | + { |
| 642 | + name: "should return not found error when policy doesn't exist", |
| 643 | + setup: func(ps *mocks.PolicyService) { |
| 644 | + ps.On("Delete", mock.Anything, testPolicyID).Return(policy.ErrNotExist) |
| 645 | + }, |
| 646 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 647 | + Id: testPolicyID, |
| 648 | + }), |
| 649 | + want: nil, |
| 650 | + wantErr: ErrPolicyNotFound, |
| 651 | + errCode: connect.CodeNotFound, |
| 652 | + }, |
| 653 | + { |
| 654 | + name: "should return not found error when policy ID is invalid", |
| 655 | + setup: func(ps *mocks.PolicyService) { |
| 656 | + ps.On("Delete", mock.Anything, "invalid-id").Return(policy.ErrInvalidID) |
| 657 | + }, |
| 658 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 659 | + Id: "invalid-id", |
| 660 | + }), |
| 661 | + want: nil, |
| 662 | + wantErr: ErrPolicyNotFound, |
| 663 | + errCode: connect.CodeNotFound, |
| 664 | + }, |
| 665 | + { |
| 666 | + name: "should return not found error when policy UUID is invalid", |
| 667 | + setup: func(ps *mocks.PolicyService) { |
| 668 | + ps.On("Delete", mock.Anything, "not-a-uuid").Return(policy.ErrInvalidUUID) |
| 669 | + }, |
| 670 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 671 | + Id: "not-a-uuid", |
| 672 | + }), |
| 673 | + want: nil, |
| 674 | + wantErr: ErrPolicyNotFound, |
| 675 | + errCode: connect.CodeNotFound, |
| 676 | + }, |
| 677 | + { |
| 678 | + name: "should return invalid argument error when policy details are invalid", |
| 679 | + setup: func(ps *mocks.PolicyService) { |
| 680 | + ps.On("Delete", mock.Anything, testPolicyID).Return(policy.ErrInvalidDetail) |
| 681 | + }, |
| 682 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 683 | + Id: testPolicyID, |
| 684 | + }), |
| 685 | + want: nil, |
| 686 | + wantErr: ErrBadRequest, |
| 687 | + errCode: connect.CodeInvalidArgument, |
| 688 | + }, |
| 689 | + { |
| 690 | + name: "should return invalid argument error when namespace doesn't exist", |
| 691 | + setup: func(ps *mocks.PolicyService) { |
| 692 | + ps.On("Delete", mock.Anything, testPolicyID).Return(namespace.ErrNotExist) |
| 693 | + }, |
| 694 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 695 | + Id: testPolicyID, |
| 696 | + }), |
| 697 | + want: nil, |
| 698 | + wantErr: ErrBadRequest, |
| 699 | + errCode: connect.CodeInvalidArgument, |
| 700 | + }, |
| 701 | + { |
| 702 | + name: "should return already exists error when policy has conflicts", |
| 703 | + setup: func(ps *mocks.PolicyService) { |
| 704 | + ps.On("Delete", mock.Anything, testPolicyID).Return(policy.ErrConflict) |
| 705 | + }, |
| 706 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 707 | + Id: testPolicyID, |
| 708 | + }), |
| 709 | + want: nil, |
| 710 | + wantErr: ErrConflictRequest, |
| 711 | + errCode: connect.CodeAlreadyExists, |
| 712 | + }, |
| 713 | + { |
| 714 | + name: "should return internal server error when policy service returns unknown error", |
| 715 | + setup: func(ps *mocks.PolicyService) { |
| 716 | + ps.On("Delete", mock.Anything, testPolicyID).Return(errors.New("service error")) |
| 717 | + }, |
| 718 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 719 | + Id: testPolicyID, |
| 720 | + }), |
| 721 | + want: nil, |
| 722 | + wantErr: ErrInternalServerError, |
| 723 | + errCode: connect.CodeInternal, |
| 724 | + }, |
| 725 | + { |
| 726 | + name: "should successfully delete policy", |
| 727 | + setup: func(ps *mocks.PolicyService) { |
| 728 | + ps.On("Delete", mock.Anything, testPolicyID).Return(nil) |
| 729 | + }, |
| 730 | + request: connect.NewRequest(&frontierv1beta1.DeletePolicyRequest{ |
| 731 | + Id: testPolicyID, |
| 732 | + }), |
| 733 | + want: connect.NewResponse(&frontierv1beta1.DeletePolicyResponse{}), |
| 734 | + wantErr: nil, |
| 735 | + }, |
| 736 | + } |
| 737 | + |
| 738 | + for _, tt := range tests { |
| 739 | + t.Run(tt.name, func(t *testing.T) { |
| 740 | + mockPolicyService := &mocks.PolicyService{} |
| 741 | + if tt.setup != nil { |
| 742 | + tt.setup(mockPolicyService) |
| 743 | + } |
| 744 | + |
| 745 | + handler := &ConnectHandler{ |
| 746 | + policyService: mockPolicyService, |
| 747 | + } |
| 748 | + |
| 749 | + got, err := handler.DeletePolicy(context.Background(), tt.request) |
| 750 | + if tt.wantErr != nil { |
| 751 | + assert.Error(t, err) |
| 752 | + assert.Equal(t, tt.errCode, connect.CodeOf(err)) |
| 753 | + assert.Nil(t, got) |
| 754 | + } else { |
| 755 | + assert.NoError(t, err) |
| 756 | + assert.Equal(t, tt.want, got) |
| 757 | + } |
| 758 | + |
| 759 | + mockPolicyService.AssertExpectations(t) |
| 760 | + }) |
| 761 | + } |
| 762 | +} |
0 commit comments