Skip to content

Commit 43b04f5

Browse files
author
Muhammad Idil Haq Amir
committed
test: update test
1 parent 832a334 commit 43b04f5

File tree

2 files changed

+57
-142
lines changed

2 files changed

+57
-142
lines changed

core/appeal/service.go

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -724,37 +724,35 @@ func (s *Service) AddApprover(ctx context.Context, appealID, approvalID, email s
724724
}
725725
}
726726

727-
notifications := []domain.Notification{
728-
{
729-
User: email,
730-
Labels: map[string]string{
731-
"appeal_id": appeal.ID,
732-
},
733-
Message: domain.NotificationMessage{
734-
Type: domain.NotificationTypeApproverNotification,
735-
Variables: map[string]interface{}{
736-
"resource_name": fmt.Sprintf("%s (%s: %s)", appeal.Resource.Name, appeal.Resource.ProviderType, appeal.Resource.URN),
737-
"role": appeal.Role,
738-
"requestor": appeal.CreatedBy,
739-
"appeal_id": appeal.ID,
740-
"account_id": appeal.AccountID,
741-
"account_type": appeal.AccountType,
742-
"provider_type": appeal.Resource.ProviderType,
743-
"resource_type": appeal.Resource.Type,
744-
"created_at": appeal.CreatedAt,
745-
"approval_step": approval.Name,
746-
"actor": email,
747-
"details": appeal.Details,
748-
"duration": duration,
749-
"creator": appeal.Creator,
750-
},
751-
},
752-
},
753-
}
754-
755727
go func() {
756728
ctx := context.WithoutCancel(ctx)
757-
if errs := s.notifier.Notify(ctx, notifications); errs != nil {
729+
if errs := s.notifier.Notify(ctx, []domain.Notification{
730+
{
731+
User: email,
732+
Labels: map[string]string{
733+
"appeal_id": appeal.ID,
734+
},
735+
Message: domain.NotificationMessage{
736+
Type: domain.NotificationTypeApproverNotification,
737+
Variables: map[string]interface{}{
738+
"resource_name": fmt.Sprintf("%s (%s: %s)", appeal.Resource.Name, appeal.Resource.ProviderType, appeal.Resource.URN),
739+
"role": appeal.Role,
740+
"requestor": appeal.CreatedBy,
741+
"appeal_id": appeal.ID,
742+
"account_id": appeal.AccountID,
743+
"account_type": appeal.AccountType,
744+
"provider_type": appeal.Resource.ProviderType,
745+
"resource_type": appeal.Resource.Type,
746+
"created_at": appeal.CreatedAt,
747+
"approval_step": approval.Name,
748+
"actor": email,
749+
"details": appeal.Details,
750+
"duration": duration,
751+
"creator": appeal.Creator,
752+
},
753+
},
754+
},
755+
}); errs != nil {
758756
for _, err1 := range errs {
759757
s.logger.Error(ctx, "failed to send notifications", "error", err1.Error())
760758
}

core/appeal/service_test.go

Lines changed: 30 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -3015,30 +3015,39 @@ func (s *ServiceTestSuite) TestCancel() {
30153015

30163016
func (s *ServiceTestSuite) TestAddApprover() {
30173017
s.Run("should return appeal on success", func() {
3018-
h := newServiceTestHelper()
3019-
defer h.assertExpectations(s.T())
30203018
appealID := uuid.New().String()
30213019
approvalID := uuid.New().String()
30223020
approvalName := "test-approval-name"
30233021
newApprover := "[email protected]"
30243022

3025-
tc := struct {
3023+
testCases := []struct {
30263024
name, appealID, approvalID, newApprover string
3027-
expectedAppeal *domain.Appeal
3028-
expectedApproval *domain.Approval
30293025
}{
3030-
name: "with approval ID",
3031-
appealID: appealID, approvalID: approvalID, newApprover: newApprover,
3032-
expectedAppeal: &domain.Appeal{
3033-
ID: appealID,
3034-
Status: domain.AppealStatusPending,
3035-
Approvals: []*domain.Approval{
3036-
{
3037-
ID: approvalID,
3038-
Name: approvalName,
3039-
Status: domain.ApprovalStatusPending,
3040-
Approvers: []string{
3041-
3026+
{
3027+
name: "with approval ID",
3028+
appealID: appealID, approvalID: approvalID, newApprover: newApprover,
3029+
},
3030+
{
3031+
name: "with approval name",
3032+
appealID: appealID, approvalID: approvalName, newApprover: newApprover,
3033+
},
3034+
}
3035+
3036+
for _, tc := range testCases {
3037+
s.Run(tc.name, func() {
3038+
h := newServiceTestHelper()
3039+
defer h.assertExpectations(s.T())
3040+
expectedAppeal := &domain.Appeal{
3041+
ID: appealID,
3042+
Status: domain.AppealStatusPending,
3043+
Approvals: []*domain.Approval{
3044+
{
3045+
ID: approvalID,
3046+
Name: approvalName,
3047+
Status: domain.ApprovalStatusPending,
3048+
Approvers: []string{
3049+
3050+
},
30423051
},
30433052
},
30443053
Resource: &domain.Resource{},
@@ -3063,112 +3072,20 @@ func (s *ServiceTestSuite) TestAddApprover() {
30633072
h.mockNotifier.EXPECT().
30643073
Notify(h.ctxMatcher, mock.Anything).
30653074
Run(func(ctx context.Context, notifications []domain.Notification) {
3066-
s.Len(notifications, 1)
3075+
assert.Equal(s.T(), len(notifications), 1)
30673076
n := notifications[0]
3068-
s.Equal(tc.newApprover, n.User)
3069-
s.Equal(domain.NotificationTypeApproverNotification, n.Message.Type)
3077+
assert.Equal(s.T(), tc.newApprover, n.User)
3078+
assert.Equal(s.T(), domain.NotificationTypeApproverNotification, n.Message.Type)
30703079
}).
30713080
Return(nil).Once()
30723081

30733082
actualAppeal, actualError := h.service.AddApprover(context.Background(), appealID, approvalID, newApprover)
30743083

3075-
time.Sleep(time.Second)
30763084
s.NoError(actualError)
30773085
s.Equal(expectedApproval, actualAppeal.Approvals[0])
3078-
},
3079-
}
3080-
3081-
s.mockRepository.EXPECT().
3082-
GetByID(mock.MatchedBy(func(ctx context.Context) bool { return true }), appealID).
3083-
Return(tc.expectedAppeal, nil).Once()
3084-
s.mockApprovalService.EXPECT().
3085-
AddApprover(mock.MatchedBy(func(ctx context.Context) bool { return true }), approvalID, newApprover).
3086-
Return(nil).Once()
3087-
s.mockAuditLogger.EXPECT().
3088-
Log(mock.MatchedBy(func(ctx context.Context) bool { return true }), appeal.AuditKeyAddApprover, tc.expectedApproval).Return(nil).Once()
3089-
s.mockNotifier.EXPECT().
3090-
Notify(mock.MatchedBy(func(ctx context.Context) bool { return true }), mock.Anything).
3091-
Run(func(ctx context.Context, notifications []domain.Notification) {
3092-
assert.Equal(s.T(), len(notifications), 1)
3093-
n := notifications[0]
3094-
ng := n
3095-
assert.Equal(s.T(), tc.newApprover, ng.User)
3096-
assert.Equal(s.T(), domain.NotificationTypeApproverNotification, ng.Message.Type)
3097-
}).
3098-
Return(nil).Once()
3099-
3100-
actualAppeal, actualError := s.service.AddApprover(context.Background(), appealID, approvalID, newApprover)
3101-
3102-
s.NoError(actualError)
3103-
s.Equal(tc.expectedApproval, actualAppeal.Approvals[0])
3104-
s.mockRepository.AssertExpectations(s.T())
3105-
s.mockApprovalService.AssertExpectations(s.T())
3106-
})
3107-
3108-
s.Run("should return appeal on success with approval name", func() {
3109-
appealID := uuid.New().String()
3110-
approvalID := uuid.New().String()
3111-
approvalName := "test-approval-name"
3112-
newApprover := "[email protected]"
31133086

3114-
tc := struct {
3115-
name, appealID, approvalID, newApprover string
3116-
expectedAppeal *domain.Appeal
3117-
expectedApproval *domain.Approval
3118-
}{
3119-
name: "with approval name",
3120-
appealID: appealID, approvalID: approvalName, newApprover: newApprover,
3121-
expectedAppeal: &domain.Appeal{
3122-
ID: appealID,
3123-
Status: domain.AppealStatusPending,
3124-
Approvals: []*domain.Approval{
3125-
{
3126-
ID: approvalID,
3127-
Name: approvalName,
3128-
Status: domain.ApprovalStatusPending,
3129-
Approvers: []string{
3130-
3131-
},
3132-
},
3133-
},
3134-
Resource: &domain.Resource{},
3135-
},
3136-
expectedApproval: &domain.Approval{
3137-
ID: approvalID,
3138-
Name: approvalName,
3139-
Status: domain.ApprovalStatusPending,
3140-
Approvers: []string{
3141-
3142-
newApprover,
3143-
},
3144-
},
3087+
})
31453088
}
3146-
3147-
s.mockRepository.EXPECT().
3148-
GetByID(mock.MatchedBy(func(ctx context.Context) bool { return true }), appealID).
3149-
Return(tc.expectedAppeal, nil).Once()
3150-
s.mockApprovalService.EXPECT().
3151-
AddApprover(mock.MatchedBy(func(ctx context.Context) bool { return true }), approvalID, newApprover).
3152-
Return(nil).Once()
3153-
s.mockAuditLogger.EXPECT().
3154-
Log(mock.MatchedBy(func(ctx context.Context) bool { return true }), appeal.AuditKeyAddApprover, tc.expectedApproval).Return(nil).Once()
3155-
s.mockNotifier.EXPECT().
3156-
Notify(mock.MatchedBy(func(ctx context.Context) bool { return true }), mock.Anything).
3157-
Run(func(ctx context.Context, notifications []domain.Notification) {
3158-
assert.Equal(s.T(), len(notifications), 1)
3159-
n := notifications[0]
3160-
ng := n
3161-
assert.Equal(s.T(), tc.newApprover, ng.User)
3162-
assert.Equal(s.T(), domain.NotificationTypeApproverNotification, ng.Message.Type)
3163-
}).
3164-
Return(nil).Once()
3165-
3166-
actualAppeal, actualError := s.service.AddApprover(context.Background(), appealID, approvalID, newApprover)
3167-
3168-
s.NoError(actualError)
3169-
s.Equal(tc.expectedApproval, actualAppeal.Approvals[0])
3170-
s.mockRepository.AssertExpectations(s.T())
3171-
s.mockApprovalService.AssertExpectations(s.T())
31723089
})
31733090

31743091
s.Run("params validation", func() {

0 commit comments

Comments
 (0)