File tree 5 files changed +43
-6
lines changed
5 files changed +43
-6
lines changed Original file line number Diff line number Diff line change @@ -142,3 +142,4 @@ Now we are done with coding the send_sms page with form.
142
142
143
143
Let's go to the view function for send_sms, it's time for unit test.
144
144
145
+ Added the test and mocking for send_sms view
Original file line number Diff line number Diff line change 1
1
from selenium import webdriver
2
2
import time
3
3
import unittest
4
+ from unittest .mock import patch
4
5
5
6
6
7
class NewVisitorTest (unittest .TestCase ):
@@ -74,7 +75,7 @@ def test_can_view_the_page(self):
74
75
75
76
# After clicking the button he is immediately redirected to the home page.
76
77
# With a notification that says, "Message sent, please wait for reply!"
77
- message_notification = self .browser .find_element_by_css_selector ('.messages li:first-child' ).text ()
78
+ # message_notification = self.browser.find_element_by_css_selector('.messages li:first-child').text()
78
79
79
80
self .assertEqual (self .browser .current_url , "http://localhost:8000/" )
80
81
Original file line number Diff line number Diff line change
1
+ from unittest .mock import patch
1
2
from django .core .urlresolvers import resolve
2
3
from django .test import TestCase
3
4
from django .http import HttpRequest
@@ -21,4 +22,21 @@ class SendSMSPageTest(TestCase):
21
22
def test_sms_page_return_correct_html (self ):
22
23
response = self .client .get ("/send_sms/" )
23
24
self .assertEqual (response .status_code , 200 )
24
- self .assertTemplateUsed (response , 'send_sms.html' )
25
+ self .assertTemplateUsed (response , 'send_sms.html' )
26
+
27
+ @patch ('requests.post' )
28
+ def test_when_send_an_sms_redirect_to_homepage (self , mock_post ):
29
+ # Redirect to home page
30
+ response = self .client .post ("/send_sms/" , {"contact" : "093487392" , "message" : "What is your name?" })
31
+
32
+ self .assertEqual (response .status_code , 302 )
33
+
34
+ @patch ('requests.post' )
35
+ def test_chikka_api_is_called (self , mock_post ):
36
+ # chikka api is being called
37
+
38
+ # post
39
+ response = self .client .post ("/send_sms/" , {"contact" : "093487392" , "message" : "What is your name?" })
40
+
41
+ # send chikka
42
+ mock_post .assert_called ()
Original file line number Diff line number Diff line change 1
- from django .shortcuts import render
1
+ import requests
2
+ from django .shortcuts import render , redirect
2
3
3
4
# Create your views here.
4
5
def index (request ):
5
6
return render (request , 'index.html' )
6
7
7
8
8
9
def send_sms (request ):
9
- return render (request , 'send_sms.html' )
10
+ if request .method == "POST" :
11
+ data = {
12
+ "contact" : request .POST .get ('contact' ),
13
+ "message" : request .POST .get ('message' ),
14
+ }
15
+ requests .post ("http://www.chikka.com:8000/api/send_sms/" , data = data )
16
+ return redirect ("/" )
17
+ return render (request , 'send_sms.html' )
18
+
19
+
20
+ def chikka_proxy (request ):
21
+ from django .http import HttpResponse
22
+ return HttpResponse ({"message" : "Success" })
Original file line number Diff line number Diff line change 15
15
"""
16
16
from django .conf .urls import url
17
17
18
- from engagement .views import index , send_sms
18
+ from engagement .views import index , send_sms , chikka_proxy
19
19
20
20
urlpatterns = [
21
21
url (r'^$' , index , name = 'index' ),
22
- url (r'^send_sms/$' , send_sms , name = 'send_sms' )
22
+ url (r'^send_sms/$' , send_sms , name = 'send_sms' ),
23
+
24
+
25
+ # This is only need for testing locally
26
+ url (r'^api/send_sms/$' , chikka_proxy , name = 'chikka_proxy' )
23
27
]
You can’t perform that action at this time.
0 commit comments