Fix notification-patch CLI fails if type not given

The type field was always being sent to the API even if a value wasn't
given. Change code to only send type field if --type was actually used
as an argument

Added tests for this case

Change-Id: Idb495eceb189db341753fd82be57d414e17cd64b
Closes-Bug: #1664326
This commit is contained in:
Craig Bryant 2017-02-13 11:36:57 -07:00
parent 6c91e28edb
commit 537389cad3
2 changed files with 30 additions and 9 deletions

View File

@ -1,4 +1,4 @@
# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP
# (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -266,6 +266,31 @@ class ShellTestMonascaCommands(ShellBase):
self.assertEqual(data, self.requests_mock.last_request.json())
def test_good_notifications_patch(self):
args = '--type EMAIL --address john.doe@hpe.com --period 0'
data = {'type': 'EMAIL',
'address': 'john.doe@hpe.com',
'period': 0}
self.run_notification_patch_test(args, data)
def test_good_notifications_patch_just_name(self):
name = 'fred'
args = '--name ' + name
data = {'name': name}
self.run_notification_patch_test(args, data)
def test_good_notifications_patch_just_address(self):
address = 'fred@fl.com'
args = '--address ' + address
data = {'address': address}
self.run_notification_patch_test(args, data)
def test_good_notifications_patch_just_period(self):
period = 0
args = '--period ' + str(period)
data = {'period': period}
self.run_notification_patch_test(args, data)
def run_notification_patch_test(self, args, data):
self._script_keystone_client()
self.m.ReplayAll()
@ -279,15 +304,10 @@ class ShellTestMonascaCommands(ShellBase):
headers=headers,
json='id')
argstring = 'notification-patch {0} --type EMAIL --address' \
' john.doe@hpe.com --period 0'.format(id_str)
argstring = 'notification-patch {0} {1}'.format(id_str, args)
retvalue = self.shell(argstring)
self.assertRegexpMatches(retvalue, "id")
data = {'type': 'EMAIL',
'address': 'john.doe@hpe.com',
'period': 0}
self.assertHeaders()
self.assertEqual(data, self.requests_mock.last_request.json())

View File

@ -1,4 +1,4 @@
# (C) Copyright 2014-2016 Hewlett Packard Enterprise Development LP
# (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
@ -758,7 +758,8 @@ def do_notification_patch(mc, args):
if args.name:
fields['name'] = args.name
fields['type'] = args.type
if args.type:
fields['type'] = args.type
if args.address:
fields['address'] = args.address
if args.period or args.period == 0: