Ejemplo n.º 1
0
def test_docker_login_with_credsstore(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser',
                        lambda x: x.replace('~', str(tmpdir)))
    monkeypatch.setattr('pierone.api.get_token',
                        MagicMock(return_value='12377'))
    path = os.path.expanduser('~/.docker/config.json')
    os.makedirs(os.path.dirname(path))
    with open(path, 'w') as fd:
        json.dump(
            {
                "auths": {
                    "https://pierone.stups.zalan.do": {
                        "auth": "xxx",
                        "email": "*****@*****.**"
                    }
                },
                "credsStore": "osxkeychain"
            }, fd)
    docker_login('https://pierone.example.org',
                 'services',
                 'mytok',
                 'myuser',
                 'mypass',
                 'https://token.example.org',
                 use_keyring=False)
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {
            'auth': 'b2F1dGgyOjEyMzc3',
            'email': '*****@*****.**'
        } == data.get('auths').get('https://pierone.example.org')
        assert 'credsStore' not in data
Ejemplo n.º 2
0
def test_keep_dockercfg_entries(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {'access_token': '12377'}
    monkeypatch.setattr('requests.get', MagicMock(return_value=response))
    path = os.path.expanduser('~/.docker/config.json')

    key = 'https://old.example.org'
    existing_data = {
        key: {
            'auth': 'abc123',
            'email': '*****@*****.**'
        }
    }
    os.makedirs(os.path.dirname(path))
    with open(path, 'w') as fd:
        json.dump(existing_data, fd)

    docker_login('https://pierone.example.org', 'services', 'mytok',
                 'myuser', 'mypass', 'https://token.example.org', use_keyring=False)
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {'auth': 'b2F1dGgyOjEyMzc3',
                'email': '*****@*****.**'} == data.get('auths', {}).get('https://pierone.example.org')
        assert existing_data.get(key) == data.get(key)
Ejemplo n.º 3
0
def test_keep_dockercfg_entries(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser',
                        lambda x: x.replace('~', str(tmpdir)))
    monkeypatch.setattr('pierone.api.get_token',
                        MagicMock(return_value='12377'))
    path = os.path.expanduser('~/.docker/config.json')

    key = 'https://old.example.org'
    existing_data = {key: {'auth': 'abc123', 'email': '*****@*****.**'}}
    os.makedirs(os.path.dirname(path))
    with open(path, 'w') as fd:
        json.dump(existing_data, fd)

    docker_login('https://pierone.example.org',
                 'services',
                 'mytok',
                 'myuser',
                 'mypass',
                 'https://token.example.org',
                 use_keyring=False)
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {
            'auth': 'b2F1dGgyOjEyMzc3',
            'email': '*****@*****.**'
        } == data.get('auths', {}).get('https://pierone.example.org')
        assert existing_data.get(key) == data.get(key)
Ejemplo n.º 4
0
def test_docker_login_service_token(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    monkeypatch.setattr('tokens.get', lambda x: '12377')
    docker_login('https://pierone.example.org', None, 'mytok', 'myuser', 'mypass', 'https://token.example.org')
    path = os.path.expanduser('~/.docker/config.json')
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {'auth': 'b2F1dGgyOjEyMzc3', 'email': '*****@*****.**'} == data.get('auths').get('https://pierone.example.org')
Ejemplo n.º 5
0
def test_docker_login_service_token(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    monkeypatch.setattr('tokens.get', lambda x: '12377')
    docker_login('https://pierone.example.org', None, 'mytok', 'myuser', 'mypass', 'https://token.example.org')
    path = os.path.expanduser('~/.docker/config.json')
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {'auth': 'b2F1dGgyOjEyMzc3',
                'email': '*****@*****.**'} == data.get('auths').get('https://pierone.example.org')
Ejemplo n.º 6
0
def test_docker_login(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    monkeypatch.setattr('pierone.api.get_token', MagicMock(return_value='12377'))
    docker_login('https://pierone.example.org', 'services', 'mytok',
                 'myuser', 'mypass', 'https://token.example.org', use_keyring=False)
    path = os.path.expanduser('~/.docker/config.json')
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {'auth': 'b2F1dGgyOjEyMzc3',
                'email': '*****@*****.**'} == data.get('auths').get('https://pierone.example.org')
Ejemplo n.º 7
0
def test_docker_login(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    monkeypatch.setattr('pierone.api.get_token', MagicMock(return_value='12377'))
    docker_login('https://pierone.example.org', 'services', 'mytok',
                 'myuser', 'mypass', 'https://token.example.org', use_keyring=False)
    path = os.path.expanduser('~/.docker/config.json')
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {'auth': 'b2F1dGgyOjEyMzc3',
                'email': '*****@*****.**'} == data.get('auths').get('https://pierone.example.org')
        assert "" == data.get('credHelpers', {}).get('pierone.example.org')
Ejemplo n.º 8
0
def test_docker_login(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {'access_token': '12377'}
    monkeypatch.setattr('requests.get', MagicMock(return_value=response))
    docker_login('https://pierone.example.org', 'services', 'mytok',
                 'myuser', 'mypass', 'https://token.example.org', use_keyring=False)
    path = os.path.expanduser('~/.docker/config.json')
    with open(path) as fd:
        data = yaml.safe_load(fd)
        assert {'auth': 'b2F1dGgyOjEyMzc3', 'email': '*****@*****.**'} == data.get('auths').get('https://pierone.example.org')
Ejemplo n.º 9
0
def test_docker_login_error(monkeypatch, status_code):
    mock_get = MagicMock()
    response = MagicMock()
    response.status_code = status_code
    mock_get.side_effect = requests.exceptions.HTTPError(response=response)
    monkeypatch.setattr('tokens.get', mock_get)

    mock_action = MagicMock()
    mock_action.side_effect = SystemExit(1)
    monkeypatch.setattr('pierone.api.Action.fatal_error', mock_action)
    with pytest.raises(SystemExit):
        docker_login('https://pierone.example.org', None, 'mytok', 'myuser', 'mypass', 'https://token.example.org')
    mock_action.assert_called_once_with(ANY)
    call = mock_action.call_args[0]
    argument = call[0]  # type: str
    assert argument.startswith("Authentication Failed")
    assert str(status_code) in argument
    if 400 <= status_code < 500:
        assert "Client Error" in argument
    if 500 <= status_code < 600:
        assert "Server Error" in argument
Ejemplo n.º 10
0
def test_docker_login_error(monkeypatch, status_code):
    mock_get = MagicMock()
    response = MagicMock()
    response.status_code = status_code
    mock_get.side_effect = requests.exceptions.HTTPError(response=response)
    monkeypatch.setattr('tokens.get', mock_get)

    mock_action = MagicMock()
    mock_action.side_effect = SystemExit(1)
    monkeypatch.setattr('pierone.api.Action.fatal_error', mock_action)
    with pytest.raises(SystemExit):
        docker_login('https://pierone.example.org', None, 'mytok', 'myuser',
                     'mypass', 'https://token.example.org')
    mock_action.assert_called_once_with(ANY)
    call = mock_action.call_args[0]
    argument = call[0]  # type: str
    assert argument.startswith("Authentication Failed")
    assert str(status_code) in argument
    if 400 <= status_code < 500:
        assert "Client Error" in argument
    if 500 <= status_code < 600:
        assert "Server Error" in argument
Ejemplo n.º 11
0
def test_docker_login(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {'access_token': '12377'}
    monkeypatch.setattr('requests.get', MagicMock(return_value=response))
    token = docker_login('https://pierone.example.org', 'services', 'mytok',
                         'myuser', 'mypass', 'https://token.example.org', use_keyring=False)
    path = os.path.expanduser('~/.dockercfg')
    with open(path) as fd:
        data = yaml.safe_load(fd)
    assert {'auth': 'b2F1dGgyOjEyMzc3', 'email': '*****@*****.**'} == data.get('https://pierone.example.org')

    path = os.path.expanduser('~/.docker/config.json')
    with open(path) as fd:
        data = yaml.safe_load(fd)
    assert {'auth': 'b2F1dGgyOjEyMzc3', 'email': '*****@*****.**'} == data.get('auths').get('https://pierone.example.org')
Ejemplo n.º 12
0
def test_keep_dockercfg_entries(monkeypatch, tmpdir):
    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
    response = MagicMock()
    response.status_code = 200
    response.json.return_value = {'access_token': '12377'}
    monkeypatch.setattr('requests.get', MagicMock(return_value=response))
    path = os.path.expanduser('~/.dockercfg')

    key = 'https://old.example.org'
    existing_data = {
        key: {
            'auth': 'abc123',
            'email': '*****@*****.**'
        }
    }
    with open(path, 'w') as fd:
        json.dump(existing_data, fd)

    token = docker_login('https://pierone.example.org', 'services', 'mytok',
                         'myuser', 'mypass', 'https://token.example.org', use_keyring=False)
    with open(path) as fd:
        data = yaml.safe_load(fd)
    assert {'auth': 'b2F1dGgyOjEyMzc3', 'email': '*****@*****.**'} == data.get('https://pierone.example.org')
    assert existing_data.get(key) == data.get(key)