Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Add username replacement API
Browse files Browse the repository at this point in the history
  • Loading branch information
tuchfarber committed Feb 28, 2019
1 parent b023bc2 commit 70f5135
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
14 changes: 14 additions & 0 deletions api/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,17 @@
user.update_attribute(:username, params["retired_username"])
user.save
end

post "#{APIPREFIX}/users/:user_id/replace_username" do |user_id|
if not params["new_username"]
error 500, {message: "Missing new_username param. "}.to_json
end
begin
user = User.find_by(external_id: user_id)
rescue Mongoid::Errors::DocumentNotFound
error 404, {message: "User not found."}.to_json
end
user.update_attribute(:username, params["new_username"])
user.replace_username_in_all_content(params["new_username"])
user.save
end
30 changes: 30 additions & 0 deletions models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ def retire_all_content(retired_username)
end
end

def replace_comment_username(comment, new_username)
# Replace the username of a single comment with the new username
data = {
username: new_username
}
comment.without_es do
comment.update!(data)
end
{
update: {
_index: Content::ES_INDEX_NAME,
_type: comment.__elasticsearch__.document_type,
_id: comment._id,
data: { doc: data }
}
}
end

def replace_username_in_all_content(new_username)
# Replaces the username on all content authored by this user
user_comments = all_comments
user_comment_threads = all_comment_threads
user_content = all_comments + all_comment_threads
unless user_content.empty?
bulk_data = user_content.map {|comment| replace_comment_username(comment, new_username)}
Elasticsearch::Model.client.bulk(body: bulk_data)
end
end


def mark_as_read(thread)
read_state = read_states.find_or_create_by(course_id: thread.course_id)
read_state.last_read_times[thread.id.to_s] = Time.now.utc
Expand Down
67 changes: 67 additions & 0 deletions spec/api/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -528,5 +528,72 @@ def test_unicode_data(text)
end
end

describe "POST /api/v1/users/:user_id/replace_username" do
include_context 'search_enabled'

describe "with an inactive forums user," do
before :each do
User.all.delete
Content.all.delete
create_test_user(1)
# No user content
end

it "replaces a user's username" do
new_username = "test_username_replacement"
user = User.where(external_id: '1').first
# User should have original username.
expect(user.username).to eq('user1')

# Replace the username.
post "/api/v1/users/#{user.external_id}/replace_username", new_username: new_username
expect(last_response).to be_ok

user.reload
# User should have new username.
expect(user.username).to eq(new_username)
end
end

describe "with an active forums user," do
before :each do
User.all.delete
Content.all.delete
init_without_subscriptions
end

it "attempts to replace username without sending new username" do
post "/api/v1/users/1/replace_username"
expect(last_response.status).to eq(500)
end

it "attempts to replace username of a non-existant user" do
post "/api/v1/users/1234/replace_username", new_username: new_username
expect(last_response.status).to eq(404)
end

it "attempts to replace username and username on content" do
new_username = "test_username_replacement"
user = User.where(external_id: '1').first
# User should have original username.
expect(user.username).to eq('user1')

# Change the username.
post "/api/v1/users/#{user.external_id}/replace_username", new_username: new_username
expect(last_response).to be_ok

user.reload
# User should have new username.
expect(user.username).to eq(new_username)

# User's comments should all have new username.
comments = user.all_comments + user.all_comment_threads
expect(comments.count).should_not eq(0)
comments.each do |single_comment|
expect(single_comment.author_username).to match(new_username)
end
end
end
end
end
end

0 comments on commit 70f5135

Please sign in to comment.