diff --git a/.github/workflows/jest.yml b/.github/workflows/jest.yml index 4d36655..632feb5 100644 --- a/.github/workflows/jest.yml +++ b/.github/workflows/jest.yml @@ -26,7 +26,7 @@ jobs: - name: Build vite page run: | cd client - npm i + npm install npm run build test: @@ -41,11 +41,11 @@ jobs: - name: Install dependencies run: | - npm i + npm install cd server - npm i + npm install cd ../client - npm i + npm install - name: Create server env file run: | diff --git a/client/src/App.tsx b/client/src/App.tsx index 79a157c..1babb84 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -34,26 +34,30 @@ function AppContent() { }, []); return ( -
-
- {session ? ( - <> -

Soundscape

- - - - - ) : ( -
-
-
-

Soundscape

-

Soundscape is a music-sharing social platform that creates a map-based feed of music sharing personal moments. This enables users to discover music through friend's real world experiences.

- +
+
+ {session ? ( + <> +

Soundscape

+ + + + + ) : ( +
+
+
+

Soundscape

+

+ Soundscape is a music-sharing social platform that creates a map-based + feed of music sharing personal moments. This enables users to discover + music through friend's real world experiences. +

+ +
+
-
-
- )} + )}
); } diff --git a/client/src/MakePost.tsx b/client/src/MakePost.tsx index bb350fc..78a007f 100644 --- a/client/src/MakePost.tsx +++ b/client/src/MakePost.tsx @@ -9,10 +9,8 @@ interface PostProp { position: [number, number]; timestamp: number; }) => void; - userIdStr: string; } const MakePost = ({ onPostCreated, userIdStr}: PostProp) => { - const [userId, setUserId] = useState(userIdStr); const [isOpen, setIsOpen] = useState(false); const [songName, setSongName] = useState(""); const [artistName, setArtistName] = useState(""); diff --git a/client/src/MapView.tsx b/client/src/MapView.tsx index 14bcbbb..1557bd0 100644 --- a/client/src/MapView.tsx +++ b/client/src/MapView.tsx @@ -21,7 +21,6 @@ L.Icon.Default.mergeOptions({ interface PostProp { latestPost: { - feedRefresh: number; songName: string; artistName: string; comment: string; @@ -35,7 +34,7 @@ type FeedPost = { postid: number; userid: string; time: string; - location: string; // e.g. "(44.565,-123.276)" + location: string; comment: { songTitle?: string; artistName?: string; @@ -95,9 +94,9 @@ function MapView({ latestPost, feedRefresh }: PostProp) { } const data: FeedPost[] = await res.json(); + console.log("Feed data from backend:", data); setFeedPosts(data); - setFeedPosts(data); } catch (err) { console.error("Failed to fetch feed:", err); } diff --git a/env-setup.sh b/env-setup.sh index fd67aca..7308c01 100755 --- a/env-setup.sh +++ b/env-setup.sh @@ -50,7 +50,7 @@ if [ ! -f "server/.env" ] || [ ! -f "client/.env" ]; then # create HTTPS keys pass=$(getHTTPSPassword) - $(generateHTTPSKeyAndCert $pass) + generateHTTPSKeyAndCert "$pass" # write server env file port="8000" @@ -67,7 +67,7 @@ if [ ! -f "server/.env" ] || [ ! -f "client/.env" ]; then echo "# .env" >> client/.env echo "VITE_PUBLIC_SUPABASE_URL=$supabaseUrl" >> client/.env echo "VITE_PUBLIC_SUPABASE_PUBLISHABLE_DEFAULT_KEY=$supabaseKey" >> client/.env - echo "VITE_SERVER_URL=https://localhost:8000" >> client/.env + echo "VITE_SERVER_URL=http://localhost:8000" >> client/.env echo "VITE_COOKIE_HOST_URL=.localhost" >> client/.env # check HTTPS certifications @@ -78,7 +78,7 @@ elif [ ! -f "server/cert.pem" ] || [ ! -f "server/key.pem" ]; then passEnvLine=$(grep "HTTPS_KEY_PASSPHRASE=" server/.env) # get password for keys - if [ -z passEnvLine ]; then + if [ -z "$passEnvLine" ]; then pass=$(getHTTPSPassword) echo "HTTPS_KEY_PASSPHRASE=$pass" >> server/.env diff --git a/server/package-lock.json b/server/package-lock.json index d743f30..b633c4e 100644 --- a/server/package-lock.json +++ b/server/package-lock.json @@ -33,6 +33,8 @@ "@jest/globals": "^30.2.0", "@types/cors": "^2.8.19", "@types/jest": "^30.0.0", + "@types/supertest": "^7.2.0", + "supertest": "^7.2.2", "ts-jest": "^29.4.6" } }, @@ -1255,6 +1257,29 @@ "@tybys/wasm-util": "^0.10.0" } }, + "node_modules/@noble/hashes": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.8.0.tgz", + "integrity": "sha512-jCs9ldd7NwzpgXDIf6P3+NrHh9/sD6CQdxHyjQI+h/6rDNo88ypBxxz45UDuZHz9r3tNz7N/VInSVoVdtXEI4A==", + "dev": true, + "license": "MIT", + "engines": { + "node": "^14.21.3 || >=16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@paralleldrive/cuid2": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/@paralleldrive/cuid2/-/cuid2-2.3.1.tgz", + "integrity": "sha512-XO7cAxhnTZl0Yggq6jOgjiOHhbgcO4NqFqwSmQpjK3b6TEE6Uj/jfSk6wzYyemh3+I0sHirKSetjQwn5cZktFw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@noble/hashes": "^1.1.5" + } + }, "node_modules/@pkgjs/parseargs": { "version": "0.11.0", "resolved": "https://registry.npmjs.org/@pkgjs/parseargs/-/parseargs-0.11.0.tgz", @@ -1572,6 +1597,13 @@ "@types/node": "*" } }, + "node_modules/@types/cookiejar": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/@types/cookiejar/-/cookiejar-2.1.5.tgz", + "integrity": "sha512-he+DHOWReW0nghN24E1WUqM0efK4kI9oTqDm6XmK8ZPe2djZ90BSNdGnIyCLzCPw7/pogPlGbzI2wHGGmi4O/Q==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/cors": { "version": "2.8.19", "resolved": "https://registry.npmjs.org/@types/cors/-/cors-2.8.19.tgz", @@ -1671,6 +1703,13 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/methods": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/@types/methods/-/methods-1.1.4.tgz", + "integrity": "sha512-ymXWVrDiCxTBE3+RIrrP533E70eA+9qu7zdWoHuOmGujkYtzf4HQF96b8nwHLqhuf4ykX61IGRIB38CC6/sImQ==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "25.0.10", "resolved": "https://registry.npmjs.org/@types/node/-/node-25.0.10.tgz", @@ -1754,6 +1793,30 @@ "dev": true, "license": "MIT" }, + "node_modules/@types/superagent": { + "version": "8.1.9", + "resolved": "https://registry.npmjs.org/@types/superagent/-/superagent-8.1.9.tgz", + "integrity": "sha512-pTVjI73witn+9ILmoJdajHGW2jkSaOzhiFYF1Rd3EQ94kymLqB9PjD9ISg7WaALC7+dCHT0FGe9T2LktLq/3GQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/cookiejar": "^2.1.5", + "@types/methods": "^1.1.4", + "@types/node": "*", + "form-data": "^4.0.0" + } + }, + "node_modules/@types/supertest": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@types/supertest/-/supertest-7.2.0.tgz", + "integrity": "sha512-uh2Lv57xvggst6lCqNdFAmDSvoMG7M/HDtX4iUCquxQ5EGPtaPM5PL5Hmi7LCvOG8db7YaCPNJEeoI8s/WzIQw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/methods": "^1.1.4", + "@types/superagent": "^8.1.0" + } + }, "node_modules/@types/tmp": { "version": "0.2.6", "resolved": "https://registry.npmjs.org/@types/tmp/-/tmp-0.2.6.tgz", @@ -2238,6 +2301,13 @@ "dequal": "^2.0.3" } }, + "node_modules/asap": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/asap/-/asap-2.0.6.tgz", + "integrity": "sha512-BSHWgDSAiKs50o2Re8ppvp3seVHXSRM44cdSsT9FfNEUUZLOGWVCsiWaRPWM1Znn+mqZ1OfVZ3z3DWEzSp7hRA==", + "dev": true, + "license": "MIT" + }, "node_modules/asn1": { "version": "0.2.6", "resolved": "https://registry.npmjs.org/asn1/-/asn1-0.2.6.tgz", @@ -2961,6 +3031,16 @@ "node": ">=4.0.0" } }, + "node_modules/component-emitter": { + "version": "1.3.1", + "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.3.1.tgz", + "integrity": "sha512-T0+barUSQRTUQASh8bx02dl+DhF54GtIDY13Y3m9oWTklKbb3Wv974meRpeZ3lp1JpLVECWWNHC4vaG2XHXouQ==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/concat-map": { "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", @@ -3018,6 +3098,13 @@ "node": ">=6.6.0" } }, + "node_modules/cookiejar": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/cookiejar/-/cookiejar-2.1.4.tgz", + "integrity": "sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==", + "dev": true, + "license": "MIT" + }, "node_modules/core-util-is": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", @@ -3224,6 +3311,17 @@ "node": ">=8" } }, + "node_modules/dezalgo": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/dezalgo/-/dezalgo-1.0.4.tgz", + "integrity": "sha512-rXSP0bf+5n0Qonsb+SVVfNfIsimO4HEtmnIpPHY8Q1UCzKlQrDMfdobr8nJOOsRgWCyMRqeSBQzmWUMq7zvVig==", + "dev": true, + "license": "ISC", + "dependencies": { + "asap": "^2.0.0", + "wrappy": "1" + } + }, "node_modules/dom-accessibility-api": { "version": "0.5.16", "resolved": "https://registry.npmjs.org/dom-accessibility-api/-/dom-accessibility-api-0.5.16.tgz", @@ -3606,6 +3704,13 @@ "dev": true, "license": "MIT" }, + "node_modules/fast-safe-stringify": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", + "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==", + "dev": true, + "license": "MIT" + }, "node_modules/fb-watchman": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/fb-watchman/-/fb-watchman-2.0.2.tgz", @@ -3765,6 +3870,24 @@ "node": ">= 0.6" } }, + "node_modules/formidable": { + "version": "3.5.4", + "resolved": "https://registry.npmjs.org/formidable/-/formidable-3.5.4.tgz", + "integrity": "sha512-YikH+7CUTOtP44ZTnUhR7Ic2UASBPOqmaRkRKxRbywPTe5VxF7RRCck4af9wutiZ/QKM5nME9Bie2fFaPz5Gug==", + "dev": true, + "license": "MIT", + "dependencies": { + "@paralleldrive/cuid2": "^2.2.2", + "dezalgo": "^1.0.4", + "once": "^1.4.0" + }, + "engines": { + "node": ">=14.0.0" + }, + "funding": { + "url": "https://ko-fi.com/tunnckoCore/commissions" + } + }, "node_modules/forwarded": { "version": "0.2.0", "resolved": "https://registry.npmjs.org/forwarded/-/forwarded-0.2.0.tgz", @@ -5713,6 +5836,16 @@ "integrity": "sha512-abv/qOcuPfk3URPfDzmZU1LKmuw8kT+0nIHvKrKgFrwifol/doWcdA4ZqsWQ8ENrFKkd67Mfpo/LovbIUsbt3w==", "license": "MIT" }, + "node_modules/methods": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/methods/-/methods-1.1.2.tgz", + "integrity": "sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/micromatch": { "version": "4.0.8", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", @@ -5740,6 +5873,19 @@ "url": "https://github.com/sponsors/jonschlinkert" } }, + "node_modules/mime": { + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/mime/-/mime-2.6.0.tgz", + "integrity": "sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==", + "dev": true, + "license": "MIT", + "bin": { + "mime": "cli.js" + }, + "engines": { + "node": ">=4.0.0" + } + }, "node_modules/mime-db": { "version": "1.54.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.54.0.tgz", @@ -6974,6 +7120,42 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/superagent": { + "version": "10.3.0", + "resolved": "https://registry.npmjs.org/superagent/-/superagent-10.3.0.tgz", + "integrity": "sha512-B+4Ik7ROgVKrQsXTV0Jwp2u+PXYLSlqtDAhYnkkD+zn3yg8s/zjA2MeGayPoY/KICrbitwneDHrjSotxKL+0XQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "component-emitter": "^1.3.1", + "cookiejar": "^2.1.4", + "debug": "^4.3.7", + "fast-safe-stringify": "^2.1.1", + "form-data": "^4.0.5", + "formidable": "^3.5.4", + "methods": "^1.1.2", + "mime": "2.6.0", + "qs": "^6.14.1" + }, + "engines": { + "node": ">=14.18.0" + } + }, + "node_modules/supertest": { + "version": "7.2.2", + "resolved": "https://registry.npmjs.org/supertest/-/supertest-7.2.2.tgz", + "integrity": "sha512-oK8WG9diS3DlhdUkcFn4tkNIiIbBx9lI2ClF8K+b2/m8Eyv47LSawxUzZQSNKUrVb2KsqeTDCcjAAVPYaSLVTA==", + "dev": true, + "license": "MIT", + "dependencies": { + "cookie-signature": "^1.2.2", + "methods": "^1.1.2", + "superagent": "^10.3.0" + }, + "engines": { + "node": ">=14.18.0" + } + }, "node_modules/supports-color": { "version": "8.1.1", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz", diff --git a/server/package.json b/server/package.json index 4fd4b67..544c137 100644 --- a/server/package.json +++ b/server/package.json @@ -17,7 +17,7 @@ "scripts": { "start": "node --env-file .env src/server.ts", "dev": "nodemon -e .ts --env-file .env src/server.ts", - "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --setupFiles dotenv/config --coverage" + "test": "node --experimental-vm-modules ./node_modules/jest/bin/jest.js --setupFiles dotenv/config --coverage" }, "dependencies": { "@supabase/ssr": "^0.8.0", @@ -44,6 +44,8 @@ "@jest/globals": "^30.2.0", "@types/cors": "^2.8.19", "@types/jest": "^30.0.0", + "@types/supertest": "^7.2.0", + "supertest": "^7.2.2", "ts-jest": "^29.4.6" } } diff --git a/server/src/app.ts b/server/src/app.ts index 3c24375..6cb98a8 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -7,7 +7,7 @@ import cors from "cors"; const app = express() // middleware -const corsOrigin = process.env.NODE_ENV === 'production' ? process.env.CLIENT_ORIGIN! : "*"; +const corsOrigin = process.env.CLIENT_ORIGIN || "http://localhost:5173"; app.use(cors({ origin: corsOrigin, credentials: true, @@ -42,11 +42,14 @@ app.post("/posts", async (req: Request, res: Response) => { // Build new post and insert into Supabase const newPost = { userid: userData.uid, - songtitle: songTitle, - artistname: artistName ?? "", - latitude, - longitude, - comment: comment ?? "", + time: new Date().toISOString(), + songid: null, + location: `(${latitude},${longitude})`, + comment: { + songTitle, + artistName: artistName ?? "", + text: comment ?? "", + } }; // Insert into Supabase @@ -106,16 +109,14 @@ app.get("/feed", async (req: Request, res: Response) => { row.userid1 === userID ? row.userid2 : row.userid1 ); - // Return empty feed if no friends - if (friendsIDs.length === 0) { - return res.status(200).json([]); - } + // include your own posts too + const feedUserIDs = [...new Set([userID, ...friendsIDs])]; - // Fetch posts where userid is in friendIDs by newest first + // Fetch posts where userid is in feedUserIDs by newest first const { data: posts, error: postsErr } = await supabase .from("posts") - .select("postid, userid, songtitle, artistname, latitude, longitude, comment") - .in("userid", friendsIDs) + .select("postid, userid, time, location, comment") + .in("userid", feedUserIDs) .order("time", { ascending: false }) .limit(20); diff --git a/server/test/posts.test.ts b/server/test/posts.test.ts new file mode 100644 index 0000000..18ff805 --- /dev/null +++ b/server/test/posts.test.ts @@ -0,0 +1,103 @@ +import { jest } from "@jest/globals"; +import request from "supertest"; + +// ESM-safe mocking of the database module +await jest.unstable_mockModule("../src/database.ts", () => { + return { + createClient: jest.fn(() => { + return { + from: jest.fn(() => ({ + insert: jest.fn(() => ({ + select: jest.fn(() => ({ + single: jest.fn(async () => ({ + data: { + postid: 123, + userid: "fake-user-id", + location: "(44.565,-123.276)", + comment: { + songTitle: "Test Song", + artistName: "Test Artist", + text: "hello", + }, + time: new Date().toISOString(), + }, + error: null, + })), + })), + })), + })), + }; + }), + + // Default mock authentication success + authenticate: jest.fn(async () => ({ + uid: "fake-user-id", + email: "test@test.com", + email_verified: true, + })), + }; +}); + +const { default: app } = await import("../src/app.ts"); +const database = await import("../src/database.ts"); + +describe("POST /posts", () => { + test("201 creates a post when payload is valid", async () => { + const res = await request(app) + .post("/posts") + .send({ + songTitle: "Test Song", + artistName: "Test Artist", + latitude: 44.565, + longitude: -123.276, + comment: "hello", + }); + + expect(res.status).toBe(201); + expect(res.body).toHaveProperty("postid"); + }); + + test("400 when songTitle is missing", async () => { + const res = await request(app) + .post("/posts") + .send({ + artistName: "Test Artist", + latitude: 44.565, + longitude: -123.276, + comment: "hello", + }); + + expect(res.status).toBe(400); + expect(res.text).toMatch(/songTitle is required/i); + }); + + test("400 when latitude/longitude are not numbers", async () => { + const res = await request(app) + .post("/posts") + .send({ + songTitle: "Test Song", + latitude: "nope", + longitude: -123.276, + comment: "hello", + }); + + expect(res.status).toBe(400); + expect(res.body).toHaveProperty("error"); + }); + + test("401 when user is not authenticated", async () => { + (database.authenticate as jest.Mock).mockResolvedValueOnce(null); + + const res = await request(app) + .post("/posts") + .send({ + songTitle: "Test Song", + artistName: "Test Artist", + latitude: 44.565, + longitude: -123.276, + comment: "hello", + }); + + expect(res.status).toBe(401); + }); +}); \ No newline at end of file