Skip to content

Commit

Permalink
tests: added tests for delete group bill
Browse files Browse the repository at this point in the history
  • Loading branch information
SpeedReach committed Jun 1, 2024
1 parent 730c0fd commit bd66e36
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 10 deletions.
30 changes: 22 additions & 8 deletions internal/services/group_bill/delete_bill.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package group_bill
import (
"context"
"database/sql"
"github.com/google/uuid"
"monify/internal/middlewares"
monify "monify/protobuf/gen/go"

Expand All @@ -11,26 +12,38 @@ import (
"google.golang.org/grpc/status"
)

func (s Service) DeleteBill(ctx context.Context, req *monify.DeleteGroupBillRequest) (*monify.GroupGroupBillEmpty, error) {
func (s Service) DeleteGroupBill(ctx context.Context, req *monify.DeleteGroupBillRequest) (*monify.GroupGroupBillEmpty, error) {
logger := ctx.Value(middlewares.LoggerContextKey{}).(*zap.Logger)

userId := ctx.Value(middlewares.UserIdContextKey{})
if userId == nil {
userId, ok := ctx.Value(middlewares.UserIdContextKey{}).(uuid.UUID)
if !ok {
return nil, status.Error(codes.Unauthenticated, "Unauthorized.")
}

db := ctx.Value(middlewares.StorageContextKey{}).(*sql.DB)

//Check permission
rows := db.QueryRowContext(ctx, `
SELECT COUNT(*) FROM group_bill
LEFT JOIN group_member gm ON group_bill.group_id = gm.group_id
WHERE group_bill.bill_id = $1 AND gm.user_id = $2
`, req.BillId, userId)
var count int
err := rows.Scan(&count)
if err != nil {
logger.Error("Failed to check permission", zap.Error(err))
}
if count != 1 {
return nil, status.Error(codes.PermissionDenied, "No permission")
}

//START transaction
tx, err := db.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadUncommitted})
if err != nil {
logger.Error("Failed to begin transaction", zap.Error(err))
return nil, status.Error(codes.Internal, "Internal")
}
defer tx.Rollback()

crearte_group_bill_response := &monify.CreateGroupBillResponse{}
req.BillId = crearte_group_bill_response.GetBillId()

//Start delete
_, err = tx.ExecContext(ctx,
`DELETE FROM group_bill WHERE bill_id = $1`, req.BillId,
)
Expand All @@ -55,6 +68,7 @@ func (s Service) DeleteBill(ctx context.Context, req *monify.DeleteGroupBillRequ
return nil, err
}

//Commit
if err = tx.Commit(); err != nil {
logger.Error("Failed to commit transaction", zap.Error(err))
return nil, status.Error(codes.Internal, "Internal")
Expand Down
19 changes: 17 additions & 2 deletions internal/test/group_bill_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (

func TestCreateAndGetGroupBill(t *testing.T) {
client := GetTestClient(t)
_ = client.CreateTestUser()
user1 := client.CreateTestUser()

//Create two users and join same group
group, err := client.CreateGroup(context.TODO(), &monify.CreateGroupRequest{Name: "test"})
assert.NoError(t, err)
inviteCode, err := client.GenerateInviteCode(context.Background(), &monify.GenerateInviteCodeRequest{GroupId: group.GroupId})
Expand Down Expand Up @@ -41,14 +42,28 @@ func TestCreateAndGetGroupBill(t *testing.T) {
},
},
})

assert.NoError(t, err)

//Check group bill exists
response2, err := client.GetGroupBills(context.TODO(), &monify.GetGroupBillsRequest{GroupId: group.GroupId})
assert.NoError(t, err)
assert.Len(t, response2.GroupBills[0].SplitPeople, 2)
assert.Len(t, response2.GroupBills[0].PrepaidPeople, 1)
assert.Equal(t, response1.BillId, response2.GroupBills[0].BillId)
assert.Equal(t, group.MemberId, response2.GroupBills[0].PrepaidPeople[0].MemberId)
assert.Equal(t, 250.0, response2.GroupBills[0].PrepaidPeople[0].Amount)

//Test with no permission user
client.CreateTestUser()
_, err = client.GetGroupBills(context.TODO(), &monify.GetGroupBillsRequest{GroupId: group.GroupId})
assert.Error(t, err, "Should have no permission")
_, err = client.DeleteGroupBill(context.Background(), &monify.DeleteGroupBillRequest{BillId: response1.BillId})
assert.Error(t, err, "Should have no permission")

//Test delete with permission user
client.SetTestUser(user1)
_, err = client.DeleteGroupBill(context.Background(), &monify.DeleteGroupBillRequest{BillId: response1.BillId})
assert.NoError(t, err)
response2, err = client.GetGroupBills(context.TODO(), &monify.GetGroupBillsRequest{GroupId: group.GroupId})
assert.Empty(t, response2.GroupBills)
}

0 comments on commit bd66e36

Please sign in to comment.