Skip to content

Commit

Permalink
Move backend types into separate file and introduce usage
Browse files Browse the repository at this point in the history
  • Loading branch information
bkimminich committed Jan 12, 2022
1 parent d6d8531 commit 56e1830
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 114 deletions.
107 changes: 3 additions & 104 deletions data/datacreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

/* jslint node: true */
import models = require('../models/index')
import * as models from '../models/index'
import { Address, Card, Challenge, Delivery, Memory, Product, Recycle, SecurityQuestion, User } from './types'
const datacache = require('./datacache')
const config = require('config')
const utils = require('../lib/utils')
Expand Down Expand Up @@ -555,7 +556,7 @@ async function createSecurityQuestions () {
const questions = await loadStaticData('securityQuestions')

await Promise.all(
questions.map(async ({ question }: { question: string }) => {
questions.map(async ({ question }: SecurityQuestion) => {
try {
await models.SecurityQuestion.create({ question })
} catch (err) {
Expand Down Expand Up @@ -669,105 +670,3 @@ async function createOrders () {
)
)
}

interface Challenge {
name: string
category: string
description: string
difficulty: number
hint: string
hintUrl: string
mitigationUrl?: string
key: string
disabledEnv?: string | string[]
tutorial?: { order: number }
tags?: string[]
}

interface User {
username?: string
email: string
password: string
customDomain?: string
key: string
role: string
deletedFlag?: boolean
profileImage?: string
securityQuestion?: {
id: number
answer: string
}
feedback?: {
comment: string
rating: number
}
address?: Address[]
card?: Card[]
totpSecret?: string
walletBalance?: number
}

interface Delivery {
name: string
price: number
deluxePrice: number
eta: number
icon: string
}

interface Address {
fullName: string
mobileNum: number
zipCode: string
streetAddress: string
city: string
state: string
country: string
}

interface Card {
fullName: string
cardNum: number
expMonth: number
expYear: number
}

interface Product {
name: string
description: string
price?: number
deluxePrice?: number
quantity?: number
limitPerUser?: number
image?: string
reviews?: Review[]
deletedDate?: string
deletedAt?: Date | string
useForChristmasSpecialChallenge?: boolean
keywordsForPastebinDataLeakChallenge?: string[]
urlForProductTamperingChallenge?: string
fileForRetrieveBlueprintChallenge?: string
}

interface Review {
text: string
author: string
}

interface Memory {
image: string
caption: string
user: string
geoStalkingMetaSecurityQuestion?: number
geoStalkingMetaSecurityAnswer?: string
geoStalkingVisualSecurityQuestion?: number
geoStalkingVisualSecurityAnswer?: string
}

interface Recycle {
UserId: number
quantity: number
AddressId: number
date: string
isPickup: boolean
}
105 changes: 105 additions & 0 deletions data/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
export interface Challenge {
name: string
category: string
description: string
difficulty: number
hint: string
hintUrl: string
mitigationUrl?: string
key: string
disabledEnv?: string | string[]
tutorial?: { order: number }
tags?: string[]
}

export interface User {
username?: string
email: string
password: string
customDomain?: string
key: string
role: string
deletedFlag?: boolean
profileImage?: string
securityQuestion?: {
id: number
answer: string
}
feedback?: {
comment: string
rating: number
}
address?: Address[]
card?: Card[]
totpSecret?: string
walletBalance?: number
}

export interface Delivery {
name: string
price: number
deluxePrice: number
eta: number
icon: string
}

export interface Address {
fullName: string
mobileNum: number
zipCode: string
streetAddress: string
city: string
state: string
country: string
}

export interface Card {
fullName: string
cardNum: number
expMonth: number
expYear: number
}

export interface Product {
name: string
description: string
price?: number
deluxePrice?: number
quantity?: number
limitPerUser?: number
image?: string
reviews?: Review[]
deletedDate?: string
deletedAt?: Date | string
useForChristmasSpecialChallenge?: boolean
keywordsForPastebinDataLeakChallenge?: string[]
urlForProductTamperingChallenge?: string
fileForRetrieveBlueprintChallenge?: string
}

export interface Review {
text: string
author: string
}

export interface Memory {
image: string
caption: string
user: string
geoStalkingMetaSecurityQuestion?: number
geoStalkingMetaSecurityAnswer?: string
geoStalkingVisualSecurityQuestion?: number
geoStalkingVisualSecurityAnswer?: string
}

export interface Recycle {
UserId: number
quantity: number
AddressId: number
date: string
isPickup: boolean
}

export interface SecurityQuestion {
question: string
}
12 changes: 8 additions & 4 deletions models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

/* jslint node: true */
import fs = require('fs')
import { Model } from 'sequelize'
const path = require('path')
const sequelizeNoUpdateAttributes = require('sequelize-notupdate-attributes')
const Sequelize = require('sequelize')
Expand All @@ -22,7 +23,7 @@ const sequelize = new Sequelize('database', 'username', 'password', {
logging: false
})
sequelizeNoUpdateAttributes(sequelize)
const db = {}
const db: Database = { sequelize, Sequelize }

fs.readdirSync(__dirname)
.filter(file => (file.match(/\.[jt]s$/) != null) && !file.includes('index.'))
Expand All @@ -37,7 +38,10 @@ Object.keys(db).forEach(modelName => {
}
})

db.sequelize = sequelize
db.Sequelize = Sequelize

module.exports = db

interface Database {
sequelize: any
Sequelize: any
[key: string]: Model
}
5 changes: 3 additions & 2 deletions test/api/searchApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
*/

import frisby = require('frisby')
import { Product } from '../../data/types'
const security = require('../../lib/insecurity')
const config = require('config')

const christmasProduct = config.get('products').filter(({ useForChristmasSpecialChallenge }) => useForChristmasSpecialChallenge)[0]
const pastebinLeakProduct = config.get('products').filter(({ keywordsForPastebinDataLeakChallenge }) => keywordsForPastebinDataLeakChallenge)[0]
const christmasProduct = config.get('products').filter(({ useForChristmasSpecialChallenge }: Product) => useForChristmasSpecialChallenge)[0]
const pastebinLeakProduct = config.get('products').filter(({ keywordsForPastebinDataLeakChallenge }: Product) => keywordsForPastebinDataLeakChallenge)[0]

const API_URL = 'http:https://localhost:3000/api'
const REST_URL = 'http:https://localhost:3000/rest'
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/contactSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import config = require('config')
import { $, $$, browser, by, element, ElementFinder, protractor } from 'protractor'
import { basePath, beforeEachLogin, expectChallengeSolved } from './e2eHelpers'
import { Product } from '../../data/types'

const utils = require('../../lib/utils')
const pastebinLeakProduct = config.get('products').filter(product => product.keywordsForPastebinDataLeakChallenge)[0]
const pastebinLeakProduct = config.get('products').filter((product: Product) => product.keywordsForPastebinDataLeakChallenge)[0]

describe('/#/contact', () => {
let comment: ElementFinder, rating: ElementFinder, submitButton: ElementFinder, captcha: ElementFinder, snackBar: ElementFinder
Expand Down
3 changes: 2 additions & 1 deletion test/e2e/restApiSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import config = require('config')
import { $, browser, by, element, protractor } from 'protractor'
import { basePath, beforeEachLogin, expectChallengeSolved } from './e2eHelpers'
import { Product } from '../../data/types'

const models = require('../../models/index')
const utils = require('../../lib/utils')
Expand Down Expand Up @@ -62,7 +63,7 @@ describe('/api', () => {

describe('challenge "changeProduct"', () => {
const tamperingProductId = ((() => {
const products = config.get('products')
const products: Product[] = config.get('products')
for (let i = 0; i < products.length; i++) {
if (products[i].urlForProductTamperingChallenge) {
return i + 1
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/searchSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import config = require('config')
import { $, browser, by, element, ElementFinder, protractor } from 'protractor'
import { basePath, beforeEachLogin, expectChallengeSolved } from './e2eHelpers'
import { Product } from '../../data/types'

const christmasProduct = config.get('products').filter(product => product.useForChristmasSpecialChallenge)[0]
const pastebinLeakProduct = config.get('products').filter(product => product.keywordsForPastebinDataLeakChallenge)[0]
const christmasProduct = config.get('products').filter((product: Product) => product.useForChristmasSpecialChallenge)[0]
const pastebinLeakProduct = config.get('products').filter((product: Product) => product.keywordsForPastebinDataLeakChallenge)[0]
const models = require('../../models/index')

describe('/#/search', () => {
Expand Down

0 comments on commit 56e1830

Please sign in to comment.