2016-04-26 13:31:57 +01:00
import pytest
2016-03-09 14:30:50 +00:00
from flask import url_for
2016-03-30 17:12:00 +01:00
import app
from app.utils import email_safe
2016-06-01 16:07:43 +01:00
from tests import validate_route_permission , service_json
2016-03-09 14:30:50 +00:00
from bs4 import BeautifulSoup
2016-04-26 13:31:57 +01:00
from unittest.mock import ANY , Mock
from werkzeug.exceptions import InternalServerError
2016-01-07 12:29:56 +00:00
2016-08-22 11:43:35 +01:00
def test_should_show_overview (
app_ ,
active_user_with_permissions ,
mocker ,
2016-08-24 09:34:14 +01:00
service_one ,
mock_get_organisation
2016-08-22 11:43:35 +01:00
):
with app_ . test_request_context (), app_ . test_client () as client :
client . login ( active_user_with_permissions , mocker , service_one )
response = client . get ( url_for (
'main.service_settings' , service_id = service_one [ 'id' ]
))
assert response . status_code == 200
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( 'h1' ) . text == 'Settings'
for index , row in enumerate ([
'Service name service one Change' ,
'Email reply to address None Change' ,
'Text message sender 40604 Change'
]):
assert row == " " . join ( page . find_all ( 'tr' )[ index + 1 ] . text . split ())
app . service_api_client . get_service . assert_called_with ( service_one [ 'id' ])
def test_should_show_overview_for_service_with_more_things_set (
app_ ,
active_user_with_permissions ,
mocker ,
2016-08-24 09:34:14 +01:00
service_with_reply_to_addresses ,
mock_get_organisation
2016-08-22 11:43:35 +01:00
):
with app_ . test_request_context (), app_ . test_client () as client :
client . login ( active_user_with_permissions , mocker , service_with_reply_to_addresses )
response = client . get ( url_for (
'main.service_settings' , service_id = service_with_reply_to_addresses [ 'id' ]
))
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
for index , row in enumerate ([
'Service name service one Change' ,
'Email reply to address test@example.com Change' ,
'Text message sender elevenchars Change'
]):
assert row == " " . join ( page . find_all ( 'tr' )[ index + 1 ] . text . split ())
2016-01-07 12:29:56 +00:00
2016-01-27 12:22:32 +00:00
def test_should_show_service_name ( app_ ,
2016-03-30 17:12:00 +01:00
active_user_with_permissions ,
mocker ,
service_one ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-03-30 17:12:00 +01:00
client . login ( active_user_with_permissions , mocker , service_one )
2016-01-18 16:01:04 +00:00
response = client . get ( url_for (
2016-03-30 17:12:00 +01:00
'main.service_name_change' , service_id = service_one [ 'id' ]))
2016-01-11 16:03:41 +00:00
assert response . status_code == 200
2016-06-20 13:33:29 +01:00
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( 'h1' ) . text == 'Change your service name'
assert page . find ( 'input' , attrs = { "type" : "text" })[ 'value' ] == 'service one'
2016-03-30 17:12:00 +01:00
app . service_api_client . get_service . assert_called_with ( service_one [ 'id' ])
2016-01-11 16:03:41 +00:00
2016-01-27 12:22:32 +00:00
def test_should_redirect_after_change_service_name ( app_ ,
2016-03-30 17:12:00 +01:00
service_one ,
2016-04-14 15:38:27 +01:00
active_user_with_permissions ,
mock_login ,
mock_get_user ,
mock_get_service ,
mock_update_service ,
mock_get_services ,
mock_has_permissions ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-04-14 15:38:27 +01:00
client . login ( active_user_with_permissions )
2016-03-10 14:29:31 +00:00
response = client . post (
2016-03-30 17:12:00 +01:00
url_for ( 'main.service_name_change' , service_id = service_one [ 'id' ]),
2016-03-10 14:29:31 +00:00
data = { 'name' : "new name" })
2016-01-07 12:29:56 +00:00
2016-01-18 17:35:28 +00:00
assert response . status_code == 302
settings_url = url_for (
2016-03-30 17:12:00 +01:00
'main.service_name_change_confirm' , service_id = service_one [ 'id' ], _external = True )
2016-01-18 17:35:28 +00:00
assert settings_url == response . location
2016-03-30 17:12:00 +01:00
assert mock_get_services . called
2016-01-07 12:29:56 +00:00
2016-09-05 14:01:28 +01:00
def test_show_restricted_service (
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_has_permissions ,
mock_get_service ,
mock_get_organisation ,
):
with app_ . test_request_context (), app_ . test_client () as client :
client . login ( active_user_with_permissions )
response = client . get ( url_for ( 'main.service_settings' , service_id = service_one [ 'id' ]))
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( 'h1' ) . text == 'Settings'
assert page . find_all ( 'h2' )[ 1 ] . text == 'Your service is in trial mode'
2016-08-24 09:34:14 +01:00
def test_switch_service_to_live (
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_get_service ,
mock_update_service ,
mock_has_permissions ,
mock_get_organisation
):
2016-04-14 15:38:27 +01:00
with app_ . test_request_context ():
with app_ . test_client () as client :
client . login ( active_user_with_permissions )
response = client . get (
url_for ( 'main.service_switch_live' , service_id = service_one [ 'id' ]))
assert response . status_code == 302
assert response . location == url_for (
'main.service_settings' ,
service_id = service_one [ 'id' ], _external = True )
2016-08-11 12:32:38 +01:00
mock_update_service . assert_called_with (
service_one [ 'id' ],
message_limit = 250000 ,
restricted = False
)
2016-04-14 15:38:27 +01:00
2016-09-05 14:01:28 +01:00
def test_show_live_service (
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_get_live_service ,
mock_has_permissions ,
mock_get_organisation ,
):
with app_ . test_request_context (), app_ . test_client () as client :
client . login ( active_user_with_permissions )
response = client . get ( url_for ( 'main.service_settings' , service_id = service_one [ 'id' ]))
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( 'h1' ) . text . strip () == 'Settings'
assert 'Your service is in trial mode' not in page . text
2016-08-24 09:34:14 +01:00
def test_switch_service_to_restricted (
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_get_live_service ,
mock_update_service ,
mock_has_permissions ,
mock_get_organisation
):
2016-04-14 15:38:27 +01:00
with app_ . test_request_context ():
with app_ . test_client () as client :
client . login ( active_user_with_permissions )
response = client . get (
url_for ( 'main.service_switch_live' , service_id = service_one [ 'id' ]))
assert response . status_code == 302
assert response . location == url_for (
'main.service_settings' ,
service_id = service_one [ 'id' ], _external = True )
2016-08-11 12:32:38 +01:00
mock_update_service . assert_called_with (
service_one [ 'id' ],
message_limit = 50 ,
restricted = True
)
2016-04-14 15:38:27 +01:00
2016-03-10 14:29:31 +00:00
def test_should_not_allow_duplicate_names ( app_ ,
2016-03-30 17:12:00 +01:00
active_user_with_permissions ,
mocker ,
2016-03-31 15:17:05 +01:00
service_one ):
2016-03-10 14:29:31 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-03-30 17:12:00 +01:00
client . login ( active_user_with_permissions , mocker , service_one )
2016-03-31 15:17:05 +01:00
mocker . patch ( 'app.service_api_client.find_all_service_email_from' ,
return_value = [ 'service_one' , 'service.two' ])
2016-03-30 17:12:00 +01:00
service_id = service_one [ 'id' ]
2016-03-10 14:29:31 +00:00
response = client . post (
url_for ( 'main.service_name_change' , service_id = service_id ),
2016-03-31 15:17:05 +01:00
data = { 'name' : "SErvICE TWO" })
2016-03-10 14:29:31 +00:00
assert response . status_code == 200
resp_data = response . get_data ( as_text = True )
assert 'This service name is already in use' in resp_data
2016-03-31 15:17:05 +01:00
app . service_api_client . find_all_service_email_from . assert_called_once_with ()
2016-03-10 14:29:31 +00:00
2016-01-18 17:35:28 +00:00
def test_should_show_service_name_confirmation ( app_ ,
2016-03-30 17:12:00 +01:00
active_user_with_permissions ,
mocker ,
service_one ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-03-30 17:12:00 +01:00
client . login ( active_user_with_permissions , mocker , service_one )
2016-01-18 16:01:04 +00:00
response = client . get ( url_for (
2016-03-30 17:12:00 +01:00
'main.service_name_change_confirm' , service_id = service_one [ 'id' ]))
2016-01-07 16:24:43 +00:00
2016-01-18 17:35:28 +00:00
assert response . status_code == 200
resp_data = response . get_data ( as_text = True )
assert 'Change your service name' in resp_data
2016-03-30 17:12:00 +01:00
app . service_api_client . get_service . assert_called_with ( service_one [ 'id' ])
2016-01-07 16:24:43 +00:00
2016-08-24 09:34:14 +01:00
def test_should_redirect_after_service_name_confirmation (
app_ ,
active_user_with_permissions ,
service_one ,
mocker ,
mock_update_service ,
mock_verify_password ,
mock_get_organisation
):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-03-30 17:12:00 +01:00
client . login ( active_user_with_permissions , mocker , service_one )
service_id = service_one [ 'id' ]
2016-01-18 17:35:28 +00:00
service_new_name = 'New Name'
with client . session_transaction () as session :
session [ 'service_name_change' ] = service_new_name
2016-01-18 16:01:04 +00:00
response = client . post ( url_for (
'main.service_name_change_confirm' , service_id = service_id ))
2016-01-07 16:24:43 +00:00
2016-01-18 17:35:28 +00:00
assert response . status_code == 302
2016-03-30 17:12:00 +01:00
settings_url = url_for ( 'main.service_settings' , service_id = service_id , _external = True )
2016-01-18 17:35:28 +00:00
assert settings_url == response . location
2016-08-11 12:32:38 +01:00
mock_update_service . assert_called_once_with (
service_id ,
name = service_new_name ,
email_from = email_safe ( service_new_name )
)
2016-03-30 17:12:00 +01:00
assert mock_verify_password . called
2016-01-07 16:24:43 +00:00
2016-03-10 14:29:31 +00:00
def test_should_raise_duplicate_name_handled ( app_ ,
2016-03-30 17:12:00 +01:00
active_user_with_permissions ,
service_one ,
mocker ,
mock_get_services ,
2016-03-10 14:29:31 +00:00
mock_update_service_raise_httperror_duplicate_name ,
2016-04-14 15:38:27 +01:00
mock_verify_password ,
fake_uuid ):
2016-03-10 14:29:31 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-03-30 17:12:00 +01:00
client . login ( active_user_with_permissions , mocker , service_one )
2016-03-10 14:29:31 +00:00
service_new_name = 'New Name'
with client . session_transaction () as session :
session [ 'service_name_change' ] = service_new_name
response = client . post ( url_for (
2016-03-30 17:12:00 +01:00
'main.service_name_change_confirm' , service_id = service_one [ 'id' ]))
2016-03-10 14:29:31 +00:00
assert response . status_code == 302
name_change_url = url_for (
2016-03-30 17:12:00 +01:00
'main.service_name_change' , service_id = service_one [ 'id' ], _external = True )
2016-03-10 14:29:31 +00:00
assert name_change_url == response . location
2016-03-30 17:12:00 +01:00
assert mock_update_service_raise_httperror_duplicate_name . called
assert mock_verify_password . called
2016-03-10 14:29:31 +00:00
2016-01-27 12:22:32 +00:00
def test_should_show_request_to_go_live ( app_ ,
api_user_active ,
mock_get_service ,
2016-01-27 16:30:33 +00:00
mock_get_user ,
mock_get_user_by_email ,
2016-02-29 17:03:56 +00:00
mock_login ,
2016-04-14 15:38:27 +01:00
mock_has_permissions ,
fake_uuid ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-01-27 12:22:32 +00:00
client . login ( api_user_active )
2016-04-14 15:38:27 +01:00
service_id = fake_uuid
2016-01-18 16:01:04 +00:00
response = client . get (
url_for ( 'main.service_request_to_go_live' , service_id = service_id ))
2016-01-18 17:35:28 +00:00
service = mock_get_service . side_effect ( service_id )[ 'data' ]
assert response . status_code == 200
resp_data = response . get_data ( as_text = True )
assert 'Request to go live' in resp_data
assert mock_get_service . called
2016-01-18 16:01:04 +00:00
2016-04-26 13:31:57 +01:00
def test_should_redirect_after_request_to_go_live (
2016-08-24 09:34:14 +01:00
app_ ,
api_user_active ,
mock_get_user ,
mock_get_service ,
mock_has_permissions ,
mock_get_organisation ,
mocker
2016-04-26 13:31:57 +01:00
):
mock_post = mocker . patch (
'app.main.views.feedback.requests.post' ,
return_value = Mock ( status_code = 201 ))
2016-01-18 16:01:04 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-01-27 12:22:32 +00:00
client . login ( api_user_active )
2016-04-26 13:31:57 +01:00
response = client . post (
url_for ( 'main.service_request_to_go_live' , service_id = '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6' ),
data = { 'usage' : "One million messages" },
follow_redirects = True
)
assert response . status_code == 200
mock_post . assert_called_with (
ANY ,
data = {
'subject' : 'Request to go live' ,
'department_id' : ANY ,
2016-05-20 15:58:20 +01:00
'agent_team_id' : ANY ,
2016-08-04 18:01:08 +01:00
'message' : 'On behalf of Test Service (http://localhost/services/6ce466d0-fd6a-11e5-82f5-e0accb9d11a6/dashboard) \n\n Usage estimate \n --- \n\n One million messages' , # noqa
'person_name' : api_user_active . name ,
'person_email' : api_user_active . email_address
2016-04-26 13:31:57 +01:00
},
headers = ANY
)
2016-03-09 14:30:50 +00:00
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
flash_banner = page . find ( 'div' , class_ = 'banner-default' ) . string . strip ()
2016-04-26 13:31:57 +01:00
h1 = page . find ( 'h1' ) . string . strip ()
assert flash_banner == 'We’ ve received your request to go live'
assert h1 == 'Settings'
def test_log_error_on_request_to_go_live (
2016-08-24 09:34:14 +01:00
app_ ,
api_user_active ,
mock_get_user ,
mock_get_service ,
mock_has_permissions ,
mocker
2016-04-26 13:31:57 +01:00
):
mock_post = mocker . patch (
'app.main.views.service_settings.requests.post' ,
return_value = Mock (
status_code = 401 ,
json = lambda : {
'error_code' : 'invalid_auth' ,
'error_message' : 'Please provide a valid API key or token'
}
)
)
with app_ . test_request_context ():
mock_logger = mocker . patch . object ( app_ . logger , 'error' )
with app_ . test_client () as client :
client . login ( api_user_active )
with pytest . raises ( InternalServerError ):
resp = client . post (
url_for ( 'main.service_request_to_go_live' , service_id = '6ce466d0-fd6a-11e5-82f5-e0accb9d11a6' ),
data = { 'usage' : 'blah' }
)
mock_logger . assert_called_with (
"Deskpro create ticket request failed with {} ' {} '" . format ( mock_post () . status_code , mock_post () . json ())
)
2016-01-18 16:01:04 +00:00
2016-01-27 12:22:32 +00:00
def test_should_show_delete_page ( app_ ,
api_user_active ,
2016-04-04 16:53:52 +01:00
mock_login ,
2016-01-27 12:22:32 +00:00
mock_get_service ,
2016-01-27 16:30:33 +00:00
mock_get_user ,
mock_get_user_by_email ,
2016-04-14 15:38:27 +01:00
mock_has_permissions ,
fake_uuid ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-01-27 12:22:32 +00:00
client . login ( api_user_active )
2016-04-14 15:38:27 +01:00
service_id = fake_uuid
2016-01-18 16:01:04 +00:00
response = client . get ( url_for (
'main.service_delete' , service_id = service_id ))
2016-01-07 12:29:56 +00:00
2016-01-18 17:35:28 +00:00
assert response . status_code == 200
2016-01-22 14:38:10 +00:00
assert 'Delete this service from GOV.UK Notify' in response . get_data ( as_text = True )
2016-01-18 17:35:28 +00:00
assert mock_get_service . called
2016-01-07 12:29:56 +00:00
2016-01-27 12:22:32 +00:00
def test_should_show_redirect_after_deleting_service ( app_ ,
api_user_active ,
mock_get_service ,
2016-01-27 16:30:33 +00:00
mock_get_user ,
mock_get_user_by_email ,
2016-02-29 17:03:56 +00:00
mock_login ,
2016-04-14 15:38:27 +01:00
mock_has_permissions ,
fake_uuid ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-01-27 12:22:32 +00:00
client . login ( api_user_active )
2016-04-14 15:38:27 +01:00
service_id = fake_uuid
2016-01-18 16:01:04 +00:00
response = client . post ( url_for (
'main.service_delete' , service_id = service_id ))
2016-01-07 12:29:56 +00:00
2016-01-18 17:35:28 +00:00
assert response . status_code == 302
delete_url = url_for (
'main.service_delete_confirm' , service_id = service_id , _external = True )
assert delete_url == response . location
2016-01-07 16:24:43 +00:00
2016-01-27 12:22:32 +00:00
def test_should_show_delete_confirmation ( app_ ,
api_user_active ,
mock_get_service ,
2016-01-27 16:30:33 +00:00
mock_get_user ,
mock_get_user_by_email ,
2016-02-29 17:03:56 +00:00
mock_login ,
2016-04-14 15:38:27 +01:00
mock_has_permissions ,
fake_uuid ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-01-27 12:22:32 +00:00
client . login ( api_user_active )
2016-04-14 15:38:27 +01:00
service_id = fake_uuid
2016-01-18 16:01:04 +00:00
response = client . get ( url_for (
'main.service_delete_confirm' , service_id = service_id ))
2016-01-07 16:24:43 +00:00
2016-01-18 17:35:28 +00:00
assert response . status_code == 200
assert 'Delete this service from Notify' in response . get_data ( as_text = True )
assert mock_get_service . called
2016-01-07 16:24:43 +00:00
2016-01-18 16:01:04 +00:00
def test_should_redirect_delete_confirmation ( app_ ,
2016-01-27 12:22:32 +00:00
api_user_active ,
2016-01-18 16:01:04 +00:00
mock_get_service ,
2016-01-20 15:13:15 +00:00
mock_delete_service ,
2016-01-27 16:30:33 +00:00
mock_get_user ,
mock_get_user_by_email ,
2016-01-27 12:22:32 +00:00
mock_login ,
2016-02-29 17:03:56 +00:00
mock_verify_password ,
2016-04-14 15:38:27 +01:00
mock_has_permissions ,
fake_uuid ):
2016-01-15 15:15:35 +00:00
with app_ . test_request_context ():
with app_ . test_client () as client :
2016-01-27 12:22:32 +00:00
client . login ( api_user_active )
2016-04-14 15:38:27 +01:00
service_id = fake_uuid
2016-01-18 16:01:04 +00:00
response = client . post ( url_for (
'main.service_delete_confirm' , service_id = service_id ))
2016-01-07 16:24:43 +00:00
2016-01-18 17:35:28 +00:00
assert response . status_code == 302
choose_url = url_for (
'main.choose_service' , _external = True )
assert choose_url == response . location
assert mock_get_service . called
assert mock_delete_service . called
2016-03-09 12:10:50 +00:00
2016-08-24 09:34:14 +01:00
def test_route_permissions ( mocker , app_ , api_user_active , service_one , mock_get_organisation ):
2016-03-09 12:10:50 +00:00
routes = [
'main.service_settings' ,
'main.service_name_change' ,
'main.service_name_change_confirm' ,
'main.service_request_to_go_live' ,
'main.service_delete' ,
'main.service_delete_confirm' ]
with app_ . test_request_context ():
for route in routes :
validate_route_permission (
mocker ,
app_ ,
"GET" ,
200 ,
url_for ( route , service_id = service_one [ 'id' ]),
[ 'manage_settings' ],
api_user_active ,
service_one )
2016-08-24 09:34:14 +01:00
def test_route_invalid_permissions ( mocker , app_ , api_user_active , service_one , mock_get_organisation ):
2016-03-09 12:10:50 +00:00
routes = [
'main.service_settings' ,
'main.service_name_change' ,
'main.service_name_change_confirm' ,
'main.service_request_to_go_live' ,
2016-06-01 16:07:43 +01:00
'main.service_switch_live' ,
'main.service_switch_research_mode' ,
2016-03-09 12:10:50 +00:00
'main.service_delete' ,
'main.service_delete_confirm' ]
with app_ . test_request_context ():
for route in routes :
validate_route_permission (
mocker ,
app_ ,
"GET" ,
403 ,
url_for ( route , service_id = service_one [ 'id' ]),
[ 'blah' ],
api_user_active ,
service_one )
2016-03-17 14:25:28 +00:00
2016-08-24 09:34:14 +01:00
def test_route_for_platform_admin ( mocker , app_ , platform_admin_user , service_one , mock_get_organisation ):
2016-03-17 14:25:28 +00:00
routes = [
'main.service_settings' ,
'main.service_name_change' ,
'main.service_name_change_confirm' ,
'main.service_request_to_go_live' ,
'main.service_delete' ,
'main.service_delete_confirm'
2016-06-01 16:07:43 +01:00
]
2016-03-17 14:25:28 +00:00
with app_ . test_request_context ():
for route in routes :
validate_route_permission ( mocker ,
app_ ,
"GET" ,
200 ,
url_for ( route , service_id = service_one [ 'id' ]),
[],
platform_admin_user ,
service_one )
2016-05-16 13:09:58 +01:00
2016-06-01 16:07:43 +01:00
def test_route_for_platform_admin_update_service ( mocker , app_ , platform_admin_user , service_one ):
routes = [
'main.service_switch_live' ,
'main.service_switch_research_mode'
]
with app_ . test_request_context ():
for route in routes :
validate_route_permission ( mocker ,
app_ ,
"GET" ,
302 ,
url_for ( route , service_id = service_one [ 'id' ]),
[],
platform_admin_user ,
service_one )
2016-06-01 17:02:12 +01:00
def test_set_reply_to_email_address (
2016-08-24 09:34:14 +01:00
app_ ,
active_user_with_permissions ,
mocker ,
mock_update_service ,
service_one ,
mock_get_organisation
):
2016-05-16 13:09:58 +01:00
with app_ . test_request_context ():
with app_ . test_client () as client :
client . login ( active_user_with_permissions , mocker , service_one )
data = { "email_address" : "test@someservice.gov.uk" }
response = client . post ( url_for ( 'main.service_set_reply_to_email' , service_id = service_one [ 'id' ]),
data = data ,
follow_redirects = True )
assert response . status_code == 200
2016-08-11 12:32:38 +01:00
mock_update_service . assert_called_with (
service_one [ 'id' ],
reply_to_email_address = "test@someservice.gov.uk"
)
2016-05-17 11:34:49 +01:00
def test_if_reply_to_email_address_set_then_form_populated ( app_ ,
active_user_with_permissions ,
mocker ,
service_one ):
service_one [ 'reply_to_email_address' ] = 'test@service.gov.uk'
with app_ . test_request_context ():
with app_ . test_client () as client :
client . login ( active_user_with_permissions , mocker , service_one )
response = client . get ( url_for ( 'main.service_set_reply_to_email' , service_id = service_one [ 'id' ]))
assert response . status_code == 200
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( id = 'email_address' )[ 'value' ] == 'test@service.gov.uk'
2016-06-01 16:07:43 +01:00
def test_switch_service_to_research_mode (
2016-08-23 10:16:05 +01:00
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_get_service ,
mock_has_permissions ,
mocker
):
with app_ . test_request_context (), app_ . test_client () as client :
mocker . patch ( 'app.service_api_client.post' , return_value = service_one )
2016-06-01 16:07:43 +01:00
2016-08-23 10:16:05 +01:00
client . login ( active_user_with_permissions )
response = client . get ( url_for ( 'main.service_switch_research_mode' , service_id = service_one [ 'id' ]))
assert response . status_code == 302
assert response . location == url_for ( 'main.service_settings' , service_id = service_one [ 'id' ], _external = True )
app . service_api_client . post . assert_called_with (
'/service/ {} ' . format ( service_one [ 'id' ]),
{
'research_mode' : True ,
'created_by' : active_user_with_permissions . id
}
)
2016-06-01 16:07:43 +01:00
def test_switch_service_from_research_mode_to_normal (
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_get_service ,
mock_has_permissions ,
mocker ):
with app_ . test_request_context ():
with app_ . test_client () as client :
service = service_json (
"1234" ,
"Test Service" ,
[ active_user_with_permissions . id ],
message_limit = 1000 ,
active = False ,
restricted = True ,
research_mode = True
)
mocker . patch ( 'app.service_api_client.get_service' , return_value = { "data" : service })
mocker . patch ( 'app.service_api_client.update_service_with_properties' , return_value = service_one )
client . login ( active_user_with_permissions )
response = client . get ( url_for ( 'main.service_switch_research_mode' , service_id = service_one [ 'id' ]))
assert response . status_code == 302
assert response . location == url_for ( 'main.service_settings' , service_id = service_one [ 'id' ], _external = True )
app . service_api_client . update_service_with_properties . assert_called_with (
service_one [ 'id' ], { "research_mode" : False }
)
def test_shows_research_mode_indicator (
2016-08-24 09:34:14 +01:00
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_get_service ,
mock_has_permissions ,
mock_get_organisation ,
mocker
):
2016-06-01 16:07:43 +01:00
with app_ . test_request_context ():
with app_ . test_client () as client :
service = service_json (
"1234" ,
"Test Service" ,
[ active_user_with_permissions . id ],
message_limit = 1000 ,
active = False ,
restricted = True ,
research_mode = True
)
mocker . patch ( 'app.service_api_client.get_service' , return_value = { "data" : service })
mocker . patch ( 'app.service_api_client.update_service_with_properties' , return_value = service_one )
client . login ( active_user_with_permissions )
response = client . get ( url_for ( 'main.service_settings' , service_id = service_one [ 'id' ]))
assert response . status_code == 200
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
element = page . find ( 'span' , { "id" : "research-mode" })
assert element . text == 'research mode'
def test_does_not_show_research_mode_indicator (
2016-08-24 09:34:14 +01:00
app_ ,
service_one ,
mock_login ,
mock_get_user ,
active_user_with_permissions ,
mock_get_service ,
mock_has_permissions ,
mock_get_organisation ,
mocker
):
2016-06-01 16:07:43 +01:00
with app_ . test_request_context ():
with app_ . test_client () as client :
client . login ( active_user_with_permissions )
response = client . get ( url_for ( 'main.service_settings' , service_id = service_one [ 'id' ]))
assert response . status_code == 200
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
element = page . find ( 'span' , { "id" : "research-mode" })
2016-06-01 17:02:12 +01:00
assert not element
2016-07-01 13:47:22 +01:00
def test_set_text_message_sender (
2016-08-24 09:34:14 +01:00
app_ ,
active_user_with_permissions ,
mocker ,
mock_update_service ,
service_one ,
mock_get_organisation
):
2016-07-01 13:47:22 +01:00
with app_ . test_request_context ():
with app_ . test_client () as client :
client . login ( active_user_with_permissions , mocker , service_one )
data = { "sms_sender" : "elevenchars" }
response = client . post ( url_for ( 'main.service_set_sms_sender' , service_id = service_one [ 'id' ]),
data = data ,
follow_redirects = True )
assert response . status_code == 200
2016-08-11 12:32:38 +01:00
mock_update_service . assert_called_with (
service_one [ 'id' ],
sms_sender = "elevenchars"
)
2016-07-01 13:47:22 +01:00
def test_if_sms_sender_set_then_form_populated ( app_ ,
active_user_with_permissions ,
mocker ,
service_one ):
service_one [ 'sms_sender' ] = 'elevenchars'
with app_ . test_request_context ():
with app_ . test_client () as client :
client . login ( active_user_with_permissions , mocker , service_one )
response = client . get ( url_for ( 'main.service_set_sms_sender' , service_id = service_one [ 'id' ]))
assert response . status_code == 200
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( id = 'sms_sender' )[ 'value' ] == 'elevenchars'
2016-07-04 13:46:50 +01:00
2016-08-16 13:43:15 +01:00
def test_should_show_branding (
2016-08-08 10:28:40 +01:00
mocker , app_ , platform_admin_user , service_one , mock_get_organisations
):
with app_ . test_request_context (), app_ . test_client () as client :
client . login ( platform_admin_user , mocker , service_one )
response = client . get ( url_for (
'main.service_set_branding_and_org' , service_id = service_one [ 'id' ]
))
assert response . status_code == 200
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( 'input' , attrs = { "id" : "branding_type-0" })[ 'value' ] == 'govuk'
assert page . find ( 'input' , attrs = { "id" : "branding_type-1" })[ 'value' ] == 'both'
assert page . find ( 'input' , attrs = { "id" : "branding_type-2" })[ 'value' ] == 'org'
2016-08-16 13:43:15 +01:00
assert 'checked' in page . find ( 'input' , attrs = { "id" : "branding_type-0" }) . attrs
assert 'checked' not in page . find ( 'input' , attrs = { "id" : "branding_type-1" }) . attrs
assert 'checked' not in page . find ( 'input' , attrs = { "id" : "branding_type-2" }) . attrs
app . organisations_client . get_organisations . assert_called_once_with ()
app . service_api_client . get_service . assert_called_once_with ( service_one [ 'id' ])
def test_should_show_organisations (
mocker , app_ , platform_admin_user , service_one , mock_get_organisations
):
with app_ . test_request_context (), app_ . test_client () as client :
client . login ( platform_admin_user , mocker , service_one )
response = client . get ( url_for (
'main.service_set_branding_and_org' , service_id = service_one [ 'id' ]
))
assert response . status_code == 200
page = BeautifulSoup ( response . data . decode ( 'utf-8' ), 'html.parser' )
assert page . find ( 'input' , attrs = { "id" : "branding_type-0" })[ 'value' ] == 'govuk'
assert page . find ( 'input' , attrs = { "id" : "branding_type-1" })[ 'value' ] == 'both'
assert page . find ( 'input' , attrs = { "id" : "branding_type-2" })[ 'value' ] == 'org'
assert 'checked' in page . find ( 'input' , attrs = { "id" : "branding_type-0" }) . attrs
assert 'checked' not in page . find ( 'input' , attrs = { "id" : "branding_type-1" }) . attrs
assert 'checked' not in page . find ( 'input' , attrs = { "id" : "branding_type-2" }) . attrs
2016-08-08 10:28:40 +01:00
app . organisations_client . get_organisations . assert_called_once_with ()
app . service_api_client . get_service . assert_called_once_with ( service_one [ 'id' ])
def test_should_set_branding_and_organisations (
mocker , app_ , platform_admin_user , service_one , mock_get_organisations , mock_update_service
):
with app_ . test_request_context (), app_ . test_client () as client :
client . login ( platform_admin_user , mocker , service_one )
response = client . post (
url_for (
'main.service_set_branding_and_org' , service_id = service_one [ 'id' ]
),
data = {
'branding_type' : 'org' ,
2016-08-16 13:43:15 +01:00
'organisation' : 'organisation-id'
2016-08-08 10:28:40 +01:00
}
)
assert response . status_code == 302
assert response . location == url_for ( 'main.service_settings' , service_id = service_one [ 'id' ], _external = True )
app . organisations_client . get_organisations . assert_called_once_with ()
app . service_api_client . update_service . assert_called_once_with (
service_one [ 'id' ],
branding = 'org' ,
2016-08-16 13:43:15 +01:00
organisation = 'organisation-id'
2016-08-08 10:28:40 +01:00
)