Skip to content

Commit

Permalink
Merge pull request #435 from debtcollective/od/update_user
Browse files Browse the repository at this point in the history
feat(discourse-service): add update_user method
  • Loading branch information
orlando authored Mar 30, 2021
2 parents 199ecaa + 4fe54cf commit 0dc95a9
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if RUBY_VERSION >= '2.7'
gem 'thwait'
end

gem 'rails', '6.1.1'
gem 'rails', '6.1.3.1'
gem 'rake', '13.0.3'
gem 'pg', '>= 0.18', '< 2.0'
gem 'puma', '4.3.7'
Expand Down
130 changes: 64 additions & 66 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,60 +9,60 @@ GIT
GEM
remote: https://rubygems.org/
specs:
actioncable (6.1.1)
actionpack (= 6.1.1)
activesupport (= 6.1.1)
actioncable (6.1.3.1)
actionpack (= 6.1.3.1)
activesupport (= 6.1.3.1)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.1)
actionpack (= 6.1.1)
activejob (= 6.1.1)
activerecord (= 6.1.1)
activestorage (= 6.1.1)
activesupport (= 6.1.1)
actionmailbox (6.1.3.1)
actionpack (= 6.1.3.1)
activejob (= 6.1.3.1)
activerecord (= 6.1.3.1)
activestorage (= 6.1.3.1)
activesupport (= 6.1.3.1)
mail (>= 2.7.1)
actionmailer (6.1.1)
actionpack (= 6.1.1)
actionview (= 6.1.1)
activejob (= 6.1.1)
activesupport (= 6.1.1)
actionmailer (6.1.3.1)
actionpack (= 6.1.3.1)
actionview (= 6.1.3.1)
activejob (= 6.1.3.1)
activesupport (= 6.1.3.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (6.1.1)
actionview (= 6.1.1)
activesupport (= 6.1.1)
actionpack (6.1.3.1)
actionview (= 6.1.3.1)
activesupport (= 6.1.3.1)
rack (~> 2.0, >= 2.0.9)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actiontext (6.1.1)
actionpack (= 6.1.1)
activerecord (= 6.1.1)
activestorage (= 6.1.1)
activesupport (= 6.1.1)
actiontext (6.1.3.1)
actionpack (= 6.1.3.1)
activerecord (= 6.1.3.1)
activestorage (= 6.1.3.1)
activesupport (= 6.1.3.1)
nokogiri (>= 1.8.5)
actionview (6.1.1)
activesupport (= 6.1.1)
actionview (6.1.3.1)
activesupport (= 6.1.3.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activejob (6.1.1)
activesupport (= 6.1.1)
activejob (6.1.3.1)
activesupport (= 6.1.3.1)
globalid (>= 0.3.6)
activemodel (6.1.1)
activesupport (= 6.1.1)
activerecord (6.1.1)
activemodel (= 6.1.1)
activesupport (= 6.1.1)
activestorage (6.1.1)
actionpack (= 6.1.1)
activejob (= 6.1.1)
activerecord (= 6.1.1)
activesupport (= 6.1.1)
marcel (~> 0.3.1)
mimemagic (~> 0.3.2)
activesupport (6.1.1)
activemodel (6.1.3.1)
activesupport (= 6.1.3.1)
activerecord (6.1.3.1)
activemodel (= 6.1.3.1)
activesupport (= 6.1.3.1)
activestorage (6.1.3.1)
actionpack (= 6.1.3.1)
activejob (= 6.1.3.1)
activerecord (= 6.1.3.1)
activesupport (= 6.1.3.1)
marcel (~> 1.0.0)
mini_mime (~> 1.0.2)
activesupport (6.1.3.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
Expand Down Expand Up @@ -175,7 +175,7 @@ GEM
rspec (>= 2.99.0, < 4.0)
hashdiff (1.0.1)
htmlentities (4.3.4)
i18n (1.8.7)
i18n (1.8.9)
concurrent-ruby (~> 1.0)
i18n_data (0.10.0)
inky-rb (1.3.8.0)
Expand All @@ -192,28 +192,26 @@ GEM
listen (3.4.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.8.0)
loofah (2.9.0)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
lumberjack (1.2.8)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.3)
mimemagic (~> 0.3.2)
marcel (1.0.0)
maruku (0.7.3)
method_source (1.0.0)
mimemagic (0.3.5)
mini_mime (1.0.2)
mini_mime (1.0.3)
mini_portile2 (2.5.0)
mini_racer (0.3.1)
libv8 (~> 8.4.255)
minitest (5.14.3)
minitest (5.14.4)
msgpack (1.4.2)
multi_json (1.15.0)
multipart-post (2.1.1)
nenv (0.3.0)
nio4r (2.5.4)
nokogiri (1.11.1)
nio4r (2.5.7)
nokogiri (1.11.2)
mini_portile2 (~> 2.5.0)
racc (~> 1.4)
notiffany (0.1.3)
Expand Down Expand Up @@ -248,29 +246,29 @@ GEM
rack
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (6.1.1)
actioncable (= 6.1.1)
actionmailbox (= 6.1.1)
actionmailer (= 6.1.1)
actionpack (= 6.1.1)
actiontext (= 6.1.1)
actionview (= 6.1.1)
activejob (= 6.1.1)
activemodel (= 6.1.1)
activerecord (= 6.1.1)
activestorage (= 6.1.1)
activesupport (= 6.1.1)
rails (6.1.3.1)
actioncable (= 6.1.3.1)
actionmailbox (= 6.1.3.1)
actionmailer (= 6.1.3.1)
actionpack (= 6.1.3.1)
actiontext (= 6.1.3.1)
actionview (= 6.1.3.1)
activejob (= 6.1.3.1)
activemodel (= 6.1.3.1)
activerecord (= 6.1.3.1)
activestorage (= 6.1.3.1)
activesupport (= 6.1.3.1)
bundler (>= 1.15.0)
railties (= 6.1.1)
railties (= 6.1.3.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.3.0)
loofah (~> 2.3)
railties (6.1.1)
actionpack (= 6.1.1)
activesupport (= 6.1.1)
railties (6.1.3.1)
actionpack (= 6.1.3.1)
activesupport (= 6.1.3.1)
method_source
rake (>= 0.8.7)
thor (~> 1.0)
Expand Down Expand Up @@ -402,7 +400,7 @@ GEM
dante (>= 0.2.0)
multi_json (~> 1.0)
stripe (> 5, < 6)
thor (1.0.1)
thor (1.1.0)
thwait (0.2.0)
e2mmap
tilt (2.0.10)
Expand Down Expand Up @@ -473,7 +471,7 @@ DEPENDENCIES
pry-byebug (~> 3.9.0)
puma (= 4.3.7)
rack-cors (= 1.1.1)
rails (= 6.1.1)
rails (= 6.1.3.1)
rake (= 13.0.3)
react_on_rails (~> 11.3)
recaptcha (~> 5.2, >= 5.2.1)
Expand Down
16 changes: 13 additions & 3 deletions app/services/discourse_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ def invite_user
end

def create_user
user_fields = {}
USER_FIELDS_MAP.each { |key, sym| user_fields[key] = user.custom_fields.fetch(sym, "") }

password = SecureRandom.hex(rand(24...32))

client.create_user({
Expand All @@ -43,6 +40,19 @@ def create_user
})
end

def user_fields
user_fields_hash = {}
USER_FIELDS_MAP.each { |key, sym| user_fields_hash[key] = user.custom_fields.fetch(sym, "") }

user_fields_hash
end

def update_user
client.update_user(user.username, {
user_fields: user_fields
})&.body
end

# This is used to create an email login link
def create_email_token
client.post("/u/email-token.json", {login: user.email})
Expand Down
2 changes: 1 addition & 1 deletion cypress/integration/membership.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe('Membership Spec', () => {
// assert thank you screen
cy.contains(
'Thank you for joining! You will receive an email with instructions to activate your account.',
{ timeout: 10000 }
{ timeout: 15000 }
)
})
})
Expand Down
63 changes: 63 additions & 0 deletions spec/services/discourse_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,67 @@
WebMock.disable_net_connect!
end
end

