예제 #1
0
 def test_no_change(self, request):
     """Test that no new report is inserted if the parameter value is unchanged."""
     self.sources[SOURCE_ID]["parameters"]["url"] = self.url
     request.json = dict(url=self.url)
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assertEqual(dict(ok=True), response)
     self.database.reports.insert.assert_not_called()
예제 #2
0
 def test_mass_edit_metric(self, request):
     """Test that a source parameter can be mass edited."""
     request.json = dict(username=self.NEW_VALUE, edit_scope="metric")
     response = post_source_parameter(SOURCE_ID, "username", self.database)
     self.assertEqual(dict(ok=True), response)
     self.database.reports.insert.assert_called_once_with(self.report)
     self.assert_value({
         self.NEW_VALUE:
         [self.sources[SOURCE_ID], self.sources[SOURCE_ID2]],
         self.OLD_VALUE: [
             self.sources[SOURCE_ID4], self.sources2[SOURCE_ID5],
             self.sources3[SOURCE_ID6]
         ],
         self.UNCHANGED_VALUE: [self.sources[SOURCE_ID3]]
     })
     self.assertEqual(
         dict(
             uuids=[
                 REPORT_ID, SUBJECT_ID, METRIC_ID, SOURCE_ID, SOURCE_ID2
             ],
             email=self.email,
             description=
             "Jenny changed the username of all sources of type 'Source type' with username 'username' "
             "of metric 'Metric' of subject 'Subject' in report 'Report' from 'username' to "
             "'new username'."), self.report["delta"])
예제 #3
0
 def test_mass_edit_reports(self, request):
     """Test that a source parameter can be mass edited."""
     request.json = dict(username=self.NEW_VALUE, edit_scope="reports")
     response = post_source_parameter(SOURCE_ID, "username", self.database)
     self.assertEqual(dict(ok=True), response)
     self.database.reports.insert_many.assert_called_once_with(
         [self.report, self.report2], ordered=False)
     self.assert_value({
         self.NEW_VALUE: [
             self.sources[SOURCE_ID],
             self.sources[SOURCE_ID2],
             self.sources2[SOURCE_ID5],
             self.sources3[SOURCE_ID6],
             self.sources4[SOURCE_ID7],
         ],
         self.OLD_VALUE: [self.sources[SOURCE_ID4]],
         self.UNCHANGED_VALUE: [self.sources[SOURCE_ID3]],
     })
     extra_uuids = [
         METRIC_ID2, SOURCE_ID5, SUBJECT_ID2, METRIC_ID3, SOURCE_ID6
     ]
     extra_uuids.extend([REPORT_ID2, SUBJECT_ID3, METRIC_ID4, SOURCE_ID7])
     self.assert_delta("in all reports from 'username' to 'new username'",
                       extra_uuids)
     self.assert_delta("in all reports from 'username' to 'new username'",
                       extra_uuids, self.report2)
예제 #4
0
 def test_empty_url(self, request):
     """Test that the source url availability is not checked when the url is empty."""
     self.sources[SOURCE_ID]["parameters"]["url"] = self.url
     request.json = dict(url="")
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assertEqual(response, dict(ok=True))
     self.database.reports.insert.assert_called_once_with(self.report)
예제 #5
0
 def test_password(self, request):
     """Test that the password can be changed and is not logged."""
     request.json = dict(url="unimportant", password="******")
     response = post_source_parameter(SOURCE_ID, "password", self.database)
     self.assertEqual(response, dict(ok=True))
     self.database.reports.insert.assert_called_once_with(self.report)
     self.assert_delta(
         "password of source 'Source' of metric 'Metric' of subject 'Subject' in report 'Report' from '' to '******'"
     )
예제 #6
0
 def test_obsolete_multiple_choice_value(self, request):
     """Test that obsolete multiple choice values are removed."""
     parameters = self.report["subjects"][SUBJECT_ID]["metrics"][METRIC_ID][
         "sources"][SOURCE_ID]["parameters"]
     self.assertEqual(["D"], parameters["choices"])
     request.json = dict(choices=["A", "D"])
     response = post_source_parameter(SOURCE_ID, "choices", self.database)
     self.assertEqual(response, dict(ok=True))
     self.assertEqual(["A"], parameters["choices"])
     self.database.reports.insert.assert_called_once_with(self.report)
예제 #7
0
 def test_urls_connection_on_update_other_field(self, mock_get, request):
     """Test that the all urls availability is checked when a parameter that it depends on is changed."""
     self.data_model["sources"]["source_type"]["parameters"]["url"][
         "validate_on"] = "password"
     mock_get.return_value = self.url_check_get_response
     request.json = dict(password="******")
     self.sources[SOURCE_ID]["parameters"]["url"] = self.url
     response = post_source_parameter(SOURCE_ID, "password", self.database)
     self.assert_url_check(response)
     self.database.reports.insert.assert_called_once_with(self.report)
