Skip to content
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 tests to init command #84

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d730a42
Adds jest as a dev-dependency
lricoy Dec 29, 2017
8c767ac
Includes jest and associated types as a dev-dependency
lricoy Dec 29, 2017
e6c26dd
Adds jsdocs to default export on auth module
lricoy Dec 29, 2017
b7bd70d
Adds initial tests to the auth module
lricoy Dec 29, 2017
09a77a4
Renames spyConsole to spyOnConsoleLog
lricoy Dec 29, 2017
9c0818d
Setups teardown function
lricoy Dec 29, 2017
7a9755a
Removes extra lines
lricoy Dec 29, 2017
b531644
Applies prettier on gapps commander file
lricoy Dec 29, 2017
49078bf
Formats tests with prettier
lricoy Dec 29, 2017
b6a62e2
Adds missing jsdocs to auth command
lricoy Dec 29, 2017
e6f1200
Removes early exit added for tests
lricoy Dec 29, 2017
84dce78
Defaults withWebServer to true and camelCase the var name
lricoy Dec 29, 2017
c206d83
Changes console.error to console.log
lricoy Dec 29, 2017
a8cfce2
Merge branch 'master' into add_tests
lricoy Dec 29, 2017
04c49fa
Defaults main auth method to use webServer as default
lricoy Dec 29, 2017
990b32d
Merge branch 'master' into add_tests
lricoy Dec 29, 2017
86974f7
Merge branch 'master' into add_tests
lricoy Dec 29, 2017
d92228b
Fixes format
lricoy Dec 29, 2017
8fa40ac
Uses single quote instead of double
lricoy Dec 29, 2017
933f466
Merge branch 'master' into add_tests
lricoy Dec 29, 2017
6fae38d
Merge branch 'add_tests' of github.com:danthareja/node-google-apps-sc…
lricoy Dec 29, 2017
e782e54
Merge branch 'add_tests' into auth-tests
lricoy Dec 29, 2017
447deac
WIP - init tests
aurelienshz Dec 31, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/__mocks__/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = {
DEFAULT_SUBDIR: 'src',
STORAGE_FILE: '/tmp/.gapps-test',
CONFIG_NAME: 'gapps-test.config.json',
WEBSERVER_PORT: 2386,
DOWNLOAD_URL: 'https://script.google.com/feeds/download/export?format=json&id='
};
32 changes: 26 additions & 6 deletions lib/commands/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ const GOOGLE_AUTH_SCOPE = [
'https://www.googleapis.com/auth/drive.scripts'
];

module.exports = function auth(clientSecretPath, withWebserver) {
/**
* Tries to find the storage file. If found, tells the users to remove it, otherwiser runs the
* authentication flow using the provided path the to the clientSecret path
* @param {String} clientSecretPath
* @param {Boolean} withWebServer
* @returns {Promise}
*/
module.exports = function auth(clientSecretPath, withWebServer = true) {
return fs
.lstatAsync(defaults.STORAGE_FILE)
.then(() => {
Expand All @@ -27,20 +34,29 @@ module.exports = function auth(clientSecretPath, withWebserver) {
throw defaults.STORAGE_FILE +
' does not exist yet. Specify a credential file with `--auth ~/Downloads/client_secret_abcd.json`';
}
return performAuthenticationFlow(clientSecretPath, withWebserver);
return performAuthenticationFlow(clientSecretPath, withWebServer);
});
};

function performAuthenticationFlow(clientSecretPath, withWebserver) {
/**
* Performs the authentication flow by reading the .json file
* and using it's content to authenticate with Google. When
* done, clean ups the stdin.
* @param {String} clientSecretPath
* @param {Boolean} withWebServer
* @return {Promise}
*/
function performAuthenticationFlow(clientSecretPath, withWebServer) {
return getCredentialsFromFile(clientSecretPath)
.then(credentials => authenticateWithGoogle(credentials, withWebserver))
.then(credentials => authenticateWithGoogle(credentials, withWebServer))
.then(saveAuthenticationConfig)
.then(cleanUp)
.then(() => {
console.log('Successfully Authenticated with Google Drive!'.green);
})
.catch(err => {
console.log('Error running auth command'.underline.red);
console.log(err.red);
throw err;
});
}
Expand Down Expand Up @@ -73,7 +89,7 @@ function getCredentialsFromFile(clientSecretPath) {
return {
client_id: credentials.installed.client_id,
client_secret: credentials.installed.client_secret,
redirect_uri: credentials.installed.redirect_uris[0],
redirect_uri: credentials.installed.redirect_uris[0]
};
})
.catch(SyntaxError, err => {
Expand Down Expand Up @@ -137,7 +153,7 @@ function authenticateWithGoogle(credentials, withWebServer) {
} else {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
output: process.stdout
});

rl.question('Copy the provided code and paste it here: ', code => {
Expand All @@ -160,6 +176,10 @@ function saveAuthenticationConfig(credentials) {
);
}

/**
* Destroys the stdin
* @todo put inline if only used here
*/
function cleanUp() {
process.stdin.destroy();
}
Loading