Skip to content

Commit

Permalink
add verification data fetcher test
Browse files Browse the repository at this point in the history
  • Loading branch information
hfxbse committed Mar 25, 2024
1 parent 49aa7fb commit d5e6904
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import sealBox from "tweetnacl-sealedbox-js";
const crypto = globalThis.crypto
const encoder = new TextEncoder()

async function fetchVerification(): Promise<{ csrf: string, keyVersion: number, keyId: number, publicKey: string }> {
export async function fetchVerification(): Promise<{ csrf: string, keyVersion: number, keyId: number, publicKey: string }> {
const response = await fetch("https://www.instagram.com/api/v1/web/data/shared_data/", {
headers: {
"Sec-Fetch-Site": "same-origin"
Expand Down
45 changes: 42 additions & 3 deletions test/instagram.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, expect, test} from "@jest/globals";
import {encryptPassword} from "../src/instagram";
import {beforeEach, describe, expect, test, jest} from "@jest/globals";
import {encryptPassword, fetchVerification} from "../src/instagram";

interface InstagramEncryptionKey {
public: string,
Expand Down Expand Up @@ -96,7 +96,6 @@ describe("Password encryption", () => {
})
})


test("Consecutive runs generate different encrypted passwords", async () => {
const password = "12345678"
const time = new Date()
Expand All @@ -116,3 +115,43 @@ describe("Password encryption", () => {
expect(first).not.toStrictEqual(second)
})
})

const sharedData = {
encryption: {
key_id: "87",
public_key: "8dd9aad29d9a614c338cff479f850d3ec57c525c33b3f702ab65e9e057fc087e",
version: "9"
},
config: {
csrf_token: "KdiF63JpmmBdeXp2Bs2LT7t8vlwWXXXX",
}
}

describe("Verification data", () => {
beforeEach(() => {
jest.spyOn(global, "fetch").mockImplementation(() => Promise.resolve({
ok: true,
json: () => Promise.resolve(sharedData)
} as Response))
})

test("Fetches CSRF token", async () => {
const {csrf} = await fetchVerification()
expect(csrf).toStrictEqual(sharedData.config.csrf_token)
})

test("Fetches public key", async () => {
const {publicKey} = await fetchVerification()
expect(publicKey).toStrictEqual(sharedData.encryption.public_key)
})

test("Fetches key id", async () => {
const {keyId} = await fetchVerification()
expect(keyId).toStrictEqual(parseInt(sharedData.encryption.key_id, 10))
})

test("Fetches key version", async () => {
const {keyVersion} = await fetchVerification()
expect(keyVersion).toStrictEqual(parseInt(sharedData.encryption.version, 10))
})
})

0 comments on commit d5e6904

Please sign in to comment.