All files / lib calendar.js

100% Statements 8/8
100% Branches 0/0
100% Functions 6/6
100% Lines 8/8
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71              36x               7x         7x                               14x 14x         3x         5x                               2x              
import path from 'path';
import { fs, mkdirp } from 'appium-support';
import { execSQLiteQuery } from './utils';
 
export default class Calendar {
 
  constructor (sharedResourcesDir) {
    this.sharedResourcesDir = sharedResourcesDir;
  }
 
  async getDB () {
    if (this.db) {
      return this.db;
    }
 
    let tccPath = path.resolve(this.sharedResourcesDir, 'Library', 'TCC');
    if (!(await fs.exists(tccPath))) {
      await mkdirp(tccPath);
    }
 
    this.db = path.resolve(tccPath, 'TCC.db');
    await execSQLiteQuery(this.db, `CREATE TABLE IF NOT EXISTS access (
      service TEXT NOT NULL DEFAULT '',
      client TEXT NOT NULL DEFAULT '',
      client_type INTEGER,
      allowed INTEGER,
      prompt_count INTEGER,
      csreq BLOB NOT NULL DEFAULT '',
      policy_ID INTEGER,
      PRIMARY KEY(service, client, client_type)
    );`.replace(/\n/g, ''));
 
    return this.db;
  }
 
  async getCalendarRowCount (bundleID) {
    let db = await this.getDB();
    let count = await execSQLiteQuery(db, `SELECT count(*) FROM access WHERE client='?' AND service='kTCCServiceCalendar';`, bundleID);
    return parseInt(count.stdout.split('=')[1], 10);
  }
 
  async hasCalendarAccess (bundleID) {
    let count = await execSQLiteQuery(await this.getDB(), `SELECT count(*) FROM access WHERE client='?' AND allowed=1 AND service='kTCCServiceCalendar';`, bundleID);
    return parseInt(count.stdout.split('=')[1], 10) > 0;
  }
 
  async enableCalendarAccess (bundleID) {
    let db = await this.getDB();
 
    if ((await this.getCalendarRowCount(bundleID)) > 0) {
      await execSQLiteQuery(db, `UPDATE 'access' SET
        service='kTCCServiceCalendar',
        client_type=0,
        allowed=1,
        prompt_count=1,
        csreq=0
      WHERE client='?' AND service='kTCCServiceCalendar'`.replace(/\n/g, ' '), bundleID);
    } else {
      await execSQLiteQuery(db, `INSERT INTO 'access' VALUES ('kTCCServiceCalendar', '?', 0, 1, 1, 0, 0);`, bundleID);
    }
  }
 
  async disableCalendarAccess (bundleID) {
    let db = await this.getDB();
 
    if ((await this.getCalendarRowCount(bundleID)) > 0) {
      await execSQLiteQuery(db, `DELETE FROM 'access' WHERE client='?' AND service='kTCCServiceCalendar'`.replace(/\n/g, ' '), bundleID);
    }
  }
}