File: test_auth.py

package info (click to toggle)
python-dcos 0.2.0-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,440 kB
  • sloc: python: 8,196; sh: 194; makefile: 36
file content (57 lines) | stat: -rw-r--r-- 1,623 bytes parent folder | download | duplicates (4)
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
import os
import webbrowser

from dcos import auth, constants, util
from dcoscli.main import main

from mock import Mock, patch


def test_no_browser_auth():
    webbrowser.get = Mock(side_effect=webbrowser.Error())
    with patch('webbrowser.open') as op:
        _mock_dcos_run([util.which('dcos')], False)
        assert op.call_count == 0


def test_when_authenticated():
    with patch('dcos.auth.force_auth'):

        _mock_dcos_run([util.which('dcos')], True)
        assert auth.force_auth.call_count == 0


def test_anonymous_login():
    with patch('sys.stdin.readline', return_value='\n'), \
            patch('uuid.uuid1', return_value='anonymous@email'):

        assert _mock_dcos_run([util.which('dcos'),
                               'help'], False) == 0
        assert _mock_dcos_run([util.which('dcos'), 'config',
                               'show', 'core.email'], False) == 0
        assert _mock_dcos_run([util.which('dcos'), 'config',
                               'unset', 'core.email'], False) == 0


def _mock_dcos_run(args, authenticated=True):
    if authenticated:
        env = _config_with_credentials()
    else:
        env = _config_without_credentials()

    with patch('sys.argv', args), patch.dict(os.environ, env):
        return main()


def _config_with_credentials():
    return {
        constants.DCOS_CONFIG_ENV: os.path.join(
            'tests', 'data', 'auth', 'dcos_with_credentials.toml')
    }


def _config_without_credentials():
    return {
        constants.DCOS_CONFIG_ENV: os.path.join(
            'tests', 'data', 'auth', 'dcos_without_credentials.toml')
    }