1
# dmedia: distributed media library
2
# Copyright (C) 2012 Novacut Inc
4
# This file is part of `dmedia`.
6
# `dmedia` is free software: you can redistribute it and/or modify it under
7
# the terms of the GNU Affero General Public License as published by the Free
8
# Software Foundation, either version 3 of the License, or (at your option) any
11
# `dmedia` is distributed in the hope that it will be useful, but WITHOUT ANY
12
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
16
# You should have received a copy of the GNU Affero General Public License along
17
# with `dmedia`. If not, see <http://www.gnu.org/licenses/>.
20
# Jason Gerard DeRose <jderose@novacut.com>
23
Unit tests for `dmedia.startup`.
26
from unittest import TestCase
30
from copy import deepcopy
33
from microfiber import random_id
35
from .base import TempDir
36
from dmedia.peering import PKI
37
from dmedia import startup
40
class TestFunctions(TestCase):
41
def test_load_config(self):
43
filename = tmp.join('foo.json')
45
with self.assertRaises(IOError) as cm:
46
startup.load_config(filename)
48
config = {'junk': random_id()}
49
json.dump(config, open(filename, 'w'))
50
self.assertEqual(startup.load_config(filename), config)
52
open(filename, 'w').write('bad json, not treat for you')
53
with self.assertRaises(ValueError) as cm:
54
startup.load_config(filename)
56
def test_save_config(self):
58
config = {'stuff': random_id()}
59
filename = tmp.join('foo.json')
61
with self.assertRaises(TypeError) as cm:
62
startup.save_config(filename, object)
63
self.assertFalse(path.exists(filename))
64
self.assertTrue(path.isfile(filename + '.tmp'))
66
startup.save_config(filename, config)
67
self.assertTrue(path.isfile(filename))
68
self.assertFalse(path.exists(filename + '.tmp'))
69
self.assertEqual(json.load(open(filename, 'r')), config)
71
def test_get_usercouch(self):
73
couch = startup.get_usercouch(tmp.dir)
74
self.assertIsInstance(couch, usercouch.UserCouch)
75
self.assertEqual(couch.basedir, tmp.dir)
76
self.assertIsNone(couch.couchdb)
77
self.assertIsInstance(couch.pki, PKI)
78
self.assertIs(couch.pki.ssldir, couch.paths.ssl)
81
with self.assertRaises(usercouch.LockError) as cm:
82
couch2 = startup.get_usercouch(tmp.dir)
83
self.assertEqual(cm.exception.lockfile, tmp.join('lockfile'))
85
def test_machine_filename(self):
87
couch = startup.get_usercouch(tmp.dir)
89
startup.machine_filename(couch),
90
path.join(tmp.dir, 'machine.json')
93
def test_user_filename(self):
95
couch = startup.get_usercouch(tmp.dir)
97
startup.user_filename(couch),
98
path.join(tmp.dir, 'user.json')
101
def test_has_machine(self):
103
couch = startup.get_usercouch(tmp.dir)
104
self.assertFalse(startup.has_machine(couch))
105
tmp.touch('machine.json')
106
self.assertTrue(startup.has_machine(couch))
108
def test_has_user(self):
110
couch = startup.get_usercouch(tmp.dir)
111
self.assertFalse(startup.has_user(couch))
112
tmp.touch('user.json')
113
self.assertTrue(startup.has_user(couch))
115
def test_init_machine(self):
117
couch = startup.get_usercouch(tmp.dir)
118
self.assertIsNone(startup.init_machine(couch))
119
doc = json.load(open(tmp.join('machine.json'), 'r'))
120
self.assertIsInstance(doc, dict)
122
def test_init_user(self):
124
couch = startup.get_usercouch(tmp.dir)
125
machine_id = random_id(25)
126
self.assertIsNone(startup.init_user(couch, machine_id))
127
doc = json.load(open(tmp.join('user.json'), 'r'))
128
self.assertIsInstance(doc, dict)
130
def test_bootstrap_args(self):
132
couch = startup.get_usercouch(tmp.dir)
133
machine_id = random_id(25)
134
user_id = random_id(25)
135
cert_id = random_id(25)
136
oauth = usercouch.random_oauth()
139
'oauth': deepcopy(oauth),
140
'certs': {machine_id: cert_id},
142
(auth, config) = startup.bootstrap_args(couch, machine_id, user)
143
self.assertEqual(auth, 'oauth')
144
self.assertEqual(config,
147
'bind_address': '0.0.0.0',
150
'key_file': tmp.join('ssl', cert_id + '.key'),
151
'cert_file': tmp.join('ssl', cert_id + '.cert'),
154
'ca_file': tmp.join('ssl', user_id + '.cert'),
159
startup.bootstrap_args(couch, machine_id, None),
160
('basic', {'username': 'admin'})