Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Brecht De Rooms committed Apr 17, 2020
1 parent 869d9a8 commit c1a19a4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 38 deletions.
1 change: 0 additions & 1 deletion scripts/populate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ const main = async () => {

let adminKey = process.env.REACT_APP_LOCAL___ADMIN

console.log(adminKey)
if (!adminKey) {
const interactiveSession = readline.createInterface({
input: process.stdin,
Expand Down
19 changes: 3 additions & 16 deletions src/fauna/queries/fweets.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ beforeAll(async () => {
} catch (err) {
console.error(err)
}
}, 60000)
}, 600000)

it('A logged in user cant create tweets since it needs to use the UDF', function() {
return expect(
Expand All @@ -79,22 +79,10 @@ it('The initial client can only call the UDF functions, it cant access tweets',
return expect(getFweets(bootstrapClient)).rejects.toHaveProperty(['message'], 'permission denied')
}, 60000)

it('A logged in user can retrieve tweets, but will not see any until it follows these people', function() {
return getFweets(loggedInClient)
.then(res => {
console.log(res)
expect(res).toMatchObject([])
})
.catch(err => {
console.error(err)
throw err
})
}, 60000)

it('A logged in user can retrieve tweets, but will not see any until it follows these people', function() {
it('A logged in user can retrieve tweets, but will only see his own until it follows these people', function() {
return getFweets(loggedInClient)
.then(res => {
expect(res).toMatchObject([])
expect(res.length).toBe(1)
})
.catch(err => {
console.error(err)
Expand All @@ -108,7 +96,6 @@ it('A user that follows another user, sees their fweets and all associated data'
return getFweets(loggedInClient)
})
.then(res => {
console.log(res)
expect(res).toHaveProperty([0])
expect(res).toHaveProperty([0, 'fweet'])
expect(res).toHaveProperty([0, 'fweet', 'comments']) // the number of comments
Expand Down
20 changes: 12 additions & 8 deletions src/fauna/queries/rate-limiting.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ it('We can rate-limit functions on identity and call them within the limit', fun
console.error(err)
console.log(err.requestResult.responseContent.errors)
})
}, 20000)
}, 60000)

it('Edge case, we can call exactly the limit amount of items', function() {
// For testing, Let's just take a very silly add function
Expand All @@ -101,7 +101,7 @@ it('Edge case, we can call exactly the limit amount of items', function() {
console.error(err)
console.log(err.requestResult.responseContent.errors)
})
}, 20000)
}, 60000)

it('Rate limiting kicks in if we go over', function() {
// For testing, Let's just take a very silly add function
Expand All @@ -118,7 +118,7 @@ it('Rate limiting kicks in if we go over', function() {
.then(() => loggedInClient.query(RateLimitedSillySum))
.then(() => loggedInClient.query(RateLimitedSillySum))
).rejects.toHaveProperty(['message'], 'transaction aborted')
}, 20000)
}, 60000)

it('Rate limiting is scoped per user since we added Identity', function() {
let loggedInClient = null
Expand All @@ -139,8 +139,12 @@ it('Rate limiting is scoped per user since we added Identity', function() {
.then(() => loggedInClient.query(RateLimitedSillySum))
.then(() => loggedInClient.query(RateLimitedSillySum))
.then(res => expect(res).toBe(5))
.catch(err => {
console.log(err)
throw err
})
)
}, 20000)
}, 60000)

it('We can rate-limit functions globally (e.g. for register) by providing a constant', function() {
// let's just set the constant 'silly_sum_global' as the key
Expand All @@ -163,7 +167,7 @@ it('We can rate-limit functions globally (e.g. for register) by providing a cons
.then(() => loggedInClient.query(RateLimitedSillySum))
.then(() => loggedInClient.query(RateLimitedSillySum))
).rejects.toHaveProperty(['message'], 'transaction aborted')
}, 20000)
}, 60000)

it('Everything is fine if we wait a while', function() {
// For testing, Let's just take a very silly add function
Expand All @@ -190,7 +194,7 @@ it('Everything is fine if we wait a while', function() {
console.error(err)
console.log(err.requestResult.responseContent.errors)
})
}, 20000)
}, 60000)

it('We can omit the time unit by passing in zeros to ', function() {
// We could have a rate limit that never resets!
Expand All @@ -214,7 +218,7 @@ it('We can omit the time unit by passing in zeros to ', function() {
.then(() => loggedInClient.query(RateLimitedSillySum))
.then(() => loggedInClient.query(RateLimitedSillySum))
).rejects.toHaveProperty(['message'], 'transaction aborted')
}, 20000)
}, 60000)

