104 lines
2.2 KiB
TypeScript
104 lines
2.2 KiB
TypeScript
import { db, resetDbInstance } from "./database"
|
|
import migrateToLatest, { resetAll } from "./migrate"
|
|
import {
|
|
createSubscription,
|
|
getSubscription,
|
|
Subscription,
|
|
updateSubscription,
|
|
} from "./subscription"
|
|
|
|
const pushSubscription1 = {
|
|
endpoint: "https://updates.push.services.mozilla.com/wpush/v2/aaaaaaa",
|
|
expirationTime: null,
|
|
keys: {
|
|
auth: "adfsadfasdf",
|
|
p256dh: "aaaaaaaaaaaa",
|
|
},
|
|
}
|
|
|
|
const pushSubscription2 = {
|
|
endpoint: "https://updates.push.services.mozilla.com/wpush/v2/bbbbbbbb",
|
|
expirationTime: null,
|
|
keys: {
|
|
auth: "whoahauth",
|
|
p256dh: "bbbbbbbb",
|
|
},
|
|
}
|
|
|
|
jest.mock("./settings", () => ({
|
|
port: 5434,
|
|
user: "postgres",
|
|
password: process.env.POSTGRES_PASSWORD,
|
|
database: "test",
|
|
host: "localhost",
|
|
}))
|
|
|
|
xdescribe("subscriptions", () => {
|
|
beforeAll(() => {
|
|
resetDbInstance()
|
|
})
|
|
|
|
beforeEach(async () => {
|
|
await migrateToLatest()
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await resetAll()
|
|
})
|
|
|
|
afterAll(async () => {
|
|
await db.destroy()
|
|
})
|
|
|
|
test("createSubscription", async () => {
|
|
const { id } = await createSubscription(pushSubscription1)
|
|
|
|
const subscription = (await db
|
|
.selectFrom("subscription")
|
|
.selectAll()
|
|
.where("id", "=", id)
|
|
.executeTakeFirst()) as Subscription
|
|
|
|
expect(subscription).toEqual({
|
|
id,
|
|
subscription: pushSubscription1,
|
|
})
|
|
})
|
|
|
|
test("updateSubscription", async () => {
|
|
const { id } = (await db
|
|
.insertInto("subscription")
|
|
.values({ subscription: JSON.stringify(pushSubscription1), id: 50 })
|
|
.returning("id")
|
|
.executeTakeFirst())!
|
|
|
|
await updateSubscription(pushSubscription2, id)
|
|
|
|
const updated = (await db
|
|
.selectFrom("subscription")
|
|
.selectAll()
|
|
.where("id", "=", id)
|
|
.executeTakeFirst()) as Subscription
|
|
|
|
expect(updated).toEqual({
|
|
id,
|
|
subscription: pushSubscription2,
|
|
})
|
|
})
|
|
|
|
test("getSubscription", async () => {
|
|
const { id } = (await db
|
|
.insertInto("subscription")
|
|
.values({ subscription: JSON.stringify(pushSubscription1), id: 5000 })
|
|
.returning("id")
|
|
.executeTakeFirst())!
|
|
|
|
const got = await getSubscription(id)
|
|
|
|
expect(got).toEqual({
|
|
subscription: pushSubscription1,
|
|
id: 5000,
|
|
})
|
|
})
|
|
})
|