Skip to content

Commit

Permalink
add more test cases and make the code DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
timlubanga committed Sep 15, 2021
1 parent 1f9f336 commit 8e08bdd
Show file tree
Hide file tree
Showing 25 changed files with 794 additions and 74 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ jobs:
- uses: actions/checkout@v2
- uses: google-github-actions/setup-gcloud@master
with:
project_id: ${{ secrets.GOOGLE_CLOUD_PROJECT }}
service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
export_default_credentials: true
project_id: ${{ secrets.GOOGLE_CLOUD_PROJECT }}
service_account_key: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS }}
export_default_credentials: true
- name: Install Go
uses: actions/setup-go@v2
with:
Expand Down Expand Up @@ -124,7 +124,7 @@ jobs:
gocov convert coverage.out > coverage.json
gocov report coverage.json > coverage_report.txt
tail coverage_report.txt
- name: Install goveralls
env:
GO111MODULE: off
Expand All @@ -136,6 +136,6 @@ jobs:

- name: Quality Gate - Test coverage shall be above threshold
env:
TESTCOVERAGE_THRESHOLD: 71.90
TESTCOVERAGE_THRESHOLD: 71.7
run: |
bash scripts/coverage.sh
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ require (
github.com/savannahghi/converterandformatter v0.0.9
github.com/savannahghi/enumutils v0.0.2
github.com/savannahghi/errorcodeutil v0.0.1
github.com/savannahghi/feedlib v0.0.1
github.com/savannahghi/feedlib v0.0.5
github.com/savannahghi/firebasetools v0.0.13
github.com/savannahghi/interserviceclient v0.0.13
github.com/savannahghi/profileutils v0.0.7
github.com/savannahghi/pubsubtools v0.0.2
github.com/savannahghi/scalarutils v0.0.2 // indirect
github.com/savannahghi/serverutils v0.0.3
github.com/savannahghi/serverutils v0.0.4
github.com/segmentio/ksuid v1.0.4
github.com/sirupsen/logrus v1.8.1
github.com/stretchr/testify v1.7.0
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ github.com/savannahghi/enumutils v0.0.2/go.mod h1:DDdjQBO1qyf5BxLzhTs1uN91drCIHH
github.com/savannahghi/errorcodeutil v0.0.1 h1:q5JYMrzXDAJ9/99fV7Mj2bFF1XOidlRgSlYHy73q2GA=
github.com/savannahghi/errorcodeutil v0.0.1/go.mod h1:nNBaBjatvoRusnDr2aRoNr4Rpmz9Z779mjcL0tr/IXk=
github.com/savannahghi/feedlib v0.0.0-20210628122532-ef88e353d4d6/go.mod h1:P6YHmWsa/TK/qTb2U1ryjuASotTQmbarArB0LYLjMo4=
github.com/savannahghi/feedlib v0.0.1 h1:BixXDyfj1Kj+f8AOwd32A/xXr5CZxrOqLitp09xDQcg=
github.com/savannahghi/feedlib v0.0.1/go.mod h1:P6YHmWsa/TK/qTb2U1ryjuASotTQmbarArB0LYLjMo4=
github.com/savannahghi/feedlib v0.0.5 h1:LQRLQoWCLFjCp4mkQncCNHJ/v9tg2D3yyzvSCnk3xRY=
github.com/savannahghi/feedlib v0.0.5/go.mod h1:PzEP7gw4w1Xuw36uIRr1H2ffd+kozWHyJCuGWO06AtQ=
github.com/savannahghi/firebasetools v0.0.5/go.mod h1:obAxyBfPaGTx/g86LF19gSmmEh5x+Tbnziatk514mUQ=
github.com/savannahghi/firebasetools v0.0.6/go.mod h1:obAxyBfPaGTx/g86LF19gSmmEh5x+Tbnziatk514mUQ=
github.com/savannahghi/firebasetools v0.0.13 h1:2eBFwzsnmhRUzxfgQZxOjedlMuyef2dz4tvpw59pJkE=
Expand All @@ -404,11 +404,10 @@ github.com/savannahghi/pubsubtools v0.0.2/go.mod h1:FQ+BxO4uA1ZkcziY4dR0uT9CKHVH
github.com/savannahghi/scalarutils v0.0.0-20210622091443-bad5089abdad/go.mod h1:Z+Dl3wc3vy5zKvthctHAtYzol1p8w27zEVRfOYueoks=
github.com/savannahghi/scalarutils v0.0.2 h1:naX6Zhv/tDbwKAKVY4bksyhnE43f2583xWesAKHb1CU=
github.com/savannahghi/scalarutils v0.0.2/go.mod h1:Z+Dl3wc3vy5zKvthctHAtYzol1p8w27zEVRfOYueoks=
github.com/savannahghi/server_utils v0.0.1 h1:q6SL4BRvkpYJujXvWwCl4vU1U+VTzKRGfNXnl0H6tfw=
github.com/savannahghi/server_utils v0.0.1/go.mod h1:Tldm4Op4ARxk8xaP3BA0V52XVZEJenkq0ck4FCdEp+I=
github.com/savannahghi/serverutils v0.0.2/go.mod h1:sLX0El0i0DKN/9cUkB8xqm5cVMP79qCZvK60EzB7Pa4=
github.com/savannahghi/serverutils v0.0.3 h1:1gX/zD6tt4tAEpcQFyzXBUp70BvUiqmJpjADqjdvFwI=
github.com/savannahghi/serverutils v0.0.3/go.mod h1:3VCEJ8BTHf/DW3WFjLqV4SznzrXaul/As2RJ5eNOO7U=
github.com/savannahghi/serverutils v0.0.4 h1:mQGAwhNgS1NPSBXqCeyywuGQFnvRxiOmHJEHPgzhWQE=
github.com/savannahghi/serverutils v0.0.4/go.mod h1:3VCEJ8BTHf/DW3WFjLqV4SznzrXaul/As2RJ5eNOO7U=
github.com/schollz/closestmatch v2.1.0+incompatible/go.mod h1:RtP1ddjLong6gTkbtmuhtR2uUrrJOpYzYRvbcPAid+g=
github.com/segmentio/ksuid v1.0.3/go.mod h1:/XUiZBD3kVx5SmUOl55voK5yeAbBNNIed+2O73XgrPE=
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
Expand Down
2 changes: 1 addition & 1 deletion gqlgen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ exec:

# model:
# filename: pkg/engagement/domain/model/models_gen.go
# package: model
# package: model

resolver:
layout: follow-schema
Expand Down
8 changes: 3 additions & 5 deletions pkg/engagement/application/authorization/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@ import (
"github.com/savannahghi/profileutils"
)

var (
enforcer *casbin.Enforcer
)


// CheckPemissions is used to check whether the permissions of a subject are set
func CheckPemissions(subject string, input profileutils.PermissionInput) (bool, error) {

enforcer := &casbin.Enforcer{}
ok, err := enforcer.Enforce(subject, input.Resource, input.Action)
if err != nil {
return false, fmt.Errorf("unable to check permissions %w", err)
Expand Down Expand Up @@ -52,5 +50,5 @@ func IsAuthorized(user *profileutils.UserInfo, permission profileutils.Permissio
return CheckAuthorization(user.Email, permission)

}
return true, nil
return false, nil
}
152 changes: 152 additions & 0 deletions pkg/engagement/application/authorization/auth_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,153 @@
package authorization

import (
"testing"

"github.com/savannahghi/profileutils"
)

func TestCheckPemissions(t *testing.T) {
type args struct {
subject string
input profileutils.PermissionInput
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "happy: permission passed",
args: args{
subject: "view",
input: profileutils.PermissionInput{
Resource: "action_view",
Action: "view",
},
},
wantErr: false,
},
{
name: "happy: permission passed",
args: args{
subject: "delete",
input: profileutils.PermissionInput{
Resource: "delete_item",
Action: "delete",
},
},
wantErr: false,
},
{
name: "happy: permission passed",
args: args{
subject: "view",
input: profileutils.PermissionInput{
Resource: "resolve_item",
Action: "resolve",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := CheckPemissions(tt.args.subject, tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("CheckPemissions() error = %v, wantErr %v", err, tt.wantErr)
return
}

})
}
}

func TestCheckAuthorization(t *testing.T) {
type args struct {
subject string
input profileutils.PermissionInput
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "happy: permission passed",
args: args{
subject: "can publish",
input: profileutils.PermissionInput{
Resource: "publish_item",
Action: "create",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := CheckAuthorization(tt.args.subject, tt.args.input)
if (err != nil) != tt.wantErr {
t.Errorf("CheckPemissions() error = %v, wantErr %v", err, tt.wantErr)
return
}

})
}
}

func TestIsAuthorized(t *testing.T) {
type args struct {
user *profileutils.UserInfo
permission profileutils.PermissionInput
}

user1 := &profileutils.UserInfo{
DisplayName: "",
Email: "timhealthcloud.co.ke",
PhoneNumber: "+254714568338",
PhotoURL: "",
ProviderID: "",
UID: "",
}
user2 := &profileutils.UserInfo{}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "happy: permission passed",
args: args{
user: user1,
permission: profileutils.PermissionInput{
Resource: "publish_item",
Action: "create",
},
},
wantErr: false,
},

{
name: "sad: permission denied",
args: args{
user: user2,
permission: profileutils.PermissionInput{
Resource: "publish_item",
Action: "create",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := IsAuthorized(tt.args.user, tt.args.permission)
if (err != nil) != tt.wantErr {
t.Errorf("CheckPemissions() error = %v, wantErr %v", err, tt.wantErr)
return
}

})
}
}
8 changes: 8 additions & 0 deletions pkg/engagement/application/common/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ func TestEpochTimetoStandardTime(t *testing.T) {
},
panics: true,
},

{
name: "sad case: invalid time of delivery provided",
args: args{
timeOfDelivery: "invalidtime",
},
panics: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
Loading

0 comments on commit 8e08bdd

Please sign in to comment.