test_create_resource.py 2.05 KB
Newer Older
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
import google_create_resource
import os
import pytest
import yaml

data_dir = os.path.join(os.path.dirname(__file__) + '/data/')
config = data_dir + 'test_cak_config.yml'


def test_create(capsys):
    args = {
        'name': 'Test Resource',
        'owners': ['user1', 'user2'],
        'resource_type': 'test resource',
        'description': 'a test resource'
    }
    resource = google_create_resource.Resource(
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    resource.create_resource()
    resource.add_resource_owners()
    resource.strip_global_access()
    resource.generate_canned_text()
    captured = capsys.readouterr()
    assert 'Calendar Name: Test Resource' in captured.out


def test_with_bad_owner(caplog):
    args = {
        'name': 'Test Resource',
        'owners': ['user1', 'failuser'],
        'resource_type': 'test resource',
        'description': 'a test resource'
    }
    resource = google_create_resource.Resource(
        args,
        yaml.load(open(config), Loader=yaml.BaseLoader)
    )
    resource.create_resource()
    resource.add_resource_owners()
    errorstr = 'Failed to add failuser as calendar owner'
    assert errorstr in caplog.text


def test_create_duplicate(caplog):
    with pytest.raises(SystemExit):
        args = {
            'name': 'duplicate',
            'owners': ['user1', 'user2'],
            'resource_type': 'test resource',
            'description': 'a test resource'
        }
        resource = google_create_resource.Resource(
            args,
            yaml.load(open(config), Loader=yaml.BaseLoader)
        )
        resource.create_resource()


def test_fail_retrieval(caplog):
    with pytest.raises(SystemExit):
        args = {
            'name': 'missing',
            'owners': ['user1', 'user2'],
            'resource_type': 'test resource',
            'description': 'a test resource'
        }
        resource = google_create_resource.Resource(
            args,
            yaml.load(open(config), Loader=yaml.BaseLoader)
        )
        resource.create_resource()