Skip to content

Commit e7de493

Browse files
committed
Added the test and mocking for send_sms view
1 parent 1ad63ae commit e7de493

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

PROCESS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,4 @@ Now we are done with coding the send_sms page with form.
142142

143143
Let's go to the view function for send_sms, it's time for unit test.
144144

145+
Added the test and mocking for send_sms view

functional_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from selenium import webdriver
22
import time
33
import unittest
4+
from unittest.mock import patch
45

56

67
class NewVisitorTest(unittest.TestCase):
@@ -74,7 +75,7 @@ def test_can_view_the_page(self):
7475

7576
# After clicking the button he is immediately redirected to the home page.
7677
# 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()
7879

7980
self.assertEqual(self.browser.current_url, "http://localhost:8000/")
8081

sms_survey/engagement/tests.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from unittest.mock import patch
12
from django.core.urlresolvers import resolve
23
from django.test import TestCase
34
from django.http import HttpRequest
@@ -21,4 +22,21 @@ class SendSMSPageTest(TestCase):
2122
def test_sms_page_return_correct_html(self):
2223
response = self.client.get("/send_sms/")
2324
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()

sms_survey/engagement/views.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
from django.shortcuts import render
1+
import requests
2+
from django.shortcuts import render, redirect
23

34
# Create your views here.
45
def index(request):
56
return render(request, 'index.html')
67

78

89
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"})

sms_survey/sms_survey/urls.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515
"""
1616
from django.conf.urls import url
1717

18-
from engagement.views import index, send_sms
18+
from engagement.views import index, send_sms, chikka_proxy
1919

2020
urlpatterns = [
2121
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')
2327
]

0 commit comments

Comments
 (0)