Unregister forum membership at Discourse Hub when deleting a user

This commit is contained in:
Neil Lalonde 2013-04-15 14:52:07 -04:00
parent 56c858e58b
commit c6ab9fec9d
4 changed files with 57 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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}

View File

@ -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