noise-server/test-api.sh
EliasSchriefer f8a4775bcc Add retrieving limited number of messages
Optional `lastMsg` now works by getting all messages with a newer
timestamp than the reference message.
  Optional `limit` works using SQL ~magic~ keyword `LIMIT`.
  Also `limit` has to be at least 1.
2021-12-19 01:05:13 +01:00

46 lines
6.7 KiB
Bash
Executable File

#!/usr/bin/env bash
echo Create users @a, @b, and @c:
userIDs=$(curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data '{"query":"mutation addUsers($a: NewUser!, $b: NewUser!, $c: NewUser!) {\n a: newUser(newUser: $a)\n b: newUser(newUser: $b)\n c: newUser(newUser: $c)\n}","variables":{"a":{"userName":"@a","passwordHash":"1234567890"},"b":{"userName":"@b","passwordHash":"1234567890"},"c":{"userName":"@c","passwordHash":"1234567890"}},"operationName":"addUsers"}' | jq .data)
echo "$userIDs" | jq .
a=$(echo "$userIDs" | jq -r .a)
b=$(echo "$userIDs" | jq -r .b)
c=$(echo "$userIDs" | jq -r .c)
echo
echo Create chats between @a and @b, @a and @c, and group chats between @a, @b, and @c:
curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"mutation createChat(\$a: ID!, \$b: ID!, \$c: ID!, \$pwHash: String!) {\n ab: newChat(user: \$a, passwordHash: \$pwHash, with: \$b)\n ac: newChat(user: \$a, passwordHash: \$pwHash, with: \$c)\n abc: newGroupChat(user: \$a, passwordHash: \$pwHash, with: [\$b, \$c], title: \\\"ABC\\\")\n}\",\"variables\":{\"pwHash\":\"1234567890\",\"a\":\"$a\",\"b\":\"$b\",\"c\":\"$c\"},\"operationName\":\"createChat\"}" | jq .data
echo
echo List all users\' chats:
chatIDs=$(curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"query listChats(\$a: ID!, \$b: ID!, \$c: ID!, \$pwHash: String!) {\n aChats: chats(user: \$a, passwordHash: \$pwHash) {\n id\n users\n }\n aGroupChats: groupChats(user: \$a, passwordHash: \$pwHash) {\n id\n title\n description\n users\n }\n bChats: chats(user: \$b, passwordHash: \$pwHash) {\n id\n users\n }\n bGroupChats: groupChats(user: \$b, passwordHash: \$pwHash) {\n id\n title\n description\n users\n }\n cChats: chats(user: \$c, passwordHash: \$pwHash) {\n id\n users\n }\n cGroupChats: groupChats(user: \$c, passwordHash: \$pwHash) {\n id\n title\n description\n users\n }\n}\",\"variables\":{\"pwHash\":\"1234567890\",\"a\":\"$a\",\"b\":\"$b\",\"c\":\"$c\"},\"operationName\":\"listChats\"}" | jq .data)
echo "$chatIDs" | jq .
ab=$(echo "$chatIDs" | jq -r .aChats[0].id)
abc=$(echo "$chatIDs" | jq -r .aGroupChats[0].id)
echo
echo Send a private/group message:
sentMessageIDs=$(curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"mutation sendMessages(\$a: ID!, \$pwHash: String!, \$bChat: ID!, \$abc: ID!, \$msg: MessageInput!) {\n chat: sendMessage(user: \$a, passwordHash: \$pwHash, chat: \$bChat, msg: \$msg)\n group: sendMessage(user: \$a, passwordHash: \$pwHash, chat: \$abc, msg: \$msg)\n}\",\"variables\":{\"pwHash\":\"1234567890\",\"a\":\"$a\",\"bChat\":\"$ab\",\"abc\":\"$abc\",\"msg\":{\"msgType\":\"TEXT\",\"content\":\"Test\"}},\"operationName\":\"sendMessages\"}" | jq .data)
echo "$sentMessageIDs" | jq .
lastChatMsg=$(echo "$sentMessageIDs" | jq -r .chat)
lastGroupChatMsg=$(echo "$sentMessageIDs" | jq -r .group)
echo
echo Get private/group messages:
curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"query getMessages(\$a: ID!, \$pwHash: String!, \$chat: ID!, \$groupChat: ID!) {\n chat: getMessages(user: \$a, passwordHash: \$pwHash, chat: \$chat) {\n id\n timestamp\n sender\n msgType\n content\n hideFor\n seenBy\n }\n groupChat: getMessages(user: \$a, passwordHash: \$pwHash, chat: \$groupChat) {\n id\n timestamp\n sender\n msgType\n content\n hideFor\n seenBy\n }\n}\",\"variables\":{\"a\":\"$a\",\"pwHash\":\"1234567890\",\"chat\":\"$ab\",\"groupChat\":\"$abc\"},\"operationName\":\"getMessages\"}" | jq .data
echo
echo Send a new private/group message:
curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"mutation sendNewMessages(\$a: ID!, \$pwHash: String!, \$chat: ID!, \$groupChat: ID!, \$msg: MessageInput!) {\n chat: sendMessage(user: \$a, passwordHash: \$pwHash, chat: \$chat, msg: \$msg)\n group: sendMessage(user: \$a, passwordHash: \$pwHash, chat: \$groupChat, msg: \$msg)\n}\",\"variables\":{\"a\":\"$a\",\"pwHash\":\"1234567890\",\"chat\":\"$ab\",\"groupChat\":\"$abc\",\"msg\":{\"msgType\":\"TEXT\",\"content\":\"New Message!\"}},\"operationName\":\"sendNewMessages\"}" | jq .data
echo
echo Send a second new private/group message:
curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"mutation sendNewMessages(\$a: ID!, \$pwHash: String!, \$chat: ID!, \$groupChat: ID!, \$msg: MessageInput!) {\n chat: sendMessage(user: \$a, passwordHash: \$pwHash, chat: \$chat, msg: \$msg)\n group: sendMessage(user: \$a, passwordHash: \$pwHash, chat: \$groupChat, msg: \$msg)\n}\",\"variables\":{\"a\":\"$a\",\"pwHash\":\"1234567890\",\"chat\":\"$ab\",\"groupChat\":\"$abc\",\"msg\":{\"msgType\":\"TEXT\",\"content\":\"New Message!\"}},\"operationName\":\"sendNewMessages\"}" | jq .data
echo
echo Get new private/group messages:
curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"query getMessages(\$a: ID!, \$pwHash: String!, \$chat: ID!, \$groupChat: ID!, \$lastChatMsg: ID!, \$lastGroupChatMsg: ID!) {\n chat: getMessages(user: \$a, passwordHash: \$pwHash, chat: \$chat, lastMsg: \$lastChatMsg) {\n id\n timestamp\n sender\n msgType\n content\n hideFor\n seenBy\n }\n groupChat: getMessages(user: \$a, passwordHash: \$pwHash, chat: \$groupChat, lastMsg: \$lastGroupChatMsg) {\n id\n timestamp\n sender\n msgType\n content\n hideFor\n seenBy\n }\n}\",\"variables\":{\"a\":\"$a\",\"pwHash\":\"1234567890\",\"chat\":\"$ab\",\"groupChat\":\"$abc\",\"lastChatMsg\":\"$lastChatMsg\",\"lastGroupChatMsg\":\"$lastGroupChatMsg\"},\"operationName\":\"getMessages\"}" | jq .data
echo
echo Get new private/group messages with a limit of 1:
curl "localhost:8080/graphql" -X POST -H "content-type: application/json" --data "{\"query\":\"query getMessages(\$a: ID!, \$pwHash: String!, \$chat: ID!, \$groupChat: ID!, \$lastChatMsg: ID!, \$lastGroupChatMsg: ID!, \$limit: Int) {\n chat: getMessages(user: \$a, passwordHash: \$pwHash, chat: \$chat, lastMsg: \$lastChatMsg, limit: \$limit) {\n id\n timestamp\n sender\n msgType\n content\n hideFor\n seenBy\n }\n groupChat: getMessages(user: \$a, passwordHash: \$pwHash, chat: \$groupChat, lastMsg: \$lastGroupChatMsg, limit: \$limit) {\n id\n timestamp\n sender\n msgType\n content\n hideFor\n seenBy\n }\n}\",\"variables\":{\"a\":\"$a\",\"pwHash\":\"1234567890\",\"chat\":\"$ab\",\"groupChat\":\"$abc\",\"lastChatMsg\":\"$lastChatMsg\",\"lastGroupChatMsg\":\"$lastGroupChatMsg\",\"limit\":1},\"operationName\":\"getMessages\"}" | jq .data