예제 #8
0
 def test_url_no_url_type(self, mock_get, request):
     """Test that the source url can be changed and that the availability is not checked if it's not a url type."""
     self.data_model["sources"]["source_type"]["parameters"]["url"][
         "type"] = "string"
     mock_get.return_value = self.url_check_get_response
     request.json = dict(url="unimportant")
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assertEqual(response, dict(ok=True))
     self.database.reports.insert.assert_called_once_with(self.report)
     mock_get.assert_not_called()
예제 #9
0
 def test_url_http_error(self, mock_get, request):
     """Test that the error is reported if a request exception occurs, while checking connection of a url."""
     mock_get.side_effect = requests.exceptions.RequestException
     request.json = dict(url=self.url)
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assert_url_check(response, -1, "Unknown error")
     self.database.reports.insert.assert_called_once_with(self.report)
     self.assert_delta(
         f"url of source 'Source' of metric 'Metric' of subject 'Subject' in report 'Report' from '' to '{self.url}'"
     )
예제 #10
0
 def test_url_with_token(self, mock_get, request):
     """Test that the source url can be changed and that the availability is checked."""
     mock_get.return_value = self.url_check_get_response
     request.json = dict(url=self.url)
     self.sources[SOURCE_ID]["parameters"]["private_token"] = "xxx"
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assert_url_check(response)
     self.database.reports.insert.assert_called_once_with(self.report)
     mock_get.assert_called_once_with(self.url,
                                      auth=("xxx", ""),
                                      headers={"Private-Token": "xxx"},
                                      verify=False)
예제 #11
0
 def test_url_with_user(self, mock_get, request):
     """Test that the source url can be changed and that the availability is checked."""
     self.sources[SOURCE_ID]["parameters"]["username"] = "******"
     self.sources[SOURCE_ID]["parameters"]["password"] = "******"
     mock_get.return_value = self.url_check_get_response
     request.json = dict(url=self.url)
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assert_url_check(response)
     self.database.reports.insert.assert_called_once_with(self.report)
     mock_get.assert_called_once_with(self.url,
                                      auth=("un", "pwd"),
                                      headers={},
                                      verify=False)
예제 #12
0
 def test_password(self, request):
     """Test that the password can be changed and is not logged."""
     request.json = dict(url="unimportant", password="******")
     response = post_source_parameter(SOURCE_ID, "password", self.database)
     self.assertEqual(response, dict(ok=True))
     self.database.reports.insert.assert_called_once_with(self.report)
     self.assertEqual(
         dict(
             uuids=[REPORT_ID, SUBJECT_ID, METRIC_ID, SOURCE_ID],
             email=self.email,
             description=
             "Jenny changed the password of source 'Source' of metric 'Metric' of subject 'Subject' in "
             "report 'Report' from '' to '******'."), self.report["delta"])
예제 #13
0
 def test_url(self, mock_get, request):
     """Test that the source url can be changed and that the availability is checked."""
     mock_get.return_value = self.url_check_get_response
     request.json = dict(url=self.url)
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assert_url_check(response)
     self.database.reports.insert.assert_called_once_with(self.report)
     mock_get.assert_called_once_with(self.url,
                                      auth=None,
                                      headers={},
                                      verify=False)
     self.assert_delta(
         f"url of source 'Source' of metric 'Metric' of subject 'Subject' in report 'Report' from '' to '{self.url}'"
     )
예제 #14
0
 def test_url_http_error(self, mock_get, request):
     """Test that the error is reported if a request exception occurs, while checking connection of a url."""
     mock_get.side_effect = requests.exceptions.RequestException
     request.json = dict(url=self.url)
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assert_url_check(response, -1, "Unknown error")
     self.database.reports.insert.assert_called_once_with(self.report)
     self.assertEqual(
         dict(
             uuids=[REPORT_ID, SUBJECT_ID, METRIC_ID, SOURCE_ID],
             email=self.email,
             description=
             "Jenny changed the url of source 'Source' of metric 'Metric' of subject 'Subject' in "
             f"report 'Report' from '' to '{self.url}'."),
         self.report["delta"])
