File: test_lp.py

package info (click to toggle)
ppa-dev-tools 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,096 kB
  • sloc: python: 5,069; makefile: 3
file content (181 lines) | stat: -rw-r--r-- 5,777 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
#
# Copyright (C) 2019-2020 Bryce Harrington <bryce@canonical.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# pylint: disable=W0212

"""Launchpad Interface Tests.

Tests for our launchpadlib wrapper & helper methods.
"""

import sys
import os.path
from unittest.mock import Mock, patch

import logging
import pytest
from launchpadlib.launchpad import Launchpad

sys.path.insert(0, os.path.realpath(
    os.path.join(os.path.dirname(os.path.realpath(__file__)), "..")))

from ppa.lp import Lp

APPNAME = 'test-lp'


@pytest.fixture(name='fakelp')
def fixture_fakelp():
    """Connect to Launchpad."""
    mock_launchpad = Mock(spec=Launchpad)

    return Lp(
        application_name=APPNAME,
        service=mock_launchpad)


def test_new_connection():
    """Verifies the Lp object will auto-login properly."""
    mock_launchpad = Mock(spec=Launchpad)

    lp = Lp(
        application_name=APPNAME,
        service=mock_launchpad)

    # Cause the lp._instance() internal property to be triggered
    logging.debug(lp.me)
    logging.debug(lp.people)

    # Verify the triggering resulted in a login attempt
    mock_launchpad.login_with.assert_called_once_with(
        application_name=APPNAME,
        service_root='production',
        allow_access_levels=['WRITE_PRIVATE'],
        version='devel',
    )


@patch("ppa.lp.Credentials")
def test_new_connection_creds(credentials_mock):
    """Verifies the Lp object will login if credentials are provided."""
    mock_launchpad = Mock(spec=Launchpad)

    lp = Lp(
        application_name=APPNAME,
        service=mock_launchpad,
        credentials='...')

    # Cause the lp._instance() internal property to be triggered
    logging.debug(lp.me)
    logging.debug(lp.people)

    # Verify the triggering did not result in a login attempt
    mock_launchpad.login_with.assert_not_called()
    # Rather that the login was done via envvar
    # creating the credentials
    credentials_mock.from_string.assert_called_once()
    # and the service was created with the given credentials
    mock_launchpad.assert_called_once_with(
        credentials_mock.from_string.return_value,
        None,
        None,
        service_root="production",
        version="devel"
    )


def test_api_root(fakelp):
    """Ensures we can get LP's API root."""
    fakelp.load(Lp.API_ROOT_URL + 'hi')
    fakelp._instance.load.assert_called_once_with(
        'https://api.launchpad.net/devel/hi')


def test_ubuntu(fakelp):
    """Checks the .ubuntu property."""
    fakelp._instance.distributions = {'ubuntu': 'UBUNTU'}
    assert fakelp.ubuntu == 'UBUNTU'


def test_ubuntu_active_series(fakelp):
    """Checks the active series names for Ubuntu."""
    mock_hirsute = Mock(active=False)
    mock_hirsute.name = 'hirsute'

    mock_jammy = Mock(active=True)
    mock_jammy.name = 'jammy'

    mock_ubuntu = Mock(series=[mock_hirsute, mock_jammy])
    fakelp._instance.distributions = {'ubuntu': mock_ubuntu}
    assert fakelp.ubuntu_active_series()[0] == mock_jammy
    assert fakelp.ubuntu_active_series()[0].name == 'jammy'


def test_debian(fakelp):
    """Checks the .debian property."""
    fakelp._instance.distributions = {'debian': 'DEBIAN'}
    assert fakelp.debian == 'DEBIAN'


def test_debian_active_series(fakelp):
    """Checks the active series names for Debian."""
    mock_woody = Mock(active=False)
    mock_woody.name = 'woody'

    mock_buster = Mock(active=True)
    mock_buster.name = 'buster'

    mock_sid = Mock(active=False)
    mock_sid.name = 'experimental'

    mock_debian = Mock(series=[mock_woody, mock_buster, mock_sid])
    fakelp._instance.distributions = {'debian': mock_debian}
    assert fakelp.debian_active_series()[0].name == 'buster'


def test_debian_experimental_series(fakelp):
    """Checks the experimental series name for Debian."""
    mock_woody = Mock(active=False)
    mock_woody.name = 'woody'

    mock_buster = Mock(active=True)
    mock_buster.name = 'buster'

    mock_sid = Mock(active=False)
    mock_sid.name = 'experimental'

    mock_debian = Mock(series=[mock_woody, mock_buster, mock_sid])
    fakelp._instance.distributions = {'debian': mock_debian}
    assert fakelp.debian_experimental_series() == mock_sid


def test_get_teams(fakelp):
    """Checks that a person's teams can be retrieved."""
    mock_me = Mock(memberships_details=[
        Mock(name='t1', self_link="https://launchpad.net/~t1"),
        Mock(name='t2', self_link="https://launchpad.net/~t2/+blah"),
        Mock(name='t3', self_link="https://launchpad.net/~t3/+blah"),
    ])
    fakelp._instance.people = {'me': mock_me}
    assert fakelp.get_teams('me') == ['t1', 't2', 't3']