aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit/test_bitbucket.py
blob: 4c47a6a31289e4b902be5461063d818c8f2bfd77 (plain) (blame)
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
# Copyright (c) 2021 Egor Tensin <egor@tensin.name>
# This file is part of the "cgitize" project.
# For details, see https://github.com/egor-tensin/cgitize
# Distributed under the MIT License.

import os
import unittest

from atlassian.bitbucket.cloud import Cloud
from requests.exceptions import HTTPError


class BitbucketTests(unittest.TestCase):
    def setUp(self):
        self.bitbucket = Cloud(
            username=os.environ.get("CGITIZE_BITBUCKET_EMAIL"),
            password=os.environ.get("CGITIZE_BITBUCKET_TOKEN"),
            cloud=True,
        )

    def test_nonexistent_repo(self):
        with self.assertRaises(HTTPError):
            self.bitbucket.repositories.get("doesnot", "exist")

    def test_public_repo(self):
        r = self.bitbucket.repositories.get("cgitize-test-workspace", "public")
        self.assertEqual(r.name, "public")
        self.assertEqual(r.description, "Public test cgitize repository")

        self.assertEqual(r.data["workspace"]["name"], "Test cgitize workspace")
        self.assertEqual(r.data["workspace"]["slug"], "cgitize-test-workspace")

        self.assertFalse(r.data["is_private"])

        self.assertEqual(
            r.get_link("html"), "https://bitbucket.org/cgitize-test-workspace/public"
        )

        clone_urls = [
            link for link in r.data["links"]["clone"] if link["name"] == "https"
        ]
        self.assertEqual(len(clone_urls), 1)
        self.assertEqual(
            clone_urls[0]["href"],
            "https://cgitize-test@bitbucket.org/cgitize-test-workspace/public.git",
        )

        ssh_urls = [link for link in r.data["links"]["clone"] if link["name"] == "ssh"]
        self.assertEqual(len(ssh_urls), 1)
        self.assertEqual(
            ssh_urls[0]["href"], "git@bitbucket.org:cgitize-test-workspace/public.git"
        )

    def test_private_repo(self):
        r = self.bitbucket.repositories.get("cgitize-test-workspace", "private")
        self.assertEqual(r.name, "private")
        self.assertEqual(r.description, "Private test cgitize repository")

        self.assertEqual(r.data["workspace"]["name"], "Test cgitize workspace")
        self.assertEqual(r.data["workspace"]["slug"], "cgitize-test-workspace")

        self.assertTrue(r.data["is_private"])

        self.assertEqual(
            r.get_link("html"), "https://bitbucket.org/cgitize-test-workspace/private"
        )

        clone_urls = [
            link for link in r.data["links"]["clone"] if link["name"] == "https"
        ]
        self.assertEqual(len(clone_urls), 1)
        self.assertEqual(
            clone_urls[0]["href"],
            "https://cgitize-test@bitbucket.org/cgitize-test-workspace/private.git",
        )

        ssh_urls = [link for link in r.data["links"]["clone"] if link["name"] == "ssh"]
        self.assertEqual(len(ssh_urls), 1)
        self.assertEqual(
            ssh_urls[0]["href"], "git@bitbucket.org:cgitize-test-workspace/private.git"
        )

    def test_user(self):
        w = self.bitbucket.workspaces.get("cgitize-test-workspace")
        self.assertEqual(
            len([r for r in w.repositories.each() if r.name == "public"]), 1
        )
        self.assertEqual(
            len([r for r in w.repositories.each() if r.name == "private"]), 1
        )


class BitbucketTestPrivateRepo(unittest.TestCase):
    def setUp(self):
        self.bitbucket = Cloud(cloud=True)

    def test_private_repo(self):
        with self.assertRaises(HTTPError):
            self.bitbucket.repositories.get("cgitize-test-workspace", "private")