From c6ab9fec9db029aad4011a0e5ff3dcb1ea788ce2 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Mon, 15 Apr 2013 14:52:07 -0400 Subject: [PATCH] Unregister forum membership at Discourse Hub when deleting a user --- lib/discourse_hub.rb | 34 ++++++++++++++++++-------- lib/user_destroyer.rb | 1 + spec/components/discourse_hub_spec.rb | 12 +++++++++ spec/components/user_destroyer_spec.rb | 20 +++++++++++++++ 4 files changed, 57 insertions(+), 10 deletions(-) diff --git a/lib/discourse_hub.rb b/lib/discourse_hub.rb index 1df351170..d352f92ef 100644 --- a/lib/discourse_hub.rb +++ b/lib/discourse_hub.rb @@ -6,13 +6,13 @@ module DiscourseHub class NicknameUnavailable < RuntimeError; end def self.nickname_available?(nickname) - response = get('/users/nickname_available', {nickname: nickname}) - [response['available'], response['suggestion']] + json = get('/users/nickname_available', {nickname: nickname}) + [json['available'], json['suggestion']] end def self.nickname_match?(nickname, email) - response = get('/users/nickname_match', {nickname: nickname, email: email}) - [response['match'], response['available'] || false, response['suggestion']] + json = get('/users/nickname_match', {nickname: nickname, email: email}) + [json['match'], json['available'] || false, json['suggestion']] end def self.register_nickname(nickname, email) @@ -24,6 +24,11 @@ module DiscourseHub end end + def self.unregister_nickname(nickname) + json = delete('/memberships/' + nickname) + json.has_key?('success') + end + def self.change_nickname(current_nickname, new_nickname) json = put("/users/#{current_nickname}/nickname", {nickname: new_nickname}) if json.has_key?('success') @@ -46,18 +51,27 @@ module DiscourseHub private def self.get(rel_url, params={}) - response = RestClient.get( "#{hub_base_url}#{rel_url}", {params: {access_token: access_token}.merge(params), accept: accepts } ) - JSON.parse(response) + singular_action :get, rel_url, params end def self.post(rel_url, params={}) - response = RestClient.post( "#{hub_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts ) - JSON.parse(response) + collection_action :post, rel_url, params end def self.put(rel_url, params={}) - response = RestClient.put( "#{hub_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts ) - JSON.parse(response) + collection_action :put, rel_url, params + end + + def self.delete(rel_url, params={}) + singular_action :delete, rel_url, params + end + + def self.singular_action(action, rel_url, params={}) + JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {params: {access_token: access_token}.merge(params), accept: accepts } ) + end + + def self.collection_action(action, rel_url, params={}) + JSON.parse RestClient.send(action, "#{hub_base_url}#{rel_url}", {access_token: access_token}.merge(params), content_type: :json, accept: accepts ) end def self.hub_base_url diff --git a/lib/user_destroyer.rb b/lib/user_destroyer.rb index 306d97e4b..3b2230657 100644 --- a/lib/user_destroyer.rb +++ b/lib/user_destroyer.rb @@ -20,6 +20,7 @@ class UserDestroyer user.destroy.tap do |u| if u AdminLogger.new(@admin).log_user_deletion(user) + DiscourseHub.unregister_nickname(user.username) MessageBus.publish "/file-change", ["refresh"], user_ids: [user.id] end end diff --git a/spec/components/discourse_hub_spec.rb b/spec/components/discourse_hub_spec.rb index ae2c4183a..9af97c11b 100644 --- a/spec/components/discourse_hub_spec.rb +++ b/spec/components/discourse_hub_spec.rb @@ -55,6 +55,18 @@ describe DiscourseHub do end end + describe '#unregister_nickname' do + it 'should return true when unregister succeeds' do + RestClient.stubs(:delete).returns( {success: 'OK'}.to_json ) + DiscourseHub.unregister_nickname('byebye').should be_true + end + + it 'should return false when unregister fails' do + RestClient.stubs(:delete).returns( {failed: -20}.to_json ) + DiscourseHub.unregister_nickname('byebye').should be_false + end + end + describe '#discourse_version_check' do it 'should return just return the json that the hub returns' do hub_response = {'success' => 'OK', 'latest_version' => '0.8.1', 'critical_updates' => false} diff --git a/spec/components/user_destroyer_spec.rb b/spec/components/user_destroyer_spec.rb index 6b1540066..6d9ef0bf2 100644 --- a/spec/components/user_destroyer_spec.rb +++ b/spec/components/user_destroyer_spec.rb @@ -2,6 +2,11 @@ require 'spec_helper' require_dependency 'user_destroyer' describe UserDestroyer do + + before do + RestClient.stubs(:delete).returns( {success: 'OK'}.to_json ) + end + describe 'new' do it 'raises an error when user is nil' do expect { UserDestroyer.new(nil) }.to raise_error(Discourse::InvalidParameters) @@ -57,6 +62,11 @@ describe UserDestroyer do AdminLogger.any_instance.expects(:log_user_deletion).never destroy rescue nil end + + it 'should not unregister the user at the discourse hub' do + DiscourseHub.expects(:unregister_nickname).never + destroy rescue nil + end end context 'user has no posts' do @@ -75,6 +85,11 @@ describe UserDestroyer do AdminLogger.any_instance.expects(:log_user_deletion).with(@user).once destroy end + + it 'should unregister the nickname as the discourse hub' do + DiscourseHub.expects(:unregister_nickname).with(@user.username) + destroy + end end context 'and destroy fails' do @@ -90,6 +105,11 @@ describe UserDestroyer do AdminLogger.any_instance.expects(:log_user_deletion).never destroy end + + it 'should not unregister the user at the discourse hub' do + DiscourseHub.expects(:unregister_nickname).never + destroy rescue nil + end end end end