• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ringcentral / google-forms-notification-add-in / 8434520061

26 Mar 2024 10:32AM UTC coverage: 96.959% (-0.03%) from 96.99%
8434520061

Pull #62

github

embbnux
chore: remove async
Pull Request #62: misc: pass token in header

219 of 235 branches covered (93.19%)

Branch coverage included in aggregate %.

19 of 19 new or added lines in 4 files covered. (100.0%)

642 of 653 relevant lines covered (98.32%)

14.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.56
/src/server/handlers/authorizationHandler.js
1
const { User } = require('../models/userModel');
4✔
2
const { Subscription } = require('../models/subscriptionModel');
4✔
3
const { GoogleClient } = require('../lib/GoogleClient');
4✔
4

5
async function onAuthorize(accessToken, refreshToken, expires) {
6
  const googleClient = new GoogleClient({ token: accessToken });
3✔
7
  const userInfoResponse = await googleClient.getUserInfo();
3✔
8
  const userId = userInfoResponse.sub;
3✔
9

10
  let user = await User.findByPk(userId);
3✔
11
  if (!user) {
3✔
12
    user = await User.create({
1✔
13
      id: userId,
14
      accessToken: accessToken,
15
      refreshToken: refreshToken,
16
      tokenExpiredAt: expires,
17
      name: userInfoResponse.name,
18
      subscriptions: [],
19
    });
20
  }
21
  else {
22
    user.accessToken = accessToken;
2✔
23
    if (refreshToken) {
2✔
24
      user.refreshToken = refreshToken;
1✔
25
    }
26
    user.tokenExpiredAt = expires;
2✔
27
    await user.save();
2✔
28
  }
29

30
  return userId;
3✔
31
}
32

33
function getUniqueSubscriptions(subscriptions) {
34
  const data = {};
6✔
35
  subscriptions.forEach((subscription) => {
6✔
36
    data[subscription.id] = subscription;
4✔
37
  });
38
  return Object.keys(data).map((key) => data[key]);
6✔
39
}
40

41
async function onUnauthorize(user) {
42
  const googleClient = new GoogleClient({ token: user.accessToken });
6✔
43
  const subscriptions = user.subscriptions;
6✔
44
  const uniqueSubscriptions = getUniqueSubscriptions(subscriptions);
6✔
45
  await Promise.all(uniqueSubscriptions.map(async (subscription) => {
6✔
46
    try {
3✔
47
      await googleClient.deleteWatch(subscription.formId, subscription.id);
3✔
48
      await Subscription.destroy({ where: { id: subscription.id } });
2✔
49
    } catch (e) {
50
      console.error('Failed to delete watch: ', subscription.id);
1✔
51
      console.error(e);
1✔
52
    }
53
  }));
54
  await googleClient.revokeToken(user.refreshToken || user.accessToken);
6!
55
  user.accessToken = '';
5✔
56
  user.refreshToken = '';
5✔
57
  user.subscriptions = [];
5✔
58
  await user.save();
5✔
59
}
60

61
exports.onAuthorize = onAuthorize;
4✔
62
exports.onUnauthorize = onUnauthorize;
4✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc