• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

goto / shield / 10787256047

10 Sep 2024 06:47AM UTC coverage: 51.953% (+0.01%) from 51.943%
10787256047

Pull #91

github

FemiNoviaLina
fix: core function name
Pull Request #91: feat: list resource of a user

66 of 143 new or added lines in 8 files covered. (46.15%)

1 existing line in 1 file now uncovered.

6809 of 13106 relevant lines covered (51.95%)

11.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/core/policy/service.go
1
package policy
2

3
import (
4
        "context"
5
        "fmt"
6

7
        "github.com/goto/salt/log"
8
        "github.com/goto/shield/core/activity"
9
        "github.com/goto/shield/core/user"
10
)
11

12
const (
13
        auditKeyPolicyUpsert = "policy.upsert"
14
        auditKeyPolicyUpdate = "policy.update"
15
)
16

17
type UserService interface {
18
        FetchCurrentUser(ctx context.Context) (user.User, error)
19
}
20

21
type ActivityService interface {
22
        Log(ctx context.Context, action string, actor activity.Actor, data any) error
23
}
24

25
type Service struct {
26
        logger          log.Logger
27
        repository      Repository
28
        userService     UserService
29
        activityService ActivityService
30
}
31

32
func NewService(logger log.Logger, repository Repository, userService UserService, activityService ActivityService) *Service {
×
33
        return &Service{
×
34
                logger:          logger,
×
35
                repository:      repository,
×
36
                userService:     userService,
×
37
                activityService: activityService,
×
38
        }
×
39
}
×
40

41
func (s Service) Get(ctx context.Context, id string) (Policy, error) {
×
42
        return s.repository.Get(ctx, id)
×
43
}
×
44

NEW
45
func (s Service) List(ctx context.Context, filter Filters) ([]Policy, error) {
×
NEW
46
        return s.repository.List(ctx, filter)
×
UNCOV
47
}
×
48

49
func (s Service) Upsert(ctx context.Context, pol *Policy) ([]Policy, error) {
×
50
        currentUser, err := s.userService.FetchCurrentUser(ctx)
×
51
        if err != nil {
×
52
                return []Policy{}, err
×
53
        }
×
54

55
        policyID, err := s.repository.Upsert(ctx, pol)
×
56
        if err != nil {
×
57
                return []Policy{}, err
×
58
        }
×
59
        pol.ID = policyID
×
60

×
NEW
61
        policies, err := s.repository.List(ctx, Filters{})
×
62
        if err != nil {
×
63
                return []Policy{}, err
×
64
        }
×
65

66
        go func() {
×
67
                ctx := context.WithoutCancel(ctx)
×
68
                policyLogData := pol.ToLogData()
×
69
                actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
×
70
                if err := s.activityService.Log(ctx, auditKeyPolicyUpsert, actor, policyLogData); err != nil {
×
71
                        s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
×
72
                }
×
73
        }()
74

75
        return policies, err
×
76
}
77

78
func (s Service) Update(ctx context.Context, pol *Policy) ([]Policy, error) {
×
79
        currentUser, err := s.userService.FetchCurrentUser(ctx)
×
80
        if err != nil {
×
81
                return []Policy{}, err
×
82
        }
×
83

84
        policyID, err := s.repository.Update(ctx, pol)
×
85
        if err != nil {
×
86
                return []Policy{}, err
×
87
        }
×
88
        pol.ID = policyID
×
89

×
NEW
90
        policies, err := s.repository.List(ctx, Filters{})
×
91
        if err != nil {
×
92
                return []Policy{}, err
×
93
        }
×
94

95
        go func() {
×
96
                ctx := context.WithoutCancel(ctx)
×
97
                policyLogData := pol.ToLogData()
×
98
                actor := activity.Actor{ID: currentUser.ID, Email: currentUser.Email}
×
99
                if err := s.activityService.Log(ctx, auditKeyPolicyUpdate, actor, policyLogData); err != nil {
×
100
                        s.logger.Error(fmt.Sprintf("%s: %s", ErrLogActivity.Error(), err.Error()))
×
101
                }
×
102
        }()
103

104
        return policies, err
×
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc