-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathtest_slack.py
157 lines (122 loc) · 5.55 KB
/
test_slack.py
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
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015-2023 Bitergia
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Authors:
# Alvaro del Castillo <acs@bitergia.com>
# Valerio Cosentino <valcos@bitergia.com>
#
import logging
import unittest
import requests
from base import TestBaseBackend
from grimoire_elk.enriched.utils import REPO_LABELS
class TestSlack(TestBaseBackend):
"""Test Slack backend"""
connector = "slack"
ocean_index = "test_" + connector
enrich_index = "test_" + connector + "_enrich"
def test_has_identites(self):
"""Test value of has_identities method"""
enrich_backend = self.connectors[self.connector][2]()
self.assertTrue(enrich_backend.has_identities())
def test_items_to_raw(self):
"""Test whether JSON items are properly inserted into ES"""
result = self._test_items_to_raw()
self.assertEqual(result['items'], 12)
self.assertEqual(result['raw'], 12)
for item in self.items:
self.ocean_backend._fix_item(item)
self.assertNotIn('previous_names', item['data']['channel_info'])
def test_raw_to_enrich(self):
"""Test whether the raw index is properly enriched"""
result = self._test_raw_to_enrich()
self.assertEqual(result['raw'], 12)
self.assertEqual(result['enrich'], 12)
enrich_backend = self.connectors[self.connector][2]()
item = self.items[0]
eitem = enrich_backend.get_rich_item(item)
self.assertEqual(eitem['channel_member_count'], 3)
self.assertIn(REPO_LABELS, eitem)
for item in self.items[1:]:
eitem = enrich_backend.get_rich_item(item)
self.assertEqual(eitem['channel_member_count'], 4565)
# test to check if files in a message is enriched
item = self.items[9]
eitem = enrich_backend.get_rich_item(item)
self.assertEqual(eitem['number_files'], 2)
self.assertEqual(eitem['message_file_size'], 4588)
def test_enrich_repo_labels(self):
"""Test whether the field REPO_LABELS is present in the enriched items"""
self._test_raw_to_enrich()
enrich_backend = self.connectors[self.connector][2]()
for item in self.items:
eitem = enrich_backend.get_rich_item(item)
self.assertIn(REPO_LABELS, eitem)
def test_raw_to_enrich_sorting_hat(self):
"""Test enrich with SortingHat"""
result = self._test_raw_to_enrich(sortinghat=True)
self.assertEqual(result['raw'], 12)
self.assertEqual(result['enrich'], 12)
enrich_backend = self.connectors[self.connector][2]()
enrich_backend.sortinghat = True
item = self.items[0]
eitem = enrich_backend.get_rich_item(item)
self.assertEqual(eitem['author_name'], 'Unknown')
self.assertEqual(eitem['author_user_name'], 'Unknown')
self.assertEqual(eitem['author_org_name'], 'Unknown')
self.assertEqual(eitem['author_multi_org_names'], ['Unknown'])
item = self.items[1]
eitem = enrich_backend.get_rich_item(item)
self.assertNotIn('author_name', eitem)
self.assertNotIn('author_user_name', eitem)
self.assertNotIn('author_org_name', eitem)
self.assertNotIn('author_multi_org_names', eitem)
self.assertNotIn('author_multi_org_name_0', eitem)
item = self.items[2]
eitem = enrich_backend.get_rich_item(item)
self.assertNotEqual(eitem['author_name'], 'Unknown')
self.assertNotEqual(eitem['author_user_name'], 'Unknown')
self.assertEqual(eitem['author_org_name'], 'Unknown')
self.assertEqual(eitem['author_multi_org_names'], ['Unknown'])
def test_raw_to_enrich_projects(self):
"""Test enrich with Projects"""
result = self._test_raw_to_enrich(projects=True)
self.assertEqual(result['raw'], 12)
self.assertEqual(result['enrich'], 12)
res = requests.get(self.es_con + "/" + self.enrich_index + "/_search", verify=False)
for eitem in res.json()['hits']['hits']:
self.assertEqual(eitem['_source']['project'], "grimoire")
def test_copy_raw_fields(self):
"""Test copied raw fields"""
self._test_raw_to_enrich()
enrich_backend = self.connectors[self.connector][2]()
for item in self.items:
eitem = enrich_backend.get_rich_item(item)
for attribute in enrich_backend.RAW_FIELDS_COPY:
if attribute in item:
self.assertEqual(item[attribute], eitem[attribute])
else:
self.assertIsNone(eitem[attribute])
def test_refresh_identities(self):
"""Test refresh identities"""
result = self._test_refresh_identities()
# ... ?
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
logging.getLogger("urllib3").setLevel(logging.WARNING)
logging.getLogger("requests").setLevel(logging.WARNING)
unittest.main(warnings='ignore')