Skip to content

Commit

Permalink
chore: add gmail samples (#1046)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Mar 7, 2018
1 parent a0834cf commit b25d57e
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
82 changes: 82 additions & 0 deletions samples/gmail/labels.js
@@ -0,0 +1,82 @@
// Copyright 2018, Google, LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const { google } = require('googleapis');
const sampleClient = require('../sampleclient');

const gmail = google.gmail({
version: 'v1',
auth: sampleClient.oAuth2Client
});

function runSample (action, messageId, labelId, callback) {
if (action === 'add') {
gmail.users.messages.modify({
userId: 'me',
id: messageId,
resource: {
'addLabelIds': [labelId]
}
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
callback(res.data);
});
} else if (action === 'remove') {
gmail.users.messages.modify({
userId: 'me',
id: messageId,
resource: {
'removeLabelIds': [labelId]
}
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
callback(res.data);
});
}
}

const scopes = ['https://www.googleapis.com/auth/gmail.modify'];

if (module === require.main) {
sampleClient.authenticate(scopes, err => {
if (err) {
throw err;
}
if (process.argv.length !== 5) {
showUsage();
}
const [action, messageId, labelId] = process.argv.slice(2);
console.log(`action: ${action}`);
console.log(`messageId: ${messageId}`);
console.log(`labelId: ${labelId}`);
runSample(action, messageId, labelId, () => { /* add complete */ });
});
}

function showUsage () {
console.info('USAGE: node labels.js <add|remove> <messageId> <labelId>');
process.exit();
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client
};
50 changes: 50 additions & 0 deletions samples/gmail/list.js
@@ -0,0 +1,50 @@
// Copyright 2018, Google, LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {google} = require('googleapis');
const sampleClient = require('../sampleclient');

const gmail = google.gmail({
version: 'v1',
auth: sampleClient.oAuth2Client
});

function runSample (callback) {
gmail.users.messages.list({
userId: 'me'
}, (err, res) => {
if (err) {
throw err;
}
console.log(res.data);
callback(res.data);
});
}

const scopes = ['https://www.googleapis.com/auth/gmail.readonly'];

if (module === require.main) {
sampleClient.authenticate(scopes, err => {
if (err) {
throw err;
}
runSample(() => { /* sample complete */ });
});
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client
};
64 changes: 64 additions & 0 deletions test/samples/test.samples.gmail.ts
@@ -0,0 +1,64 @@
// Copyright 2018, Google, LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as assert from 'assert';
import * as fs from 'fs';
import * as nock from 'nock';
import * as os from 'os';
import * as path from 'path';

import {Utils} from './../utils';

nock.disableNetConnect();

const samples = {
list: require('../../../samples/gmail/list'),
labels: require('../../../samples/gmail/labels')
};

for (const p in samples) {
if (samples[p]) {
samples[p].client.credentials = {access_token: 'not-a-token'};
}
}

describe('gmail samples', () => {
afterEach(() => {
nock.cleanAll();
});

it('should list emails', done => {
const scope =
nock(Utils.baseUrl).get(`/gmail/v1/users/me/messages`).reply(200, {
yes: true
});
samples.list.runSample(data => {
assert(data);
scope.done();
done();
});
});

it('should add a label', done => {
const messageId = '12345';
const labelId = 'abcde';
const scope = nock(Utils.baseUrl)
.post(`/gmail/v1/users/me/messages/${messageId}/modify`)
.reply(200, {yes: true});
samples.labels.runSample('add', messageId, labelId, data => {
assert(data);
scope.done();
done();
});
});
});

0 comments on commit b25d57e

Please sign in to comment.