Comments (4)
Hmmm, I gather you are trying to test the server-side code here? Unit tests or integration/functional tests?
When unit testing my own code which uses 3rd party libraries I often use SinonJS to mock/stub out the external functions that are called in whatever function I'm testing. That way you can isolate the function you're testing without involving other dependent libraries.
For functional/integration tests I have often used supertest. This will fire up a server instance, so that you can perform automated HTTP requests against it, and enables you to test the entire dependency chain.
I'm not sure if I answered your question well, so be sure to tell me if I need to clarify anything :)
from angular-client-side-auth.
Thanks for the reply!
Yes, these are server side integration/functional tests. Below is a code sample of what i'm trying to do. The problem is, after I log in, I go to the next test and the application dosen't know i'm logged in as a user and returns a 403 instead of a 200 on the /users
call.
var request = require('supertest'),
expect = require('expect.js');
//enter your domain
var baseURL = "http://localhost:8000/";
describe('Test Login', function (done) {
it('First see if the home page loads', function(done) {
request(baseURL)
.get('')
.end( function(err, result) {
expect(result.res.statusCode).to.be(200);
done();
});
});
it('then lets log in', function(done) {
var user = {'username':'admin','password':'123'};
request(baseURL)
.post('login')
.send(user)
.end( function(err, result) {
// Note, result.body has this is it...
// { role: { bitMask: 4, title: 'admin' }, username: 'admin' }
// so I know the login worked
expect(result.res.statusCode).to.be(200);
done();
});
});
it('now we should be able to do a get call /users because we are loged in', function(done) {
request(baseURL)
.get('users')
.end( function(err, result) {
/* this returns a 403 */
expect(result.res.statusCode).to.be(200);
done();
});
});
});
from angular-client-side-auth.
Okay. I got this working with the help some guys who worked on passport-stub
In server.js:
Change this line
var app = express();
To
var app = module.exports = express();
And here is my test scrip
var app = require('../server');
var request = require('supertest');
var passportStub = require('passport-stub');
passportStub.install(app);
describe('Test Login', function (done) {
afterEach(function() {
passportStub.logout(); //logout after each test
});
it('First see if the home page loads', function(done) {
request(app).get('/').expect(200, done);
});
it('a logouted out user shouldnt be able to get to this page', function(done) {
request(app).get('/users').expect(403, done);
});
it('then lets log in', function(done) {
var user = {'username':'admin','password':'123'};
request(app).post('/login').send(user).expect(200, done);
});
it('now we should be able to do a get call /users because we are loged in', function(done) {
passportStub.login({ id: 2, username: 'admin', password: '123', role: { bitMask: 4, title: 'admin' } });
request(app).get('/users').expect(200, done);
});
});
If you want, I can make a pull request so other people can use this.
from angular-client-side-auth.
Ah, right. So you're passing the entire server setup to Passport Stub, which I guess stubs out the necessary Passport components. I'd be happy to receive a pull request!
from angular-client-side-auth.
Related Issues (20)
- Replace $cookieStore to $cookies HOT 1
- I am new to npm and angular, I tried on windows 8.1, i was able to complete npm install, but npm start gave error HOT 1
- Server depends on a file located in the client directory HOT 1
- book mark HOT 2
- npm install throwing me to include npm-debug.log
- react js issue
- npm run build error HOT 3
- Can't start the "npm run dev" on my ubuntu 17.10 Laravel 5.5
- I am new with Node and it gives me error whenever i run npm start command.Can anyone please help me....
- i have error when i write npm start
- I'm new with Node
- Unresolved variable or type routingConfig HOT 2
- Google Logout HOT 2
- How about using JWT instead of cookies HOT 5
- "data.access" in toState is always false HOT 2
- Integration with Django HOT 3
- Social sites login
- Question about passing the user to HTTP GET * HOT 1
- Use ng-if instead CSS HOT 5
- any sample with asp.net mvc HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from angular-client-side-auth.