Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc comms cleanup #4610

Merged
merged 3 commits into from
Jan 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion comms/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ fmt::

test::
docker compose up -d comdb
go test ./... -count=1 -p=1
go test ./...



Expand Down
54 changes: 29 additions & 25 deletions comms/internal/rpcz/chat_block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package rpcz

import (
"fmt"
"math/rand"
"strconv"
"testing"
"time"

Expand All @@ -22,81 +24,83 @@ func TestChatBlocking(t *testing.T) {

tx := db.Conn.MustBegin()

// TODO test queries
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
user1Id := seededRand.Int31()
user2Id := seededRand.Int31()

assertBlocked := func(blockerUserId int, blockeeUserId int, timestamp time.Time, expected int) {
assertBlocked := func(blockerUserId int32, blockeeUserId int32, timestamp time.Time, expected int) {
row := tx.QueryRow("select count(*) from chat_blocked_users where blocker_user_id = $1 and blockee_user_id = $2 and created_at = $3", blockerUserId, blockeeUserId, timestamp)
var count int
err = row.Scan(&count)
assert.NoError(t, err)
assert.Equal(t, expected, count)
}

// validate 91 can block 92
// validate user1Id can block user2Id
{
hashUserId, err := misc.EncodeHashId(92)
encodedUserId, err := misc.EncodeHashId(int(user2Id))
assert.NoError(t, err)
exampleRpc := schema.RawRPC{
Params: []byte(fmt.Sprintf(`{"user_id": "%s"}`, hashUserId)),
Params: []byte(fmt.Sprintf(`{"user_id": "%s"}`, encodedUserId)),
}

chatBlock := string(schema.RPCMethodChatBlock)

err = Validators[chatBlock](tx, 91, exampleRpc)
err = Validators[chatBlock](tx, user1Id, exampleRpc)
assert.NoError(t, err)
}

// user 91 blocks 92
// user1Id blocks user2Id
messageTs := time.Now()
err = chatBlock(tx, 91, 92, messageTs)
err = chatBlock(tx, user1Id, user2Id, messageTs)
assert.NoError(t, err)
assertBlocked(91, 92, messageTs, 1)
assertBlocked(user1Id, user2Id, messageTs, 1)

// assert no update if duplicate block requests
duplicateMessageTs := time.Now()
err = chatBlock(tx, 91, 92, duplicateMessageTs)
err = chatBlock(tx, user1Id, user2Id, duplicateMessageTs)
assert.NoError(t, err)
assertBlocked(91, 92, messageTs, 1)
assertBlocked(91, 92, duplicateMessageTs, 0)
assertBlocked(user1Id, user2Id, messageTs, 1)
assertBlocked(user1Id, user2Id, duplicateMessageTs, 0)

// validate 91 and 92 cannot create a chat with each other
// validate user1Id and user2Id cannot create a chat with each other
{
chatId := "chat1"
user91HashId, err := misc.EncodeHashId(91)
chatId := strconv.Itoa(seededRand.Int())
user1IdEncoded, err := misc.EncodeHashId(int(user1Id))
assert.NoError(t, err)
user92HashId, err := misc.EncodeHashId(92)
user2IdEncoded, err := misc.EncodeHashId(int(user2Id))
assert.NoError(t, err)

exampleRpc := schema.RawRPC{
Params: []byte(fmt.Sprintf(`{"chat_id": "%s", "invites": [{"user_id": "%s", "invite_code": "1"}, {"user_id": "%s", "invite_code": "2"}]}`, chatId, user91HashId, user92HashId)),
Params: []byte(fmt.Sprintf(`{"chat_id": "%s", "invites": [{"user_id": "%s", "invite_code": "1"}, {"user_id": "%s", "invite_code": "2"}]}`, chatId, user1IdEncoded, user2IdEncoded)),
}

chatCreate := string(schema.RPCMethodChatCreate)

err = Validators[chatCreate](tx, 91, exampleRpc)
err = Validators[chatCreate](tx, user1Id, exampleRpc)
assert.ErrorContains(t, err, "Cannot chat with a user you have blocked or user who has blocked you")
}

// validate 91 and 92 cannot message each other
// validate user1Id and user2Id cannot message each other
{
// Assume chat was already created before blocking
chatId := "chat1"
SetupChatWithMembers(t, tx, chatId, 91, 92)
chatId := strconv.Itoa(seededRand.Int())
SetupChatWithMembers(t, tx, chatId, user1Id, user2Id)

exampleRpc := schema.RawRPC{
Params: []byte(fmt.Sprintf(`{"chat_id": "%s", "message_id": "1", "message": "test"}`, chatId)),
}

chatMessage := string(schema.RPCMethodChatMessage)

err = Validators[chatMessage](tx, 91, exampleRpc)
err = Validators[chatMessage](tx, user1Id, exampleRpc)
assert.ErrorContains(t, err, "Cannot chat with a user you have blocked or user who has blocked you")
}

// user 91 unblocks 92
err = chatUnblock(tx, 91, 92)
// user1Id unblocks user2Id
err = chatUnblock(tx, user1Id, user2Id)
assert.NoError(t, err)
assertBlocked(91, 92, messageTs, 0)
assertBlocked(user1Id, user2Id, messageTs, 0)

tx.Rollback()
}
29 changes: 18 additions & 11 deletions comms/internal/rpcz/chat_delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package rpcz
import (
"database/sql"
"fmt"
"math/rand"
"strconv"
"testing"
"time"

Expand All @@ -20,10 +22,15 @@ func TestChatDeletion(t *testing.T) {

tx := db.Conn.MustBegin()

chatId := "chat1"
SetupChatWithMembers(t, tx, chatId, 91, 92)
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
user1Id := seededRand.Int31()
user2Id := seededRand.Int31()
user3Id := seededRand.Int31()
chatId := strconv.Itoa(seededRand.Int())

assertDeleted := func(chatId string, userId int, expectDeleted bool) {
SetupChatWithMembers(t, tx, chatId, user1Id, user2Id)

assertDeleted := func(chatId string, userId int32, expectDeleted bool) {
row := tx.QueryRow("select cleared_history_at from chat_member where chat_id = $1 and user_id = $2", chatId, userId)
var clearedHistoryAt sql.NullTime
err = row.Scan(&clearedHistoryAt)
Expand All @@ -35,29 +42,29 @@ func TestChatDeletion(t *testing.T) {
}
}

// validate 91 and 92 can delete their chats
// validate user1Id and user2Id can delete their chats
{
exampleRpc := schema.RawRPC{
Params: []byte(fmt.Sprintf(`{"chat_id": "%s"}`, chatId)),
}

chatDelete := string(schema.RPCMethodChatDelete)

err = Validators[chatDelete](tx, 91, exampleRpc)
err = Validators[chatDelete](tx, user1Id, exampleRpc)
assert.NoError(t, err)

err = Validators[chatDelete](tx, 93, exampleRpc)
err = Validators[chatDelete](tx, user3Id, exampleRpc)
assert.ErrorIs(t, err, sql.ErrNoRows)
}

// 91 deletes the chat
// user1Id deletes the chat
deleteTs := time.Now()
err = chatDelete(tx, 91, chatId, deleteTs)
err = chatDelete(tx, user1Id, chatId, deleteTs)
assert.NoError(t, err)
assertDeleted(chatId, 91, true)
assertDeleted(chatId, user1Id, true)

// chat is not deleted for 92
assertDeleted(chatId, 92, false)
// chat is not deleted for user2Id
assertDeleted(chatId, user2Id, false)

tx.Rollback()
}
15 changes: 10 additions & 5 deletions comms/internal/rpcz/chat_permissions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package rpcz

import (
"fmt"
"math/rand"
"testing"
"time"

"comms.audius.co/db"
"comms.audius.co/schema"
Expand All @@ -18,6 +20,9 @@ func TestChatPermissions(t *testing.T) {

tx := db.Conn.MustBegin()

seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
user1Id := seededRand.Int31()

assertPermissions := func(userId int32, permits schema.ChatPermission, expected int) {
row := tx.QueryRow("select count(*) from chat_permissions where user_id = $1 and permits = $2", userId, permits)
var count int
Expand All @@ -26,25 +31,25 @@ func TestChatPermissions(t *testing.T) {
assert.Equal(t, expected, count)
}

// validate 91 can set permissions
// validate user1Id can set permissions
{
exampleRpc := schema.RawRPC{
Params: []byte(fmt.Sprintf(`{"permit": "all"}`)),
}

chatPermit := string(schema.RPCMethodChatPermit)

err = Validators[chatPermit](tx, 91, exampleRpc)
err = Validators[chatPermit](tx, user1Id, exampleRpc)
assert.NoError(t, err)
}

// 91 sets chat permissions to followees only
userId := int32(91)
// user1Id sets chat permissions to followees only
userId := int32(user1Id)
err = chatSetPermissions(tx, userId, schema.Followees)
assert.NoError(t, err)
assertPermissions(userId, schema.Followees, 1)

// 91 changes chat permissions to none
// user1Id changes chat permissions to none
err = chatSetPermissions(tx, userId, schema.None)
assert.NoError(t, err)
assertPermissions(userId, schema.Followees, 0)
Expand Down
Loading