-
Notifications
You must be signed in to change notification settings - Fork 327
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
Add HTTP tests for /source route #2036
Closed
AlexeySachkov
wants to merge
7
commits into
naturalcrit:master
from
AlexeySachkov:private/asachkov/source-route-tests
Closed
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
35c65cf
Add tests for source/ route
AlexeySachkov e0d28d2
Refactor tests, mock GoogleActions module
AlexeySachkov 580ab4f
Rename test file
AlexeySachkov 64190e2
Updates after rebase + lint
AlexeySachkov aa668db
Update mocked Google Action names
calculuschild 997e792
Merge branch 'master' into private/asachkov/source-route-tests
6a2581e
Add config for "test" environment
AlexeySachkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const GoogleActionsMock = { | ||
authCheck : (account, res)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
getGoogleFolder : async (auth)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
listGoogleBrews : async (req, res)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
existsGoogleBrew : async (auth, id)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
updateGoogleBrew : async (auth, brew)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
newGoogleBrew : async (auth, brew)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
readFileMetadata : async (auth, id, accessId, accessType)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
deleteGoolgeBrew : async (req, res, id)=>{ | ||
// FIXME: implement | ||
return null; | ||
}, | ||
increaseView : async (id, accessId, accessType, brew)=>{ | ||
// FIXME: implement | ||
return null; | ||
} | ||
}; | ||
|
||
module.exports = GoogleActionsMock; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const supertest = require('supertest'); | ||
|
||
// We need to mock google actions to avoid accessing real servers in our tests | ||
// (there are limits on unauthorized access) | ||
jest.mock('googleActions.js'); | ||
|
||
// Mimic https responses to avoid being redirected all the time | ||
const app = supertest.agent(require('app.js').app) | ||
.set('X-Forwarded-Proto', 'https'); | ||
|
||
const DB = require('db.js'); | ||
const Homebrew = require('homebrew.model.js'); | ||
|
||
const config = require('config.js'); | ||
|
||
describe('/source/:id', ()=>{ | ||
beforeAll(()=>{ | ||
return DB.connect(config); | ||
}); | ||
|
||
beforeAll(async ()=>{ | ||
const regularBrew = new Homebrew.model({ | ||
shareId : 'share-id-1', | ||
text : 'This is text', | ||
authors : ['this', 'is', 'list', 'of', 'authors'] | ||
}); | ||
await regularBrew.save(); | ||
|
||
const brewWithSpecialSymbol = new Homebrew.model({ | ||
shareId : 'share-id-2', | ||
text : '<div>&</div>' | ||
}); | ||
await brewWithSpecialSymbol.save(); | ||
}); | ||
|
||
it('able to return a source of an existing brew', ()=>{ | ||
return app.get('/source/share-id-1') | ||
.send() | ||
.expect(200) | ||
.then((response)=>{ | ||
expect(response.text).toBe('<code><pre style="white-space: pre-wrap;">This is text</pre></code>'); | ||
}); | ||
}); | ||
|
||
it('encodes special symbols', ()=>{ | ||
return app.get('/source/share-id-2') | ||
.send() | ||
.expect(200) | ||
.then((response)=>{ | ||
expect(response.text).toBe('<code><pre style="white-space: pre-wrap;"><div>&</div></pre></code>'); | ||
}); | ||
}); | ||
|
||
it('sets the correct response headers', ()=>{ | ||
return app.get('/source/share-id-1') | ||
.send() | ||
.expect(200) | ||
.then((response)=>{ | ||
expect(response.headers).toHaveProperty('content-type', 'text/html; charset=utf-8'); | ||
}); | ||
}); | ||
|
||
it('returns an error for a non-existing brew', ()=>{ | ||
return app.get('/source/invalid-id') | ||
.send() | ||
// FIXME: we should be expecting 404 Not Found (#1983) | ||
.expect(500); | ||
}); | ||
|
||
it('returns an error for a non-existing brew (google id)', ()=>{ | ||
return app.get('/source/non-existing-brew-id') | ||
.send() | ||
// FIXME: we should be expecting 404 Not Found (#1983) | ||
.expect(500); | ||
}); | ||
|
||
// FIXME: we should return an error instead of a home page here | ||
it.skip('returns an error for a missing brew id', ()=>{ | ||
return app.get('/source/') | ||
.send() | ||
.expect(404); | ||
}); | ||
|
||
// FIXME: we should return an error instead of a home page here | ||
it.skip('returns an error for a missing brew id #2', ()=>{ | ||
return app.get('/source') | ||
.send() | ||
.expect(404); | ||
}); | ||
|
||
// FIXME: add tests for retrieving a Google brew source | ||
|
||
afterAll(()=>{ | ||
return Homebrew.model.deleteMany(); | ||
}); | ||
|
||
afterAll(()=>{ | ||
return DB.disconnect(); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AlexeySachkov Um.... this deletes all brews in the database, not just the tested ones. Just ran it on my local install and sure enough, everything was deleted. We definitely don't want this running on the live server.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thing that you have spotted that. Actually, I would expect tests to be connected to a different DB, because they are launched under "test" node environment. Probably I don't fully understand the API to work with Mongo here.
I suggest we block this PR from an accidental merge by explicitly requesting changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@calculuschild , sorry that it took so long for me to respond here, but I finally figured it out: what you saw is indeed happening. The reason for that is that even in tests we by default connect to the main database.
In 6a2581e I added
config/test.json
to override database in test environment.However, there is still caveat:
jest
setsNODE_ENV=test
only ifNODE_ENV
is not set yet (link to docs). So, in local environment where you likely haveNODE_ENV=local
, it won't work.I wonder what would be the preferred way of handling that? I doubt that ignoring database cleanup in local environment is a good idea, because someone who has local database with their brews won't be happy to see them all removed just by (possibly accidentally) running tests. We could update
package.json
to includeNODE_ENV=test
to all test-related commands, but I'm concerned that we might miss some, because there are 4 of them already.