Skip to content

Commit

Permalink
Merge branch 'notifications/tests' into orion/merge-master
Browse files Browse the repository at this point in the history
  • Loading branch information
Ignazio Bovo committed Nov 1, 2023
2 parents b400052 + c5fb900 commit 0edcf2b
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/mail-scheduler/tests/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export async function populateDbWithSeedData() {
bannedFromChannels: [],
totalChannelsCreated: 0,
handle: `handle-${i}`,
handleRaw: '0x' + Buffer.from(`handle-${i}`).toString('hex'),
controllerAccount: `j4${i}7rVcUCxi2crhhjRq46fNDRbVHTjJrz6bKxZwehEMQxZeSf`,
channels: [],
})
Expand Down
148 changes: 117 additions & 31 deletions src/tests/integration/notifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
NftFeaturedOnMarketPlace,
NftOwnerChannel,
Notification,
NotificationEmailDelivery,
OwnedNft,
Video,
VideoLiked,
Expand All @@ -34,6 +35,12 @@ import { Store } from '@subsquid/typeorm-store'
import { processMemberRemarkedEvent } from '../../mappings/membership'
import Long from 'long'
import { backwardCompatibleMetaID } from '../../mappings/utils'
import { config as dontenvConfig } from 'dotenv'
import path from 'path'

dontenvConfig({
path: path.resolve(__dirname, './.env'),
})