it('We could have logic that manually resets the rate-limiting ', function() {
// The constant could be something else that identifies a user (e.g. the e-mail)
Expand Down Expand Up @@ -248,4 +252,4 @@ it('We could have logic that manually resets the rate-limiting ', function() {
.then(() => loggedInClient.query(RateLimitedSillySum))
.then(() => loggedInClient.query(RateLimitedSillySum))
.then(res => expect(res).toBe(5))
}, 20000)
}, 60000)
2 changes: 1 addition & 1 deletion src/fauna/queries/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function searchPeopleAndTags(client, keyword) {
Let(
{
// Matching an index returns a setref.
setref: Match(Index('hashtags_and_users_by_wordparts'), keyword),
setref: Match(Index('hashtags_and_users_by_wordparts'), keyword.toLowerCase()),
// We materialize this setref (get the actual index values) to be able to map over it.
// We only consider the first page which we'll set to 10 elements, this should be enough for an autocomplete.
pages: Paginate(Var('setref'), { size: 10 }),
Expand Down
22 changes: 12 additions & 10 deletions src/fauna/queries/search.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ beforeEach(async () => {
await adminClient.query(DeleteAllRatelimiting)
// register two test users
await waitForIndexActive(adminClient, 'hashtags_and_users_by_wordparts')
// we will search on the handles of users (the last parameter)
await registerWithUser(adminClient, '[email protected]', 'testtest', 'Test1', 'MrStrawberry')
await registerWithUser(adminClient, '[email protected]', 'testtest', 'Test1', 'SirPepper')
// we will search on the names of users (the second last parameter)
await registerWithUser(adminClient, '[email protected]', 'testtest', 'MrStrawberry', 'userHandle1')
await registerWithUser(adminClient, '[email protected]', 'testtest', 'SirPepper', 'userHandle2')
// and the hashtags
await adminClient.query(CreateHashtags(['Berries']))
await adminClient.query(CreateHashtags(['Pepper', 'Raw Honey']))
Expand Down Expand Up @@ -105,30 +105,32 @@ it('We can autocomplete tags and user handles', function() {
})
})
.then(res => {
return searchPeopleAndTags(loggedInClient, 'pppppppp').then(res => {
return searchPeopleAndTags(loggedInClient, 'ppppppppppppppppppppp').then(res => {
expect(res.length).toBe(1) // for very long names we have to type a longer part
return res
})
})
.then(res => {
return searchPeopleAndTags(loggedInClient, 'berr').then(res => {
expect(res.length).toBe(2) // we find the tag berries as well as the user MrStrawberry
expect(res).toHaveProperty([0, 'handle'], 'MrStrawberry')
expect(res).toHaveProperty([1, 'name'], 'Berries')
expect(res).toHaveProperty([1, 'name'], 'MrStrawberry')
expect(res).toHaveProperty([0, 'name'], 'Berries')
return res
})
})
.then(res => {
return searchPeopleAndTags(loggedInClient, 'pe').then(res => {
expect(res.length).toBe(2) // we find the tag Pepper as well as the user SirPepper
expect(res).toHaveProperty([0, 'handle'], 'SirPepper')
expect(res).toHaveProperty([1, 'name'], 'Pepper')
expect(res).toHaveProperty([1, 'name'], 'SirPepper')
expect(res).toHaveProperty([0, 'name'], 'Pepper')
return res
})
})
.catch(err => {
console.error(err)
console.log(err.requestResult.responseContent.errors)
if (err.requestResult) {
console.log(err.requestResult.responseContent.errors)
}
throw err
})
)
}, 60000)
3 changes: 3 additions & 0 deletions src/fauna/setup/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ async function setupDatabaseRateLimitingSpec(client) {
await handleSetupError(createUsersCollection(client), 'users collection')
await handleSetupError(createAccountCollection(client), 'accounts collection')
await handleSetupError(createRateLimitingCollection(client), 'profiles collection')
await handleSetupError(createFollowerStatsCollection(client), 'followerstats collection')

console.log('4a. -- Roles -- Creating security roles to be assumed by the functions')
await handleSetupError(client.query(CreateFnRoleLogin), 'function role - login') // without rate limiting: createFnRoleRegisterWithoutRateLimiting
Expand All @@ -129,6 +130,7 @@ async function setupDatabaseAuthSpec(client) {
await handle(createAccountCollection(client), 'Create Accounts Collection')
await handle(createUsersCollection(client), 'Create Users Collection')
await handle(createRateLimitingCollection(client), 'Create Rate Limiting Collection')
await handleSetupError(createFollowerStatsCollection(client), 'followerstats collection')
await handle(client.query(CreateFnRoleLoginWithoutRateLimiting), 'Create Login Fn role (no rate limiting)')
await handle(client.query(CreateFnRoleRegisterWithoutRateLimiting), 'Create Register Fn role (no rate limiting)')
await handle(client.query(CreateLoginSimpleUDF), 'Create Login UDF')
Expand All @@ -143,6 +145,7 @@ async function setupDatabaseSearchSpec(client) {
await handleSetupError(createRateLimitingCollection(client), 'Create Rate Limiting Collection')
await handleSetupError(createHashtagCollection(client), 'Create Hashtag Collection')
await handleSetupError(createSearchIndexes(client), 'Create Search Indexes')
await handleSetupError(createFollowerStatsCollection(client), 'followerstats collection')
await handleSetupError(client.query(CreateFnRoleLogin), 'function role - login')
await handleSetupError(client.query(CreateFnRoleRegister), 'function role - register')
await handleSetupError(client.query(CreateFnRoleRegisterWithUser), 'function role - register with user')
Expand Down
5 changes: 3 additions & 2 deletions src/fauna/setup/searching.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const {
LowerCase,
Filter,
GT,
Union
Union,
Distinct
} = q

/** ********************** Searching ************************/
Expand Down Expand Up @@ -183,7 +184,7 @@ const CreateHashtagsAndUsersByWordpartsWithBinding2 = CreateIndex({
fields: {
length: Query(Lambda('hashtagOrUser', Length(Select(['data', 'name'], Var('hashtagOrUser'))))),
wordparts: Query(
Lambda('hashtagOrUser', Union(WordPartGenerator(Select(['data', 'name'], Var('hashtagOrUser')))))
Lambda('hashtagOrUser', Distinct(Union(WordPartGenerator(Select(['data', 'name'], Var('hashtagOrUser'))))))
)
}
}
Expand Down

0 comments on commit c1a19a4

Please sign in to comment.