Commit | Line | Data |
---|---|---|
f719fe20 | 1 | import datetime |
31329c3f TP |
2 | import os |
3 | import unittest | |
4 | import flask_migrate | |
5 | import flask_testing | |
6 | ||
7 | from ui import config | |
8 | from ui.app import app | |
9 | from ui.models import Build, Runner, db | |
10 | ||
11 | class UsesApp(flask_testing.TestCase): | |
12 | def create_app(self): | |
13 | return app | |
14 | ||
15 | class UsesModels(UsesApp): | |
16 | def setUp(self): | |
17 | flask_migrate.upgrade(revision="head") | |
18 | ||
19 | def tearDown(self): | |
20 | flask_migrate.downgrade(revision="base") | |
21 | ||
22 | class TestModels(UsesModels): | |
23 | ||
24 | def test_build(self): | |
25 | build = Build(build_id=1) | |
26 | db.session.add(build) | |
27 | db.session.commit() | |
28 | ||
29 | assert build in db.session | |
30 | ||
31 | def test_get_or_create_build_by_id(self): | |
32 | build = Build(build_id=1) | |
33 | db.session.add(build) | |
34 | db.session.commit() | |
35 | ||
36 | assert Build.get_or_create_by_id(1).build_id == 1 | |
37 | assert Build.get_or_create_by_id(2).build_id == 2 | |
38 | ||
39 | def test_runner(self): | |
40 | runner = Runner(runner_id="foobar") | |
41 | db.session.add(runner) | |
42 | db.session.commit() | |
43 | ||
44 | assert runner in db.session | |
45 | ||
46 | def test_get_or_create_runner_by_id(self): | |
47 | runner = Runner(runner_id="foobar", runner_name="foobar") | |
48 | db.session.add(runner) | |
49 | db.session.commit() | |
50 | ||
51 | assert Runner.get_or_create_by_id('foobar').runner_name == "foobar" | |
52 | assert Runner.get_or_create_by_id("foobaz").runner_name == None | |
53 | ||
54 | def test_runner_build_rel(self): | |
55 | runner = Runner(runner_id="foobar", runner_name="foobar") | |
56 | build = Build(build_id=1, build_runner=runner) | |
57 | db.session.add(runner) | |
58 | db.session.add(build) | |
59 | db.session.commit() | |
60 | ||
61 | assert build.build_runner == runner | |
62 | assert build in runner.builds | |
63 | ||
64 | class TestWebhooks(UsesModels): | |
65 | ||
66 | def test_invalid_token(self): | |
67 | with open("tests/pipeline_webhook.json", "r") as f: | |
68 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'invalidsecret'}, data=f.read(), content_type='application/json') | |
69 | assert response.status_code == 403 | |
70 | ||
71 | def test_pipeline_webhook(self): | |
72 | with open("tests/pipeline_webhook.json", "r") as f: | |
73 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') | |
74 | assert response.status_code == 200 | |
75 | build = Build.query.filter_by(build_id=1).first() | |
76 | assert build.build_id == 1 | |
6907b2b7 TP |
77 | assert build.build_runner_id == None |
78 | ||
79 | def test_success(self): | |
80 | with open("tests/pipeline_success.json") as f: | |
81 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') | |
82 | assert response.status_code == 200 | |
83 | runner = Runner.query.filter_by(runner_id="530365").first() | |
84 | build = Build.query.filter_by(build_id=34618162).first() | |
85 | assert build.build_runner.runner_name == "phenom-test" | |
86 | assert build.build_version == "lineage-15.1" | |
87 | assert build.build_device == "FAKE_TEST" | |
88 | print(build.build_status) | |
89 | assert build.build_status == "success" | |
90 | assert build.build_duration == 81 | |
91 | ||
92 | ||
93 | def test_start(self): | |
94 | with open("tests/pipeline_start.json") as f: | |
95 | response = self.client.post("/webhook", headers={'X-Gitlab-Event': 'Pipeline Hook', 'X-Gitlab-Token': 'secret'}, data=f.read(), content_type='application/json') | |
96 | assert response.status_code == 200 | |
97 | ||
31329c3f | 98 | |
f719fe20 TP |
99 | class TestWeb(UsesModels): |
100 | ||
101 | def test_home(self): | |
102 | build1 = Build(build_id=1) | |
103 | runner = Runner(runner_name="foobar", runner_id="foobar") | |
104 | build2 = Build(build_id=2, build_status="success", build_device="mako", build_version="cm-14.1", build_type="userdebug", build_date=datetime.datetime.now(), build_runner=runner) | |
105 | db.session.add(build1) | |
106 | db.session.add(runner) | |
107 | db.session.add(build2) | |
108 | db.session.commit() | |
109 | response = self.client.get("/") | |
110 | assert response.status_code == 200 | |
111 | ||
31329c3f TP |
112 | if __name__ == "__main__": |
113 | unittest.main() |