예제 #15
0
    def test_regexp_with_curly_braces(self, request):
        """Test that regular expressions with curly braces work.

        Curly braces shouldn't be interpreted as string formatting fields.
        """
        parameters = self.report["subjects"][SUBJECT_ID]["metrics"][METRIC_ID][
            "sources"][SOURCE_ID]["parameters"]
        request.json = dict(
            choices_with_addition=[r"[\w]{3}-[\w]{3}-[\w]{4}-[\w]{3}\/"])
        response = post_source_parameter(SOURCE_ID, "choices_with_addition",
                                         self.database)
        self.assertEqual(response, dict(ok=True))
        self.assertEqual([r"[\w]{3}-[\w]{3}-[\w]{4}-[\w]{3}\/"],
                         parameters["choices_with_addition"])
        self.database.reports.insert.assert_called_once_with(self.report)
        self.assert_delta(
            "choices_with_addition of source 'Source' of metric 'Metric' of subject 'Subject' in report 'Report' "
            r"from '' to '['[\\w]{3}-[\\w]{3}-[\\w]{4}-[\\w]{3}\\/']'")
예제 #16
0
 def test_mass_edit_metric(self, request):
     """Test that a source parameter can be mass edited."""
     request.json = dict(username=self.NEW_VALUE, edit_scope="metric")
     response = post_source_parameter(SOURCE_ID, "username", self.database)
     self.assertEqual(dict(ok=True), response)
     self.database.reports.insert.assert_called_once_with(self.report)
     self.assert_value({
         self.NEW_VALUE:
         [self.sources[SOURCE_ID], self.sources[SOURCE_ID2]],
         self.OLD_VALUE: [
             self.sources[SOURCE_ID4], self.sources2[SOURCE_ID5],
             self.sources3[SOURCE_ID6]
         ],
         self.UNCHANGED_VALUE: [self.sources[SOURCE_ID3]],
     })
     self.assert_delta(
         "of metric 'Metric' of subject 'Subject' in report 'Report' from 'username' to 'new username'"
     )
예제 #17
0
 def test_url(self, mock_get, request):
     """Test that the source url can be changed and that the availability is checked."""
     mock_get.return_value = self.url_check_get_response
     request.json = dict(url=self.url)
     response = post_source_parameter(SOURCE_ID, "url", self.database)
     self.assert_url_check(response)
     self.database.reports.insert.assert_called_once_with(self.report)
     mock_get.assert_called_once_with(self.url,
                                      auth=None,
                                      headers={},
                                      verify=False)
     self.assertEqual(
         dict(
             uuids=[REPORT_ID, SUBJECT_ID, METRIC_ID, SOURCE_ID],
             email=self.email,
             description=
             "Jenny changed the url of source 'Source' of metric 'Metric' of subject 'Subject' in "
             f"report 'Report' from '' to '{self.url}'."),
         self.report["delta"])
예제 #18
0
 def test_mass_edit_reports(self, request):
     """Test that a source parameter can be mass edited."""
     request.json = dict(username=self.NEW_VALUE, edit_scope="reports")
     response = post_source_parameter(SOURCE_ID, "username", self.database)
     self.assertEqual(dict(ok=True), response)
     self.database.reports.insert_many.assert_called_once_with(
         (self.report, self.report2), ordered=False)
     self.assert_value({
         self.NEW_VALUE: [
             self.sources[SOURCE_ID], self.sources[SOURCE_ID2],
             self.sources2[SOURCE_ID5], self.sources3[SOURCE_ID6],
             self.sources4[SOURCE_ID7]
         ],
         self.OLD_VALUE: [self.sources[SOURCE_ID4]],
         self.UNCHANGED_VALUE: [self.sources[SOURCE_ID3]]
     })
     self.assertEqual(
         dict(
             uuids=[
                 REPORT_ID, SUBJECT_ID, METRIC_ID, SOURCE_ID, SOURCE_ID2,
                 METRIC_ID2, SOURCE_ID5, SUBJECT_ID2, METRIC_ID3,
                 SOURCE_ID6, REPORT_ID2, SUBJECT_ID3, METRIC_ID4, SOURCE_ID7
             ],
             email=self.email,
             description=
             "Jenny changed the username of all sources of type 'Source type' with username 'username' "
             "in all reports from 'username' to 'new username'."),
         self.report["delta"])
     self.assertEqual(
         dict(
             uuids=[
                 REPORT_ID, SUBJECT_ID, METRIC_ID, SOURCE_ID, SOURCE_ID2,
                 METRIC_ID2, SOURCE_ID5, SUBJECT_ID2, METRIC_ID3,
                 SOURCE_ID6, REPORT_ID2, SUBJECT_ID3, METRIC_ID4, SOURCE_ID7
             ],
             email=self.email,
             description=
             "Jenny changed the username of all sources of type 'Source type' with username 'username' "
             "in all reports from 'username' to 'new username'."),
         self.report2["delta"])