Completed
Pull Request — master (#29)
by Gonzalo
53s
created

test_valid_token()   A

Complexity

Conditions 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
dl 0
loc 4
rs 10
1
# -*- coding: utf-8 -*-
2
# -----------------------------------------------------------------------------
3
# Copyright (c) The Spyder Development Team
4
#
5
# Licensed under the terms of the MIT License
6
# (See LICENSE.txt for details)
7
# -----------------------------------------------------------------------------
8
"""Tests github repo."""
9
10
# Standard library imports
11
import os
12
13
# Third party imports
14
import pytest
15
16
# Local imports
17
from loghub.main import GitHubRepo
18
from loghub.external.github import ApiError
19
20
21
REPO = 'spyder-ide/loghub'
22
TEST_TOKEN = os.environ.get('TEST_TOKEN', '').replace('x', '')
23
TEST_USER = os.environ.get('TEST_USER', '').replace('x', '')
24
TEST_PASS = os.environ.get('TEST_CODE', '').replace('x', '')
25
TEST_MILESTONE = 'test-milestone'
26
TEST_TAG = 'v0.1.2'
27
NOT_ON_CI = not bool(TEST_TOKEN)
28
29
30
# --- Fixtures
31
# -----------------------------------------------------------------------------
32
@pytest.fixture
33
def gh_repo():
34
    return GitHubRepo(token=TEST_TOKEN, repo=REPO)
35
36
37
# --- Tests
38
# -----------------------------------------------------------------------------
39
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
40
def test_valid_user_password():
41
    gh = GitHubRepo(username=TEST_USER, password='invalid-password',
42
                    repo=REPO)
43
    with pytest.raises(ApiError):
44
        gh.milestone(TEST_MILESTONE)
45
46
47
def test_invalid_user_password():
48
    gh = GitHubRepo(username=TEST_USER, password='invalid-password',
49
                    repo=REPO)
50
    with pytest.raises(ApiError):
51
        gh.milestone(TEST_MILESTONE)
52
53
54
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
55
def test_valid_token():
56
    gh = GitHubRepo(token=TEST_TOKEN, repo=REPO)
57
    gh.milestone(TEST_MILESTONE)
58
59
60
def test_invalid_token():
61
    gh = GitHubRepo(token='this-is-an-invalid-token', repo=REPO)
62
    with pytest.raises(ApiError):
63
        gh.milestone(TEST_MILESTONE)
64
65
66
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
67
def test_tags(gh_repo):
68
    tags = gh_repo.tags()
69
    titles = [tag['ref'].replace('refs/tags/', '') for tag in tags]
70
    assert TEST_TAG in titles
71
72
73
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
74
def test_valid_tag(gh_repo):
75
    tag = gh_repo.tag(TEST_TAG)
76
    assert bool(tag)
77
78
79
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
80
def test_invalid_tag(gh_repo):
81
    with pytest.raises(SystemExit):
82
        gh_repo.tag('invalid-tag')
83
84
85
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
86
def test_milestones(gh_repo):
87
    milestones = gh_repo.milestones()
88
    titles = [milestone['title'] for milestone in milestones]
89
    assert TEST_MILESTONE in titles
90
91
92
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
93
def test_valid_milestone(gh_repo):
94
    assert bool(gh_repo.milestone(TEST_MILESTONE))
95
96
97
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
98
def test_invalid_milestone(gh_repo):
99
    with pytest.raises(SystemExit):
100
        gh_repo.milestone('invalid-milestone')
101
102
103
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
104
def test_issues(gh_repo):
105
    milestone_data = gh_repo.milestone(TEST_MILESTONE)
106
    print(milestone_data)
107
    milestone_number = milestone_data['number']
108
    issues = gh_repo.issues(milestone=milestone_number, state='closed')
109
    assert len(issues) == 3
110
111
112
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
113
def test_pr_merged(gh_repo):
114
    assert gh_repo.is_merged(25)
115
116
117
@pytest.mark.skipif(NOT_ON_CI, reason='test on ci server only')
118
def test_pr_closed(gh_repo):
119
    assert not gh_repo.is_merged(22)
120
121
122
def test_dates():
123
    date = GitHubRepo.str_to_date('2016-10-10T08:08:08Z')
124
    assert date.year == 2016
125
    assert date.month == 10
126
    assert date.day == 10
127
    assert date.hour == 8
128
    assert date.minute == 8
129
    assert date.second == 8
130