from unittest import mock
from django.test import TestCase, override_settings

from promgen import models
from promgen.sender.linenotify import SenderLineNotify
from promgen.tests import TEST_ALERT, TEST_SETTINGS


_MESSAGE = '''node_down foo-BETA testhost.localhost:9100 node resolved

description: testhost.localhost:9100 of job node has been down for more than 5 minutes.
summary: Instance testhost.localhost:9100 down

Prometheus: https://monitoring.promehteus.localhost/graph#%5B%7B%22expr%22%3A%22up%20%3D%3D%200%22%2C%22tab%22%3A0%7D%5D
Alert Manager: https://am.promehteus.localhost'''


class LineNotifyTest(TestCase):
    @mock.patch('django.db.models.signals.post_save', mock.Mock())
    def setUp(self):
        self.service = models.Service.objects.create(name='Service 1')
        self.project = models.Project.objects.create(name='Project 1', service=self.service)
        self.sender = models.Sender.objects.create(
            project=self.project,
            sender='promgen.sender.linenotify',
            value='hogehoge',
        )

    @override_settings(PROMGEN=TEST_SETTINGS)
    @mock.patch('requests.post')
    def test_project(self, mock_post):
        self.assertEqual(SenderLineNotify().send(TEST_ALERT), 1)
        mock_post.assert_called_once_with(
            'https://notify.example',
            data={'message': _MESSAGE},
            headers={'Authorization': 'Bearer hogehoge'},
        )

@@ -18,16 +18,76 @@
 ort mock
+%0A%0Afrom django.contrib.contenttypes.models import ContentType
 %0Afrom dj
@@ -769,16 +769,100 @@
 (self):%0A
+        project_type = ContentType.objects.get_by_natural_key('promgen', 'Project')%0A
         
@@ -1081,23 +1081,25 @@
         
-pr
 o
+b
 ject
+_id
 =self.pr
@@ -1103,16 +1103,64 @@
 .project
+.id,%0A            content_type_id=project_type.id
 ,%0A      
@@ -1383,13 +1383,12 @@
 sert
-Equal
+True
 (Sen
@@ -1423,11 +1423,8 @@
 ERT)
-, 1
 )%0A  

Convert promgen.sender.linenotify to class