Skip to content

feat: add safeBrowsingExpiry column to urls table #2402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions migrations/20250804092620-add-urls-safebrowsing-expiry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
/**
* Add altering commands here.
*
* Example:
* await queryInterface.createTable('users', { id: Sequelize.INTEGER });.
*/
await queryInterface.addColumn('urls', 'safeBrowsingExpiry', {
type: Sequelize.DATE,
allowNull: true,
defaultValue: null,
})
},

async down(queryInterface) {
/**
* Add reverting commands here.
*
* Example:
* await queryInterface.dropTable('users');.
*/
await queryInterface.removeColumn('urls', 'safeBrowsingExpiry')
},
}
1 change: 1 addition & 0 deletions src/server/mappers/UrlMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export class UrlMapper implements Mapper<StorableUrl, UrlType> {
contactEmail: urlType.contactEmail,
source: urlType.source,
tagStrings: urlType.tagStrings,
safeBrowsingExpiry: urlType.safeBrowsingExpiry,
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/server/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { UrlClicks } from './statistics/clicks'
import { syncFunctions } from './functions'
import { Tag } from './tag'
import { Job, JobItem } from './job'
import { DEV_ENV } from '../config'

// One user can create many urls but each url can only be mapped to one user.
User.hasMany(Url, { as: 'Urls', foreignKey: { allowNull: false } })
Expand Down Expand Up @@ -45,6 +46,8 @@ UrlClicks.belongsTo(Url, { foreignKey: 'shortUrl' })
* Initialise the database table.
*/
export default async () => {
await sequelize.sync()
if (DEV_ENV) {
await sequelize.sync()
}
await syncFunctions()
}
5 changes: 5 additions & 0 deletions src/server/models/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface UrlBaseType extends IdType {
readonly description: string
readonly source: StorableUrlSource
readonly tagStrings: string
readonly safeBrowsingExpiry: string | null
}

export interface UrlType extends IdType, UrlBaseType, Sequelize.Model {
Expand Down Expand Up @@ -229,6 +230,10 @@ export const Url = <UrlTypeStatic>sequelize.define(
allowNull: false,
defaultValue: '',
},
safeBrowsingExpiry: {
type: Sequelize.DATE,
allowNull: true,
},
},
{
hooks: {
Expand Down
1 change: 1 addition & 0 deletions src/server/repositories/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type StorableUrl = Pick<
| 'contactEmail'
| 'source'
| 'tagStrings'
| 'safeBrowsingExpiry'
> &
Pick<UrlClicksType, 'clicks'> & { tags?: string[] }

Expand Down
6 changes: 6 additions & 0 deletions test/integration/api/user/Urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ describe('Url integration tests', () => {
tagStrings: '',
createdAt: expect.stringMatching(DATETIME_REGEX),
updatedAt: expect.stringMatching(DATETIME_REGEX),
safeBrowsingExpiry: null,
})
})

Expand All @@ -108,6 +109,7 @@ describe('Url integration tests', () => {
tagStrings: '',
createdAt: expect.stringMatching(DATETIME_REGEX),
updatedAt: expect.stringMatching(DATETIME_REGEX),
safeBrowsingExpiry: null,
})
})

Expand Down Expand Up @@ -151,6 +153,7 @@ describe('Url integration tests', () => {
tagStrings: '',
createdAt: expect.stringMatching(DATETIME_REGEX),
updatedAt: expect.stringMatching(DATETIME_REGEX),
safeBrowsingExpiry: null,
})

// Should be able to get updated link URL
Expand All @@ -172,6 +175,7 @@ describe('Url integration tests', () => {
tagStrings: '',
createdAt: expect.stringMatching(DATETIME_REGEX),
updatedAt: expect.stringMatching(DATETIME_REGEX),
safeBrowsingExpiry: null,
},
],
count: 1,
Expand Down Expand Up @@ -203,6 +207,7 @@ describe('Url integration tests', () => {
tagStrings: '',
createdAt: expect.stringMatching(DATETIME_REGEX),
updatedAt: expect.stringMatching(DATETIME_REGEX),
safeBrowsingExpiry: null,
})

// Should be able to get updated file URL
Expand All @@ -224,6 +229,7 @@ describe('Url integration tests', () => {
tagStrings: '',
createdAt: expect.stringMatching(DATETIME_REGEX),
updatedAt: expect.stringMatching(DATETIME_REGEX),
safeBrowsingExpiry: null,
},
],
count: 1,
Expand Down
1 change: 1 addition & 0 deletions test/server/mappers/UrlV1Mapper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('url v1 mapper', () => {
tagStrings: '1;abc;foo-bar',
contactEmail: '[email protected]',
description: 'this-is-a-description',
safeBrowsingExpiry: null,
}
const urlV1DTO = urlV1Mapper.persistenceToDto(storableUrl)
expect(urlV1DTO).toEqual({
Expand Down
1 change: 1 addition & 0 deletions test/server/mocks/repositories/UrlRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export class UrlRepositoryMock implements UrlRepositoryInterface {
clicks: 0,
source: StorableUrlSource.Console,
tagStrings: '',
safeBrowsingExpiry: null,
},
],
count: 0,
Expand Down
10 changes: 6 additions & 4 deletions test/server/repositories/UrlRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { DirectoryQueryConditions } from '../../../src/server/modules/directory'
import TagRepositoryMock from '../mocks/repositories/TagRepository'
import { TAG_SEPARATOR } from '../../../src/shared/constants'
import { StorableUrl } from '../../../src/server/repositories/types'

jest.mock('../../../src/server/models/url', () => ({
Url: urlModelMock,
Expand Down Expand Up @@ -75,17 +76,18 @@ describe('UrlRepository', () => {
const baseUrlClicks = {
clicks: 2,
}
const baseTemplate = {
const baseTemplate: Omit<StorableUrl, 'tagStrings' | 'clicks'> = {
shortUrl: baseShortUrl,
longUrl: baseLongUrl,
state: 'ACTIVE',
state: StorableUrlState.Active,
isFile: false,
createdAt: new Date(),
updatedAt: new Date(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
description: 'An agency of the Singapore Government',
contactEmail: '[email protected]',
source: StorableUrlSource.Console,
tags: [],
safeBrowsingExpiry: null,
}
const baseUrl = {
...baseTemplate,
Expand Down
16 changes: 11 additions & 5 deletions test/server/repositories/UserRepository.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { UserRepository } from '../../../src/server/repositories/UserRepository'
import { UrlMapper } from '../../../src/server/mappers/UrlMapper'
import { UserMapper } from '../../../src/server/mappers/UserMapper'
import { NotFoundError } from '../../../src/server/util/error'
import { StorableUrl } from '../../../src/server/repositories/types'
import {
StorableUrlSource,
StorableUrlState,
} from '../../../src/server/repositories/enums'

jest.mock('../../../src/server/models/user', () => ({
User: userModelMock,
Expand All @@ -17,16 +22,17 @@ const userRepo = new UserRepository(
new UrlMapper(),
)

const baseUrlTemplate = {
const baseUrlTemplate: Omit<StorableUrl, 'tagStrings' | 'clicks'> = {
shortUrl: 'short-link',
longUrl: 'https://www.agency.gov.sg',
state: 'ACTIVE',
state: StorableUrlState.Active,
isFile: false,
createdAt: Date.now(),
updatedAt: Date.now(),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
description: 'An agency of the Singapore Government',
contactEmail: '[email protected]',
source: 'CONSOLE',
source: StorableUrlSource.Console,
safeBrowsingExpiry: null,
}

const urlClicks = {
Expand Down
Loading