From 25a8d5fadd5ec3cf76b296bc882a8da7ea1d52e9 Mon Sep 17 00:00:00 2001 From: Steve Howell Date: Tue, 16 Jul 2013 16:25:34 -0400 Subject: [PATCH] Create back end for patching bots. (imported from commit 189444d79bb7832b26f3e9c1b65da260122255bd) --- humbug/urls.py | 2 ++ zephyr/tests.py | 36 ++++++++++++++++++++++++++++++++++++ zephyr/views.py | 19 +++++++++++++++++++ 3 files changed, 57 insertions(+) diff --git a/humbug/urls.py b/humbug/urls.py index 288398641d..d468b23771 100644 --- a/humbug/urls.py +++ b/humbug/urls.py @@ -170,6 +170,8 @@ v1_api_and_json_patterns = patterns('zephyr.views', 'PATCH': 'update_subscriptions_backend'}), url(r'^users/(?P.*)$', 'rest_dispatch', {'DELETE': 'deactivate_user_backend'}), + url(r'^bots/(?P.*)$', 'rest_dispatch', + {'PATCH': 'patch_bot_backend'}), url(r'^register$', 'rest_dispatch', {'POST': 'api_events_register'}), ) diff --git a/zephyr/tests.py b/zephyr/tests.py index 622026a698..feb1f12fa3 100644 --- a/zephyr/tests.py +++ b/zephyr/tests.py @@ -575,6 +575,42 @@ class BotTest(AuthedTestCase): self.login("hamlet@humbughq.com") self.assert_num_bots_equal(1) + def get_bot(self): + result = self.client.post("/json/get_bots") + bots = ujson.loads(result.content)['bots'] + return bots[0] + + def test_patch_bot_full_name(self): + self.login("hamlet@humbughq.com") + bot_info = { + 'full_name': 'The Bot of Hamlet', + 'short_name': 'hambot', + } + result = self.client.post("/json/create_bot", bot_info) + self.assert_json_success(result) + bot_info = { + 'full_name': 'Fred', + } + result = self.client_patch("/json/bots/hambot-bot@humbughq.com", bot_info) + self.assert_json_success(result) + + full_name = ujson.loads(result.content)['full_name'] + self.assertEqual('Fred', full_name) + + bot = self.get_bot() + self.assertEqual('Fred', bot['full_name']) + + def test_patch_bogus_bot(self): + # Deleting a bogus bot will succeed silently. + self.login("hamlet@humbughq.com") + self.create_bot() + bot_info = { + 'full_name': 'Fred', + } + result = self.client_patch("/json/bots/nonexistent-bot@humbughq.com", bot_info) + self.assert_json_error(result, 'No such user') + self.assert_num_bots_equal(1) + class PointerTest(AuthedTestCase): def test_update_pointer(self): diff --git a/zephyr/views.py b/zephyr/views.py index 298dc36e12..469b97184a 100644 --- a/zephyr/views.py +++ b/zephyr/views.py @@ -1997,6 +1997,25 @@ def deactivate_user_backend(request, user_profile, email): do_deactivate(target) return json_success({}) +@has_request_variables +def patch_bot_backend(request, user_profile, email, full_name=REQ): + # TODO: + # 1) Validate data + # 2) Support avatar changes + # Note that API key changes will be done with a separate POST command: + # POST /bots/[email]/api_key/regenerate + try: + bot = get_user_profile_by_email(email) + except: + return json_error('No such user') + bot.full_name = full_name + bot.save() + + json_result = dict( + full_name = full_name, + ) + return json_success(json_result) + @authenticated_json_post_view @has_request_variables def json_create_bot(request, user_profile, full_name=REQ, short_name=REQ):