Skip to content

Commit

Permalink
fix: now you cannot send invitation to yourself
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielllllllllllllll committed Jun 27, 2024
1 parent 164dbb8 commit 36a43e9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
8 changes: 6 additions & 2 deletions services/api/controllers/friend/friend_invite.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
func (s Service) InviteFriend(ctx context.Context, req *monify.InviteFriendRequest) (*monify.InviteFriendResponse, error) {

logger := ctx.Value(lib.LoggerContextKey{}).(*zap.Logger)
userId := ctx.Value(lib.UserIdContextKey{})
if userId == nil {
userId := ctx.Value(lib.UserIdContextKey{}).(uuid.UUID)
if userId == uuid.Nil {
return nil, status.Error(codes.Unauthenticated, "Unauthorized.")
}
receiver_nickId := req.GetReceiverNickId()
Expand All @@ -35,6 +35,10 @@ func (s Service) InviteFriend(ctx context.Context, req *monify.InviteFriendReque
return nil, status.Error(codes.Internal, "")
}

if receiverId == userId.String() {
return nil, status.Error(codes.InvalidArgument, "Cannot send invitation to yourself.")
}

inviteId := uuid.New()
_, err = db.ExecContext(ctx,
`INSERT INTO friend_invite (invite_id, sender, receiver) VALUES ($1, $2, $3)`,
Expand Down
8 changes: 6 additions & 2 deletions services/api/test/friend_invite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ func TestInviteFriend(t *testing.T) {
_, err := client.UpdateUserNickId(context.Background(), &monify.UpdateUserNickIdRequest{NickId: "test_nickId1"})
assert.NoError(t, err)
_, err = client.InviteFriend(context.TODO(), &monify.InviteFriendRequest{ReceiverNickId: "test_nickId1"})
assert.Error(t, err)
_ = client.CreateTestUser()
_, err = client.InviteFriend(context.TODO(), &monify.InviteFriendRequest{ReceiverNickId: "test_nickId1"})
assert.NoError(t, err)

// test list invitation
_ = client.CreateTestUser()
_, err = client.InviteFriend(context.TODO(), &monify.InviteFriendRequest{ReceiverNickId: "test_nickId1"})
assert.NoError(t, err)

// test list invitation
client.SetTestUser(user1)
invitaions, err := client.ListFriendInvitation(context.TODO(), &monify.FriendEmpty{})
assert.NoError(t, err)
Expand Down

0 comments on commit 36a43e9

Please sign in to comment.