{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install websockets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "import json\n",
    "import base64\n",
    "from dotenv import dotenv_values\n",
    "\n",
    "# Connect to a websocket server\n",
    "import asyncio\n",
    "import websockets\n",
    "\n",
    "config = dotenv_values(\".test.env\")\n",
    "\n",
    "authApiUrl = config['BIGSCREEN_API_URL']\n",
    "cloudApiUrl = config['BIGSCREEN_CLOUD_API_URL']\n",
    "wssUrl = config['BIGSCREEN_WSS_URL']\n",
    "apiBearerToken = config['BIGSCREEN_API_KEY']\n",
    "\n",
    "headers = {\n",
    "    \"Content-Type\": \"application/json\",\n",
    "    \"Authorization\": f\"Bearer {apiBearerToken}\"\n",
    "}\n",
    "\n",
    "def getHeaders(accessToken):\n",
    "    return {\n",
    "        \"Content-Type\": \"application/json\",\n",
    "        \"Authorization\": f\"Bearer {apiBearerToken}\",\n",
    "        \"x-access-token\": accessToken\n",
    "    }\n",
    "\n",
    "# Convert wssPayload to json, then convert to base64 string\n",
    "def getWebsocketPayload(accessToken):\n",
    "    wssPayload = {\n",
    "        \"accessToken\": accessToken,\n",
    "        \"systemInfo\": {\n",
    "            \"deviceUniqueIdentifier\": \"python_notebook\"\n",
    "        }\n",
    "    }\n",
    "    return base64.b64encode(json.dumps(wssPayload).encode(\"utf-8\")).decode(\"utf-8\")\n",
    "\n",
    "\n",
    "def login(email, password):\n",
    "    payload = {\n",
    "        \"email\": email,\n",
    "        \"password\": password\n",
    "    }\n",
    "    r = requests.post(f\"{authApiUrl}/login\", headers=headers, json=payload)\n",
    "    return r.headers[\"x-refresh-token\"], r.headers[\"x-access-token\"]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "headers = {\n",
    "    \"Content-Type\": \"application/json\",\n",
    "    \"Authorization\": f\"Bearer {apiBearerToken}\"\n",
    "}\n",
    "r = requests.get(f\"{authApiUrl}/region\", headers=headers)\n",
    "assert r.status_code == 200, f\"status code should be 200 (actual: {r.status_code} {r.text})\"\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the file from /data/TestAccountCredentials.json and parse it as json\n",
    "f = open(\"../tests/utils/data/TestAccountCredentials.json\")\n",
    "testAccounts = json.load(f)\n",
    "\n",
    "async def sendFriendRequest(index, targetSocialId):\n",
    "    accountData = testAccounts[index]\n",
    "    refreshToken, accessToken = login(accountData[\"email\"], accountData[\"password\"])\n",
    "    headers = getHeaders(accessToken)\n",
    "\n",
    "    websocketPayload = getWebsocketPayload(accessToken)\n",
    "    # Connect to the websocket server\n",
    "    uri = f\"{wssUrl}/{websocketPayload}\"\n",
    "    await websockets.connect(uri)\n",
    "\n",
    "    # Get the profile of the account after login\n",
    "    r = requests.get(f\"{cloudApiUrl}/social/profile\", headers=headers)\n",
    "    profile = r.json()\n",
    "    socialId = profile[\"socialId\"]\n",
    "\n",
    "    notification = {\n",
    "        \"recipientSocialId\": targetSocialId,\n",
    "        \"notificationType\": \"FriendRequest\",\n",
    "        \"version\": \"0.908.25\"\n",
    "    }\n",
    "\n",
    "    # Send a friend request to the first account\n",
    "    headers = getHeaders(accessToken)\n",
    "    r = requests.post(f\"{cloudApiUrl}/social/notification\", headers=headers, json=notification)\n",
    "    #assert r.status_code == 200, f\"status code should be 200 (actual: {r.status_code} {r.text})\"\n",
    "    print(r.status_code)\n",
    "\n",
    "targetSocialId = \"6RtV0JTYt3wBzXNuBCfEuOV5kZA\"\n",
    "\n",
    "for i in range(0, len(testAccounts)):\n",
    "    await sendFriendRequest(i, targetSocialId)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "payload2 = {\n",
    "    \"email\": \"qa+basic@bigscreenvr.com\",\n",
    "    \"password\": \"LaWGGLpeZwM62iodwBveckyj68oPEVCs\"\n",
    "}\n",
    "\n",
    "refreshToken, accessToken = login(payload2[\"email\"], payload2[\"password\"])\n",
    "\n",
    "# Convert wssPayload to json, then convert to base64 string\n",
    "wssPayload = getWebsocketPayload(accessToken)\n",
    "\n",
    "# Connect to the websocket server\n",
    "uri = f\"{wssUrl}/{wssPayload}\"\n",
    "print(uri)\n",
    "await websockets.connect(uri)\n",
    "\n",
    "r = requests.get(f\"{cloudApiUrl}/social/profile\", headers=getHeaders(accessToken))\n",
    "#profile = r.json()\n",
    "#print(profile)\n",
    "#targetSocialId = profile[\"id\"]\n",
    "\n",
    "r = requests.get(f\"{cloudApiUrl}/social/notifications\", headers=getHeaders(accessToken))\n",
    "# print(r.json())\n",
    "\n",
    "r = requests.get(f\"{cloudApiUrl}/social/history?type=User\", headers=getHeaders(accessToken))\n",
    "print(r.json())\n",
    "\n",
    "# Loop through the test accounts and send test friend requests\n",
    "#for i in range(0, len(testAccounts)):\n",
    "#    await sendFriendRequest(i, targetSocialId)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "targetSocialId = \"lKeWbb9JbqM7Qy6Tujwfqi7C03Y\"\n",
    "\n",
    "# Send a friend request to the target social id\n",
    "r = requests.put(f\"{cloudApiUrl}/social/profile/{targetSocialId}/friend_request\", headers=getHeaders())\n",
    "assert r.status_code == 200, f\"status code should be 200 (actual: {r.status_code})\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "targetSocialId = \"lKeWbb9JbqM7Qy6Tujwfqi7C03Y\"\n",
    "\n",
    "# Send a friend request to the target social id\n",
    "r = requests.put(f\"{cloudApiUrl}/social/profile/{targetSocialId}/friend_request\", headers=getHeaders())\n",
    "assert r.status_code == 200, f\"status code should be 200 (actual: {r.status_code})\""
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
