aboutsummaryrefslogtreecommitdiffstats
path: root/test/servers/helper.js
blob: 260b4cb332cefa5b66cdde13b27de4963ddf4d23 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// taken from mocha
const COLORS = {
  'pass': 90,
  'fail': 31,
  'bright pass': 92,
  'bright fail': 91,
  'bright yellow': 93,
  'pending': 36,
  'suite': 0,
  'error title': 0,
  'error message': 31,
  'error stack': 90,
  'checkmark': 32,
  'fast': 90,
  'medium': 33,
  'slow': 31,
  'green': 32,
  'light': 90,
  'diff gutter': 90,
  'diff added': 42,
  'diff removed': 41
};

function color(type, str) {
  return '\u001b[' + COLORS[type] + 'm' + str + '\u001b[0m';
}

var util = require('util');
var Caldav = require('../../lib/caldav');
var debug = require('debug');

var logs = {
  'normal': debug('caldav:test'),
  'detailed': debug('caldav:test')
};

var Helper = {
  formatObject: function(obj) {
    var format = util.inspect(
      obj,
      false,
      4,
      true
    );

    return format;
  },

  log: function(message, object, level) {
    var log = debug('caldav:test');

    if (!level)
      level = 'normal';

    if (level !== 'normal') {
      log = debug('caldav:test-' + level);
    }

    log(message, Helper.formatObject(object));
  },

  /**
   * Return new caldav connection object.
   * Gets config information from testEnv `serverConfig`.
   *
   * @return {CalDav.Connection} live connection.
   */
  connection: function() {
    var config = testEnv.serverConfig;

    return new Caldav.Connection({
      domain: config.domain,
      user: config.user,
      password: config.password
    });
  },

  findEnvHome: function(done) {
    var config = testEnv.serverConfig;
    var con = Helper.connection();

    var home = new Caldav.Request.CalendarHome(con, {
      url: config.uri
    });

    home.send(function(err, data) {
      if (err) {
        done(err);
        return;
      }
      Helper.log('found home', data);
      testEnv.homeUrl = data.url;
      done();
    });
  }
};

module.exports = Helper;