describe ".update_user" do
it "updates a user profile" do
custom_fields = {
address_state: Faker::Address.state,
address_city: Faker::Address.city,
address_country_code: Faker::Address.country_code,
address_line1: Faker::Address.street_address,
address_zip: Faker::Address.zip_code,
phone_number: Faker::PhoneNumber.phone_number
}

user = FactoryBot.create(:user, custom_fields: custom_fields, username: "orlando", external_id: 1)

discourse = DiscourseService.new(user)

response = {
"success" => "OK",
"user" => {
"id" => 1,
"username" => user.username,
"name" => nil,
"avatar_template" => "/user_avatar/localhost/orlando/{size}/1_2.png",
"last_posted_at" => "2021-03-10T23:29:24.167Z",
"last_seen_at" => "2021-03-30T20:49:51.686Z",
"created_at" => "2021-03-08T21:53:49.518Z",
"ignored" => false,
"muted" => false,
"can_ignore_user" => false
}
}

stub_discourse_request(:put, "u/#{user.username}")
.to_return(status: 200, body: response.to_json, headers: {"Content-Type": "application/json"})

response = discourse.update_user

expect(response["success"]).to eq("OK")
end

xit "updates a user profile against the API" do
custom_fields = {
address_state: Faker::Address.state,
address_city: Faker::Address.city,
address_country_code: Faker::Address.country_code,
address_line1: Faker::Address.street_address,
address_zip: Faker::Address.zip_code,
phone_number: Faker::PhoneNumber.phone_number
}

user = FactoryBot.create(:user, custom_fields: custom_fields, external_id: 1, username: "orlando")

WebMock.allow_net_connect!

discourse = DiscourseService.new(user)

response = discourse.update_user

expect(response["success"]).to eq("OK")

WebMock.disable_net_connect!
end
end
end

0 comments on commit 0dc95a9

Please sign in to comment.