Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
addressing some concerns in the pull request comment and adding mock …
Browse files Browse the repository at this point in the history
…passport init for testing when using mocks.
  • Loading branch information
elixic committed Mar 16, 2015
1 parent b6f8bac commit 3b7eb23
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/client/src/modules/app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports =
.config(function ($urlRouterProvider, $httpProvider) {
$urlRouterProvider.otherwise('/');
$httpProvider.interceptors.push('authInterceptor');
}).factory('authInterceptor', function ($rootScope, $q, $location) {
}).factory('authInterceptor', function ($q, $location) {
return {
responseError(response) {
if (response.status === 401) {
Expand Down
4 changes: 2 additions & 2 deletions app/server/components/repositories/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ module.exports = {
});
},

isMember (username) {
return userUtil.isMember(username).then(function (data) {
isOrgMember (username) {
return userUtil.isOrgMember(username).then(function (data) {
return "204 No Content" === data.meta.success;
});
}
Expand Down
4 changes: 2 additions & 2 deletions app/server/components/repositories/util/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ module.exports = {
return provider.github.getUsers(args).then(users => users.map(user => convertGithubUser(user)));
},

isMember(username) {
return provider.github.isMember({
isOrgMember(username) {
return provider.github.isOrgMember({
org: provider.github.config.org,
username: username
});
Expand Down
2 changes: 1 addition & 1 deletion app/server/components/services/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ module.exports = {
config: {
org: org
},
isMember: Bluebird.promisify(github.orgs.getMember),
isOrgMember: Bluebird.promisify(github.orgs.getMember),
getUsers: Bluebird.promisify(github.orgs.getMembers),
getUser: Bluebird.promisify(github.user.getFrom),
getRepos: Bluebird.promisify(github.repos.getFromOrg),
Expand Down
8 changes: 8 additions & 0 deletions app/server/components/services/github.mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,13 @@ module.exports = {
reject(new Error('No mock team: ' + id));
}
});
},

isOrgMember (msg) {
return new Promise((resolve, reject) => {
resolve({ meta: {
success: "204 No Content"
}});
});
}
};
2 changes: 1 addition & 1 deletion app/server/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exports.start = () => {
failureCallback: '/auth/failure'
},
session: {
secret: 'keyboard cat',
secret: process.env.SESSION_SECRET || 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: {
Expand Down
2 changes: 1 addition & 1 deletion app/server/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module.exports = {
isAuthenticated (req, res, next) {
var authenticated = req.isAuthenticated();

if (authenticated && users.isMember(req.session.passport.user.username)) {
if (authenticated && users.isOrgMember(req.session.passport.user.username)) {
next();
} else {
res.send(401);
Expand Down
27 changes: 27 additions & 0 deletions app/server/mock-passport-middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';

//jscs:disable disallowDanglingUnderscores
module.exports = {
initialize(mockUser) {
return function (req, res, next) {
var passport = {};
passport._key = 'passport';
passport._userProperty = 'user';
passport.serializeUser = (user, req, done) => {
done(null, user);
};
passport.deserializeUser = (user, req, done) => {
done(null, user);
};

req._passport = { instance: passport };
req._passport.session = { user: mockUser };
req.session.passport = { user: mockUser };

next();
};
},

// TODO ... PUT Mock users in a seperate file so we can tests users that are
mockUser: { username: "TestUser", displayName: "Test User", id: 1 }
};
10 changes: 8 additions & 2 deletions app/server/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,16 @@ module.exports = {
done(null, user);
});

app.use(passport.initialize());
if (process.env.SERVICE === 'mock') {
// TODO ... is there a real di way to do this??
console.log('using the mock passport middlware');
var mock = require('./mock-passport-middleware');
app.use(mock.initialize(mock.mockUser));
} else {
app.use(passport.initialize());
}
app.use(passport.session());


app.get('/auth/authenticated', function (req, res) {
var authenticated = req.isAuthenicated();
if (authenticated) {
Expand Down

1 comment on commit 3b7eb23

@natoverse
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:shipit:

Please sign in to comment.