const metadataToBytes = <T>(metaClass: AnyMetadataClass<T>, obj: T): Bytes => {
return createType('Bytes', '0x' + Buffer.from(metaClass.encode(obj).finish()).toString('hex'))
Expand All @@ -53,25 +60,24 @@ const createOverlay = async () => {
}

describe('notifications tests', () => {
let notification: Notification | null
let overlay: EntityManagerOverlay
let em: EntityManager
before(async () => {
em = await globalEm
await populateDbWithSeedData()
})
after(async () => {
await clearDb()
})
describe('exclude channel', () => {
let notificationId: string
it('exclude channel should deposit notification', async () => {
const channelId = '1'
const rationale = 'test-rationale'
const nextNotificationIdPre = await getNextNotificationId(em, false)

notificationId = OFFCHAIN_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre
await excludeChannelInner(em, channelId, rationale)

const notification = await em.getRepository(Notification).findOneBy({
id: OFFCHAIN_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre,
notification = await em.getRepository(Notification).findOneBy({
id: notificationId,
})
const channel = await em.getRepository(Channel).findOneBy({ id: channelId })
const nextNotificationIdPost = await getNextNotificationId(em, false)
Expand All @@ -90,6 +96,14 @@ describe('notifications tests', () => {
expect(nextNotificationIdPost.toString()).to.equal((nextNotificationIdPre + 1).toString())
expect(notification?.accountId).to.equal(account?.id)
})
it('notification email entity should be correctly deposited', async () => {
const notificationEmailDelivery = await em
.getRepository(NotificationEmailDelivery)
.findOneBy({ notificationId })
expect(notificationEmailDelivery).not.to.be.null
expect(notificationEmailDelivery!.discard).to.be.false
expect(notificationEmailDelivery!.attempts).to.be.undefined
})
it('exclude channel should mark channel as excluded with entity inserted', async () => {
const channelId = '2'
const rationale = 'test-rationale'
Expand All @@ -105,15 +119,17 @@ describe('notifications tests', () => {
})
})
describe('exclude video', () => {
let notificationId: string
it('exclude video should deposit notification', async () => {
const videoId = '1'
const rationale = 'test-rationale'
const nextNotificationIdPre = await getNextNotificationId(em, false)
const notificationId = OFFCHAIN_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre

await excludeVideoInner(em, videoId, rationale)

const notification = await em.getRepository(Notification).findOneBy({
id: OFFCHAIN_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre,
notification = await em.getRepository(Notification).findOneBy({
id: notificationId,
})
const video = await em
.getRepository(Video)
Expand All @@ -133,6 +149,14 @@ describe('notifications tests', () => {
expect(nextNotificationIdPost.toString()).to.equal((nextNotificationIdPre + 1).toString())
expect(notification?.accountId).to.equal(account?.id)
})
it('notification email entity should be correctly deposited', async () => {
const notificationEmailDelivery = await em
.getRepository(NotificationEmailDelivery)
.findOneBy({ notificationId })
expect(notificationEmailDelivery).not.to.be.null
expect(notificationEmailDelivery!.discard).to.be.false
expect(notificationEmailDelivery!.attempts).to.be.undefined
})
it('exclude video should work with exclusion entity added', async () => {
const videoId = '2'
const rationale = 'test-rationale'
Expand All @@ -148,14 +172,16 @@ describe('notifications tests', () => {
})
})
describe('set nft as featured', () => {
let notificationId: string
it('feature nfts should deposit notification and set nft as featured', async () => {
const nftId = '1'
const nextNotificationIdPre = await getNextNotificationId(em, false)
notificationId = OFFCHAIN_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre

await setFeaturedNftsInner(em, [nftId])

const notification = await em.getRepository(Notification).findOneBy({
id: OFFCHAIN_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre,
notification = await em.getRepository(Notification).findOneBy({
id: notificationId,
})
const nft = await em
.getRepository(OwnedNft)
Expand All @@ -182,17 +208,27 @@ describe('notifications tests', () => {
expect(notification?.accountId).to.equal(account?.id)
expect(nft.isFeatured).to.be.true
})
it('notification email entity should be correctly deposited', async () => {
const notificationEmailDelivery = await em
.getRepository(NotificationEmailDelivery)
.findOneBy({ notificationId })
expect(notificationEmailDelivery).not.to.be.null
expect(notificationEmailDelivery!.discard).to.be.false
expect(notificationEmailDelivery!.attempts).to.be.undefined
})
})
describe('New bid made', () => {
let nft: OwnedNft
const memberId = '5'
const outbiddedMember = '4'
const videoId = '5'
let notificationId: string
let nextNotificationIdPre: number

before(async () => {
const bidAmount = BigInt(100000)
nextNotificationIdPre = await getNextNotificationId(em, true)
notificationId = RUNTIME_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre
nft = await em.getRepository(OwnedNft).findOneByOrFail({ videoId })
overlay = await createOverlay()

Expand All @@ -207,28 +243,40 @@ describe('notifications tests', () => {
)
})
it('should deposit notification for member outbidded', async () => {
const notification = await overlay
.getRepository(Notification)
.getByIdOrFail(RUNTIME_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre)
const account = await overlay
.getRepository(Account)
.getOneByRelationOrFail('membershipId', outbiddedMember)
let account: Account
before(async () => {
notification = (await overlay
.getRepository(Notification)
.getById(notificationId)) as Notification | null
account = (await overlay
.getRepository(Account)
.getOneByRelationOrFail('membershipId', outbiddedMember)) as Account
})

expect(notification).not.to.be.null
expect(notification!.notificationType.isTypeOf).to.equal('HigherBidPlaced')
expect(notification!.status.isTypeOf).to.equal('Unread')
expect(notification!.inApp).to.be.true
expect(notification!.recipient.isTypeOf).to.equal('MemberRecipient')
expect((notification!.recipient as MemberRecipient).membership).to.equal(outbiddedMember)
expect(notification?.accountId).to.equal(account?.id)
expect(notification?.accountId).to.equal(account!.id)
})
it('notification email entity should be correctly deposited', async () => {
const notificationEmailDelivery = (await overlay
.getRepository(NotificationEmailDelivery)
.getOneByRelation('notificationId', notificationId)) as NotificationEmailDelivery | null
expect(notificationEmailDelivery).not.to.be.null
expect(notificationEmailDelivery!.discard).to.be.false
expect(notificationEmailDelivery!.attempts).to.be.empty
})
it('should deposit notification for creator receiving a new auction bid', async () => {
notificationId = RUNTIME_NOTIFICATION_ID_TAG + '-' + (nextNotificationIdPre + 1)
const channel = await em
.getRepository(Channel)
.findOneBy({ id: (nft.owner as NftOwnerChannel).channel })
const notification = await overlay
notification = (await overlay
.getRepository(Notification)
.getByIdOrFail(RUNTIME_NOTIFICATION_ID_TAG + '-' + (nextNotificationIdPre + 1))
.getByIdOrFail(notificationId)) as Notification
const account = await overlay
.getRepository(Account)
.getOneByRelationOrFail('membershipId', channel!.ownerMemberId!)
Expand All @@ -243,9 +291,21 @@ describe('notifications tests', () => {
expect((notification!.recipient as ChannelRecipient).channel).to.equal(channel!.id)
expect(notification?.accountId).to.equal(account?.id)
})
describe('notification email entity should be correctly to db', () => {
let notificationEmailDelivery: NotificationEmailDelivery | null
it('notification email entity should be correctly deposited on overlay', async () => {
notificationEmailDelivery = (await overlay
.getRepository(NotificationEmailDelivery)
.getOneByRelation('notificationId', notificationId)) as NotificationEmailDelivery | null
expect(notificationEmailDelivery).not.to.be.null
expect(notificationEmailDelivery!.discard).to.be.false
expect(notificationEmailDelivery!.attempts).to.be.empty
})
})
})
describe('Video Liked', () => {
let notificationId: number
let notificationId: string
let nextNotificationIdPre: number
const block = { timestamp: 123456 } as any
const indexInBlock = 1
const extrinsicHash = '0x1234567890abcdef'
Expand All @@ -261,7 +321,8 @@ describe('notifications tests', () => {
} as any
before(async () => {
overlay = await createOverlay()
notificationId = await getNextNotificationId(em, true)
nextNotificationIdPre = await getNextNotificationId(em, true)
notificationId = RUNTIME_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre.toString()
})
it('should process video liked and deposit notification', async () => {
await processMemberRemarkedEvent({
Expand All @@ -273,21 +334,33 @@ describe('notifications tests', () => {
})

const nextNotificationId = await getNextNotificationId(em, true)
const notification = await overlay
notification = (await overlay
.getRepository(Notification)
.getByIdOrFail(RUNTIME_NOTIFICATION_ID_TAG + '-' + notificationId.toString())
.getByIdOrFail(notificationId)) as Notification

expect(notification.notificationType.isTypeOf).to.equal('VideoLiked')
const notificationData = notification.notificationType as VideoLiked
expect(notificationData.videoId).to.equal('1')
expect(notification!.status.isTypeOf).to.equal('Unread')
expect(notification!.inApp).to.be.true
expect(nextNotificationId.toString()).to.equal((notificationId + 1).toString())
expect(nextNotificationId.toString()).to.equal((nextNotificationIdPre + 1).toString())
expect(notification!.recipient.isTypeOf).to.equal('ChannelRecipient')
})
describe('notification email entity should be correctly to db', () => {
let notificationEmailDelivery: NotificationEmailDelivery | null
it('notification email entity should be correctly deposited on overlay', async () => {
notificationEmailDelivery = (await overlay
.getRepository(NotificationEmailDelivery)
.getOneByRelation('notificationId', notificationId)) as NotificationEmailDelivery | null
expect(notificationEmailDelivery).not.to.be.null
expect(notificationEmailDelivery!.discard).to.be.false
expect(notificationEmailDelivery!.attempts).to.be.empty
})
})
})
describe('Comment Posted To Video', () => {
let notificationId: number
let nextNotificationIdPre: number
let notificationId: string
const block = { timestamp: 123456 } as any
const indexInBlock = 1
const extrinsicHash = '0x1234567890abcdef'
Expand All @@ -304,7 +377,8 @@ describe('notifications tests', () => {
} as any
before(async () => {
overlay = await createOverlay()
notificationId = await getNextNotificationId(em, true)
nextNotificationIdPre = await getNextNotificationId(em, true)
notificationId = RUNTIME_NOTIFICATION_ID_TAG + '-' + nextNotificationIdPre.toString()
})
it('should process comment to video and deposit notification', async () => {
await processMemberRemarkedEvent({
Expand All @@ -316,15 +390,16 @@ describe('notifications tests', () => {
})

const nextNotificationId = await getNextNotificationId(em, true)
const notification = await overlay
notification = (await overlay
.getRepository(Notification)
.getByIdOrFail(RUNTIME_NOTIFICATION_ID_TAG + '-' + notificationId.toString())
.getByIdOrFail(notificationId)) as Notification | null

it('notification type is comment posted to video', () => {
expect(notification.notificationType.isTypeOf).to.equal('CommentPostedToVideo')
expect(notification).not.to.be.null
expect(notification!.notificationType.isTypeOf).to.equal('CommentPostedToVideo')
})
it('notification data for comment posted to video should be ok', () => {
const notificationData = notification.notificationType as CommentPostedToVideo
const notificationData = notification!.notificationType as CommentPostedToVideo
expect(notificationData.videoId).to.equal('1')
expect(notificationData.comentId).to.equal(backwardCompatibleMetaID(block, indexInBlock))
expect(notificationData.memberHandle).to.equal('handle-2')
Expand All @@ -334,9 +409,20 @@ describe('notifications tests', () => {
it('general notification creation setting should be as default', () => {
expect(notification!.status.isTypeOf).to.equal('Unread')
expect(notification!.inApp).to.be.true
expect(nextNotificationId.toString()).to.equal((notificationId + 1).toString())
expect(nextNotificationId.toString()).to.equal((nextNotificationIdPre + 1).toString())
expect(notification!.recipient.isTypeOf).to.equal('ChannelRecipient')
})
describe('notification email entity should be correctly to db', () => {
let notificationEmailDelivery: NotificationEmailDelivery | null
it('notification email entity should be correctly deposited on overlay', async () => {
notificationEmailDelivery = (await overlay
.getRepository(NotificationEmailDelivery)
.getOneByRelation('notificationId', notificationId)) as NotificationEmailDelivery | null
expect(notificationEmailDelivery).not.to.be.null
expect(notificationEmailDelivery!.discard).to.be.false
expect(notificationEmailDelivery!.attempts).to.be.empty
})
})
})
})
})
2 changes: 1 addition & 1 deletion src/tests/integration/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ cleanup() {
}

# Run the tests
npx ts-mocha "./src/tests/integration/*.ts" --timeout 60000 --exit
npx ts-mocha "./src/tests/integration/notifications.test.ts" --timeout 60000 --exit

trap cleanup EXIT
1 change: 1 addition & 0 deletions src/tests/integration/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function populateDbWithSeedData() {
id: i.toString(),
controllerAccount: `controller-account-${i}`,
handle: `handle-${i}`,
handleRaw: '0x' + Buffer.from(`handle-${i}`).toString('hex'),
totalChannelsCreated: 0,
})
const user = new User({
Expand Down
3 changes: 1 addition & 2 deletions src/utils/globalEm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ const source = new DataSource(config)

async function initGlobalEm() {
try {
console.log(process.env.DB_PORT)
console.log(config)
globalEmLogger.info(`Initializing database connection with config...`)
await source.initialize()
} catch (e) {
globalEmLogger.error(`Error during database connection initialization: ${String(e)}`)
Expand Down

0 comments on commit 0edcf2b

Please sign in to comment.