diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7defc0a..2f75c4c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,12 +10,12 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup ruby uses: ruby/setup-ruby@v1 with: - ruby-version: '3.2' + ruby-version: "3.3" bundler-cache: true - name: Rubocop @@ -32,10 +32,10 @@ jobs: strategy: matrix: - ruby: ['2.7', '3.0', '3.1', '3.2'] + ruby: ["2.7", "3.0", "3.1", "3.2", "3.3"] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup ruby uses: ruby/setup-ruby@v1 @@ -52,10 +52,10 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Release Gem - uses: discourse/publish-rubygems-action@v2 + uses: discourse/publish-rubygems-action@v3 env: RUBYGEMS_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }} GIT_EMAIL: team@discourse.org diff --git a/.rubocop.yml b/.rubocop.yml index fb14dfa..a746f0c 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -1,2 +1,5 @@ inherit_gem: rubocop-discourse: stree-compat.yml + +Discourse/Plugins: + Enabled: false diff --git a/.streerc b/.streerc index cc0be49..5c47737 100644 --- a/.streerc +++ b/.streerc @@ -1,2 +1,2 @@ --print-width=100 ---plugins=plugin/trailing_comma,disable_ternary +--plugins=plugin/trailing_comma,plugin/disable_auto_ternary diff --git a/discourse_api.gemspec b/discourse_api.gemspec index 57b3f65..6ae4bae 100644 --- a/discourse_api.gemspec +++ b/discourse_api.gemspec @@ -1,39 +1,44 @@ # frozen_string_literal: true -lib = File.expand_path('lib', __dir__) -$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) -require 'discourse_api/version' + +lib = File.expand_path("lib", __dir__) +$LOAD_PATH.unshift(lib) if !$LOAD_PATH.include?(lib) +require "discourse_api/version" Gem::Specification.new do |spec| - spec.name = 'discourse_api' - spec.version = DiscourseApi::VERSION - spec.authors = ['Sam Saffron', 'John Paul Ashenfelter', 'Michael Herold', 'Blake Erickson'] - spec.email = ['sam.saffron@gmail.com', 'john@ashenfelter.com', 'michael.j.herold@gmail.com', 'o.blakeerickson@gmail.com'] - spec.description = 'Discourse API' - spec.summary = 'Allows access to the Discourse API' - spec.homepage = 'http://github.com/discourse/discourse_api' - spec.license = 'MIT' + spec.name = "discourse_api" + spec.version = DiscourseApi::VERSION + spec.authors = ["Sam Saffron", "John Paul Ashenfelter", "Michael Herold", "Blake Erickson"] + spec.email = %w[ + sam.saffron@gmail.com + john@ashenfelter.com + michael.j.herold@gmail.com + o.blakeerickson@gmail.com + ] + spec.description = "Discourse API" + spec.summary = "Allows access to the Discourse API" + spec.homepage = "http://github.com/discourse/discourse_api" + spec.license = "MIT" - spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) - spec.require_paths = ['lib'] + spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR) + spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } + spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + spec.require_paths = ["lib"] - spec.add_runtime_dependency 'faraday', '~> 2.7' - spec.add_runtime_dependency 'faraday-follow_redirects' - spec.add_runtime_dependency 'faraday-multipart' - spec.add_runtime_dependency 'rack', '>= 1.6' + spec.add_runtime_dependency "faraday", "~> 2.7" + spec.add_runtime_dependency "faraday-follow_redirects" + spec.add_runtime_dependency "faraday-multipart" + spec.add_runtime_dependency "rack", ">= 1.6" - spec.add_development_dependency 'bundler', '~> 2.0' - spec.add_development_dependency 'guard', '~> 2.14' - spec.add_development_dependency 'guard-rspec', '~> 4.7' - spec.add_development_dependency 'rake', '>= 12.3.3' - spec.add_development_dependency 'rb-inotify', '~> 0.9' - spec.add_development_dependency 'rspec', '~> 3.4' - spec.add_development_dependency 'simplecov', '~> 0.11' - spec.add_development_dependency 'webmock', '~> 3.0' - spec.add_development_dependency 'rubocop-discourse', '~> 3.2.0' - spec.add_development_dependency 'syntax_tree', '~> 6.1.1' - spec.add_development_dependency 'syntax_tree-disable_ternary', '~> 1.0.0' + spec.add_development_dependency "bundler", "~> 2.0" + spec.add_development_dependency "guard", "~> 2.14" + spec.add_development_dependency "guard-rspec", "~> 4.7" + spec.add_development_dependency "rake", ">= 12.3.3" + spec.add_development_dependency "rb-inotify", "~> 0.9" + spec.add_development_dependency "rspec", "~> 3.4" + spec.add_development_dependency "simplecov", "~> 0.11" + spec.add_development_dependency "webmock", "~> 3.0" + spec.add_development_dependency "rubocop-discourse", "= 3.8.1" + spec.add_development_dependency "syntax_tree", "~> 6.2.0" - spec.required_ruby_version = '>= 2.7.0' + spec.required_ruby_version = ">= 2.7.0" end diff --git a/spec/discourse_api/api/api_key_spec.rb b/spec/discourse_api/api/api_key_spec.rb index 846bf52..b60047f 100644 --- a/spec/discourse_api/api/api_key_spec.rb +++ b/spec/discourse_api/api/api_key_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::ApiKey do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#list_api_keys" do before do @@ -16,13 +16,13 @@ end it "requests the correct resource" do - subject.list_api_keys + client.list_api_keys url = "#{host}/admin/api/keys" expect(a_get(url)).to have_been_made end it "returns the requested api keys" do - keys = subject.list_api_keys + keys = client.list_api_keys expect(keys["keys"]).to be_an Array expect(keys["keys"].first).to be_a Hash expect(keys["keys"].first).to have_key("key") @@ -41,13 +41,13 @@ end it "requests the correct resource" do - subject.create_api_key(key: { username: "robin" }) + client.create_api_key(key: { username: "robin" }) url = "#{host}/admin/api/keys" expect(a_post(url)).to have_been_made end it "returns the generated api key" do - api_key = subject.create_api_key(key: { username: "robin" }) + api_key = client.create_api_key(key: { username: "robin" }) expect(api_key).to be_a Hash expect(api_key["key"]).to have_key("key") end @@ -65,13 +65,13 @@ end it "requests the correct resource" do - subject.revoke_api_key(10) + client.revoke_api_key(10) url = "#{host}/admin/api/keys/10/revoke" expect(a_post(url)).to have_been_made end it "returns the api key" do - api_key = subject.revoke_api_key(10) + api_key = client.revoke_api_key(10) expect(api_key["key"]).to have_key("key") end end @@ -88,13 +88,13 @@ end it "requests the correct resource" do - subject.undo_revoke_api_key(10) + client.undo_revoke_api_key(10) url = "#{host}/admin/api/keys/10/undo-revoke" expect(a_post(url)).to have_been_made end it "returns the api key" do - api_key = subject.undo_revoke_api_key(10) + api_key = client.undo_revoke_api_key(10) expect(api_key["key"]).to have_key("key") end end @@ -111,13 +111,13 @@ end it "requests the correct resource" do - subject.delete_api_key(10) + client.delete_api_key(10) url = "#{host}/admin/api/keys/10" expect(a_delete(url)).to have_been_made end it "returns 200" do - response = subject.delete_api_key(10) + response = client.delete_api_key(10) expect(response["status"]).to eq(200) end end diff --git a/spec/discourse_api/api/backups_spec.rb b/spec/discourse_api/api/backups_spec.rb index 319a76c..774ebd0 100644 --- a/spec/discourse_api/api/backups_spec.rb +++ b/spec/discourse_api/api/backups_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Backups do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#backups" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.backups + client.backups expect(a_get("#{host}/admin/backups.json")).to have_been_made end it "returns the requested backups" do - backups = subject.backups + backups = client.backups expect(backups).to be_an Array expect(backups.first).to be_a Hash expect(backups.first).to have_key("filename") diff --git a/spec/discourse_api/api/badges_spec.rb b/spec/discourse_api/api/badges_spec.rb index ee25ba8..731fec4 100644 --- a/spec/discourse_api/api/badges_spec.rb +++ b/spec/discourse_api/api/badges_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Badges do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#badges" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.badges + client.badges expect(a_get("#{host}/admin/badges.json")).to have_been_made end it "returns the requested badges" do - badges = subject.badges + badges = client.badges expect(badges).to be_a Hash expect(badges["badges"]).to be_an Array end @@ -37,12 +37,12 @@ end it "requests the correct resource" do - subject.user_badges("test_user") + client.user_badges("test_user") expect(a_get("#{host}/user-badges/test_user.json")).to have_been_made end it "returns the requested user badges" do - badges = subject.user_badges("test_user") + badges = client.user_badges("test_user") expect(badges).to be_an Array expect(badges.first).to be_a Hash expect(badges.first).to have_key("badge_type_id") diff --git a/spec/discourse_api/api/categories_spec.rb b/spec/discourse_api/api/categories_spec.rb index 6638120..3fdc6cb 100644 --- a/spec/discourse_api/api/categories_spec.rb +++ b/spec/discourse_api/api/categories_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Categories do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#categories" do before do @@ -15,18 +15,18 @@ end it "requests the correct resource" do - subject.categories + client.categories expect(a_get("#{host}/categories.json")).to have_been_made end it "returns the requested categories" do - categories = subject.categories + categories = client.categories expect(categories).to be_an Array expect(categories.first).to be_a Hash end it "returns the requested categories with hash arg" do - categories = subject.categories({}) + categories = client.categories({}) expect(categories).to be_an Array expect(categories.first).to be_a Hash end @@ -43,12 +43,12 @@ end it "requests the correct resource" do - subject.categories + client.categories expect(a_get("#{host}/categories.json")).to have_been_made end it "returns the entire categories response" do - categories = subject.categories_full + categories = client.categories_full expect(categories).to be_a Hash expect(categories).to have_key "category_list" expect(categories).to have_key "featured_users" @@ -66,7 +66,7 @@ end it "returns the latest topics in a category" do - latest_topics = subject.category_latest_topics(category_slug: "category-slug") + latest_topics = client.category_latest_topics(category_slug: "category-slug") expect(latest_topics).to be_an Array end end @@ -82,7 +82,7 @@ end it "returns the entire latest topics in a category response" do - latest_topics = subject.category_latest_topics_full(category_slug: "category-slug") + latest_topics = client.category_latest_topics_full(category_slug: "category-slug") expect(latest_topics).to be_a Hash expect(latest_topics).to have_key "topic_list" expect(latest_topics).to have_key "users" @@ -100,7 +100,7 @@ end it "returns the top topics in a category" do - topics = subject.category_top_topics("category-slug") + topics = client.category_top_topics("category-slug") expect(topics).to be_an Array end end @@ -116,7 +116,7 @@ end it "returns the entire top topics in a category response" do - topics = subject.category_top_topics_full("category-slug") + topics = client.category_top_topics_full("category-slug") expect(topics).to be_a Hash expect(topics).to have_key "topic_list" expect(topics).to have_key "users" @@ -134,7 +134,7 @@ end it "returns the new topics in a category" do - topics = subject.category_new_topics("category-slug") + topics = client.category_new_topics("category-slug") expect(topics).to be_an Array end end @@ -150,7 +150,7 @@ end it "returns the new topics in a category" do - topics = subject.category_new_topics_full("category-slug") + topics = client.category_new_topics_full("category-slug") expect(topics).to be_a Hash expect(topics).to have_key "topic_list" expect(topics).to have_key "users" @@ -160,7 +160,7 @@ describe "#category_new_category" do before do stub_post("#{host}/categories") - subject.create_category( + client.create_category( name: "test_category", color: "283890", text_color: "FFFFFF", @@ -194,7 +194,7 @@ it "makes a categories reordering request" do payload = { "1": 2, "2": 3, "3": 4, "4": 1 } - response = subject.reorder_categories(mapping: payload.to_json) + response = client.reorder_categories(mapping: payload.to_json) expect( a_post("#{host}/categories/reorder").with( body: "mapping=#{CGI.escape(payload.to_json.to_s)}", @@ -215,7 +215,7 @@ end it "makes the post request" do - response = subject.category_set_user_notification(id: 1, notification_level: 3) + response = client.category_set_user_notification(id: 1, notification_level: 3) expect(a_post("#{host}/category/1/notifications")).to have_been_made expect(response["success"]).to eq("OK") end @@ -232,7 +232,7 @@ end it "makes the post request" do - response = subject.category_set_user_notification_level(1, notification_level: 3) + response = client.category_set_user_notification_level(1, notification_level: 3) expect( a_post("#{host}/category/1/notifications").with(body: "notification_level=3"), ).to have_been_made diff --git a/spec/discourse_api/api/email_spec.rb b/spec/discourse_api/api/email_spec.rb index cc14ef4..5f62b9d 100644 --- a/spec/discourse_api/api/email_spec.rb +++ b/spec/discourse_api/api/email_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Email do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#email_settings" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.email_settings + client.email_settings expect(a_get("#{host}/admin/email.json")).to have_been_made end it "returns the requested settings" do - settings = subject.email_settings + settings = client.email_settings expect(settings).to be_a Hash expect(settings).to have_key("delivery_method") expect(settings).to have_key("settings") @@ -38,12 +38,12 @@ end it "requests the correct resource" do - subject.list_email("all") + client.list_email("all") expect(a_get("#{host}/admin/email/all.json")).to have_been_made end it "returns all email" do - all_email = subject.list_email("all") + all_email = client.list_email("all") expect(all_email).to be_an Array end end diff --git a/spec/discourse_api/api/groups_spec.rb b/spec/discourse_api/api/groups_spec.rb index 13cd7b9..a12308b 100644 --- a/spec/discourse_api/api/groups_spec.rb +++ b/spec/discourse_api/api/groups_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Groups do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#groups" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.groups + client.groups expect(a_get("#{host}/groups.json")).to have_been_made end it "returns the requested groups" do - groups = subject.groups + groups = client.groups expect(groups).to be_an Array groups.each { |g| expect(g).to be_a Hash } end @@ -32,20 +32,20 @@ content_type: "application/json", }, ) - group = subject.group("some-group") + group = client.group("some-group") expect(group["basic_group"]).to be_a Hash end it "create new groups" do stub_post("#{host}/admin/groups") - subject.create_group(name: "test_group") + client.create_group(name: "test_group") params = escape_params("group[name]" => "test_group", "group[visibility_level]" => 0) expect(a_post("#{host}/admin/groups").with(body: params)).to have_been_made end it "update an existing group" do stub_put("#{host}/groups/42") - subject.update_group(42, name: "test_group") + client.update_group(42, name: "test_group") params = escape_params("group[name]" => "test_group", "group[visibility_level]" => 0) expect(a_put("#{host}/groups/42").with(body: params)).to have_been_made end @@ -54,14 +54,14 @@ before { stub_request(:put, "#{host}/admin/groups/123/members.json") } it "adds a single member by username" do - subject.group_add(123, username: "sam") + client.group_add(123, username: "sam") expect( a_request(:put, "#{host}/admin/groups/123/members.json").with(body: { usernames: "sam" }), ).to have_been_made end it "adds an array of members by username" do - subject.group_add(123, usernames: %w[sam jeff]) + client.group_add(123, usernames: %w[sam jeff]) expect( a_request(:put, "#{host}/admin/groups/123/members.json").with( body: { @@ -72,14 +72,14 @@ end it "adds a single member by user_id" do - subject.group_add(123, user_id: 456) + client.group_add(123, user_id: 456) expect( a_request(:put, "#{host}/admin/groups/123/members.json").with(body: { user_ids: "456" }), ).to have_been_made end it "adds an array of members by user_id" do - subject.group_add(123, user_id: [123, 456]) + client.group_add(123, user_id: [123, 456]) expect( a_request(:put, "#{host}/admin/groups/123/members.json").with( body: { @@ -96,7 +96,7 @@ before { stub_delete(url) } it "removes member" do - subject.group_remove(123, username: "sam") + client.group_remove(123, username: "sam") expect(a_delete(url)).to have_been_made end end @@ -107,7 +107,7 @@ before { stub_put(url) } it "makes the member an owner" do - subject.group_add_owners(123, usernames: "sam") + client.group_add_owners(123, usernames: "sam") params = escape_params("group[usernames]" => "sam") expect( a_request(:put, "#{host}/admin/groups/123/owners.json").with(body: params), @@ -121,7 +121,7 @@ before { stub_delete(url) } it "removes the owner role from the group member" do - subject.group_remove_owners(123, usernames: "sam") + client.group_remove_owners(123, usernames: "sam") expect(a_delete(url)).to have_been_made end end @@ -140,10 +140,10 @@ content_type: "application/json", }, ) - members = subject.group_members("mygroup") + members = client.group_members("mygroup") expect(a_get("#{host}/groups/mygroup/members.json?limit=100&offset=0")).to have_been_made expect(members.length).to eq(100) - members = subject.group_members("mygroup", offset: 100) + members = client.group_members("mygroup", offset: 100) expect(a_get("#{host}/groups/mygroup/members.json?limit=100&offset=100")).to have_been_made expect(members.length).to eq(90) end @@ -156,7 +156,7 @@ content_type: "application/json", }, ) - member_data = subject.group_members("mygroup", all: true) + member_data = client.group_members("mygroup", all: true) expect(a_get("#{host}/groups/mygroup/members.json?limit=100&offset=0")).to have_been_made expect(member_data["members"].length).to eq(100) expect(member_data["owners"].length).to eq(7) @@ -169,7 +169,7 @@ before { stub_post("#{host}/groups/mygroup/notifications?user_id=77¬ification_level=3") } it "updates user's notification level for group" do - subject.group_set_user_notification_level("mygroup", 77, 3) + client.group_set_user_notification_level("mygroup", 77, 3) expect( a_post("#{host}/groups/mygroup/notifications?user_id=77¬ification_level=3"), ).to have_been_made diff --git a/spec/discourse_api/api/invite_spec.rb b/spec/discourse_api/api/invite_spec.rb index ed9fcfe..82708b3 100644 --- a/spec/discourse_api/api/invite_spec.rb +++ b/spec/discourse_api/api/invite_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Invite do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#invite_user" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.invite_user(email: "fake_user@example.com", group_ids: "41,42") + client.invite_user(email: "fake_user@example.com", group_ids: "41,42") expect(a_post("#{host}/invites")).to have_been_made end it "returns success" do - response = subject.invite_user(email: "fake_user@example.com", group_ids: "41,42") + response = client.invite_user(email: "fake_user@example.com", group_ids: "41,42") expect(response).to be_a Hash expect(response["success"]).to be_truthy end @@ -37,7 +37,7 @@ end it "updates invite" do - subject.update_invite(27, email: "namee@example.com") + client.update_invite(27, email: "namee@example.com") expect(a_put("#{host}/invites/27")).to have_been_made end end @@ -53,17 +53,17 @@ end it "requests the correct resource" do - subject.retrieve_invite(email: "foo@bar.com") + client.retrieve_invite(email: "foo@bar.com") expect(a_get("#{host}/invites/retrieve.json?email=foo@bar.com")).to have_been_made end it "returns the requested topics" do - invites = subject.retrieve_invite(email: "foo@bar.com") + invites = client.retrieve_invite(email: "foo@bar.com") expect(invites).to be_an Hash end it "returns the requested invite" do - invites = subject.retrieve_invite(email: "foo@bar.com") + invites = client.retrieve_invite(email: "foo@bar.com") expect(invites["email"]).to eq("foo@bar.com") expect(invites).to have_key("invite_key") end @@ -82,7 +82,7 @@ end it "destroys all expired invites" do - subject.destroy_all_expired_invites + client.destroy_all_expired_invites expect(a_post(url)).to have_been_made end end @@ -100,7 +100,7 @@ end it "resends all invites" do - subject.resend_all_invites + client.resend_all_invites expect(a_post(url)).to have_been_made end end @@ -118,7 +118,7 @@ end it "resends invite" do - subject.resend_invite("foo@bar.com") + client.resend_invite("foo@bar.com") expect(a_post(url)).to have_been_made end end @@ -136,7 +136,7 @@ end it "destroys the invite" do - subject.destroy_invite(27) + client.destroy_invite(27) expect(a_delete(url)).to have_been_made end end diff --git a/spec/discourse_api/api/notifications_spec.rb b/spec/discourse_api/api/notifications_spec.rb index 8089f17..d986546 100644 --- a/spec/discourse_api/api/notifications_spec.rb +++ b/spec/discourse_api/api/notifications_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Notifications do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#notifications" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.notifications + client.notifications expect(a_get("#{host}/notifications.json")).to have_been_made end it "returns the requested notifications" do - notifications = subject.notifications + notifications = client.notifications expect(notifications).to be_an Array expect(notifications.first).to be_an Hash expect(notifications[0]["notification_type"]).to eq(9) diff --git a/spec/discourse_api/api/polls_spec.rb b/spec/discourse_api/api/polls_spec.rb index df996a9..c581eb9 100644 --- a/spec/discourse_api/api/polls_spec.rb +++ b/spec/discourse_api/api/polls_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Polls do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#poll vote" do before do @@ -17,13 +17,13 @@ it "requests the correct resource" do options = ["8b4736b1ae3dfb5a28088530f036f9e5"] - subject.poll_vote post_id: 5, poll_name: "poll", options: options + client.poll_vote post_id: 5, poll_name: "poll", options: options expect(a_put("#{host}/polls/vote")).to have_been_made end it "returns the expected votes" do options = ["8b4736b1ae3dfb5a28088530f036f9e5"] - vote = subject.poll_vote post_id: 5, poll_name: "poll", options: options + vote = client.poll_vote post_id: 5, poll_name: "poll", options: options expect(vote.body).to be_a Hash expect(vote.body["poll"]["options"]).to be_an Array expect(vote.body["vote"]).to eq(["8b4736b1ae3dfb5a28088530f036f9e5"]) @@ -41,13 +41,13 @@ end it "toggles the poll status to closed" do - subject.toggle_poll_status post_id: 5, poll_name: "poll", status: "closed" + client.toggle_poll_status post_id: 5, poll_name: "poll", status: "closed" expect(a_put("#{host}/polls/toggle_status")).to have_been_made end it "returns the expected results of closed poll" do returned_poll_status = - subject.toggle_poll_status post_id: 5, poll_name: "poll", status: "closed" + client.toggle_poll_status post_id: 5, poll_name: "poll", status: "closed" expect(returned_poll_status.body).to be_a Hash returned_poll_status.body["poll"]["options"].each { |g| expect(g).to be_a Hash } end @@ -64,12 +64,12 @@ end it "requests the correct resource" do - subject.poll_voters post_id: 5, poll_name: "poll" + client.poll_voters post_id: 5, poll_name: "poll" expect(a_get("#{host}/polls/voters.json?post_id=5&poll_name=poll")).to have_been_made end it "returns the expected votes" do - voters = subject.poll_voters post_id: 5, poll_name: "poll" + voters = client.poll_voters post_id: 5, poll_name: "poll" expect(voters).to be_a Hash voters.each { |g| expect(g).to be_an Array } expect(voters["voters"]["e539a9df8700d0d05c69356a07b768cf"]).to be_an Array diff --git a/spec/discourse_api/api/posts_spec.rb b/spec/discourse_api/api/posts_spec.rb index 06ea614..72f9104 100644 --- a/spec/discourse_api/api/posts_spec.rb +++ b/spec/discourse_api/api/posts_spec.rb @@ -2,9 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Posts do - let (:client) { - DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") - } + let(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#get_post" do before do diff --git a/spec/discourse_api/api/private_messages_spec.rb b/spec/discourse_api/api/private_messages_spec.rb index a0acbb3..e2d7c7b 100644 --- a/spec/discourse_api/api/private_messages_spec.rb +++ b/spec/discourse_api/api/private_messages_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::PrivateMessages do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#private_messages" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.private_messages("test_user") + client.private_messages("test_user") expect(a_get("#{host}/topics/private-messages/test_user.json")).to have_been_made end it "returns the requested private messages" do - private_messages = subject.private_messages("test_user") + private_messages = client.private_messages("test_user") expect(private_messages).to be_an Array end end @@ -36,12 +36,12 @@ end it "requests the correct resource" do - subject.sent_private_messages("test_user") + client.sent_private_messages("test_user") expect(a_get("#{host}/topics/private-messages-sent/test_user.json")).to have_been_made end it "returns the requested sent private messages" do - private_messages = subject.sent_private_messages("test_user") + private_messages = client.sent_private_messages("test_user") expect(private_messages).to be_an Array end end @@ -49,7 +49,7 @@ describe "#create_pm" do before do stub_post("#{host}/posts") - subject.create_pm( + client.create_pm( title: "Confidential: Hello World!", raw: "This is the raw markdown for my private message", target_recipients: "user1,user2", diff --git a/spec/discourse_api/api/search_spec.rb b/spec/discourse_api/api/search_spec.rb index e28614a..2346948 100644 --- a/spec/discourse_api/api/search_spec.rb +++ b/spec/discourse_api/api/search_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Search do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#search" do before do @@ -15,22 +15,22 @@ end it "requests the correct resource" do - subject.search("test") + client.search("test") expect(a_get("#{host}/search").with(query: { q: "test" })).to have_been_made end it "returns the requested search" do - results = subject.search("test") + results = client.search("test") expect(results).to be_an Array expect(results.first).to be_a Hash end it "raises an ArgumentError for nil" do - expect { subject.search(nil) }.to raise_error(ArgumentError) + expect { client.search(nil) }.to raise_error(ArgumentError) end it "raises an ArgumentError for empty string" do - expect { subject.search("") }.to raise_error(ArgumentError) + expect { client.search("") }.to raise_error(ArgumentError) end end end diff --git a/spec/discourse_api/api/site_settings_spec.rb b/spec/discourse_api/api/site_settings_spec.rb index 49d16ea..395674b 100644 --- a/spec/discourse_api/api/site_settings_spec.rb +++ b/spec/discourse_api/api/site_settings_spec.rb @@ -2,12 +2,12 @@ require "spec_helper" describe DiscourseApi::API::SiteSettings do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#site_setting_update" do before do stub_put("#{host}/admin/site_settings/foo") - subject.site_setting_update(name: "foo", value: "bar") + client.site_setting_update(name: "foo", value: "bar") end it "makes a site_settings_update request" do diff --git a/spec/discourse_api/api/sso_spec.rb b/spec/discourse_api/api/sso_spec.rb index 5084b4c..db2fa5a 100644 --- a/spec/discourse_api/api/sso_spec.rb +++ b/spec/discourse_api/api/sso_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::SSO do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } let(:params) do { @@ -47,7 +47,7 @@ it "assigns params to sso instance" do allow(DiscourseApi::SingleSignOn).to(receive(:parse_hash).with(params).and_return(sso_double)) - subject.sync_sso(params) + client.sync_sso(params) expect(sso_double.custom_fields).to eql( { "custom.field_1" => "tomato", :field_2 => "potato" }, @@ -56,7 +56,7 @@ end it "requests the correct resource" do - subject.sync_sso({ :sso_secret => "test_d7fd0429940", "custom.riffle_url" => "test" }) + client.sync_sso({ :sso_secret => "test_d7fd0429940", "custom.riffle_url" => "test" }) expect(a_post(/.*sync_sso.*/)).to have_been_made end end diff --git a/spec/discourse_api/api/topics_spec.rb b/spec/discourse_api/api/topics_spec.rb index fa5ed74..ad164b9 100644 --- a/spec/discourse_api/api/topics_spec.rb +++ b/spec/discourse_api/api/topics_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Topics do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#change_topic_status" do before do @@ -15,7 +15,7 @@ end it "changes the topic status" do - subject.update_topic_status(57, { status: "visible", enabled: false }) + client.update_topic_status(57, { status: "visible", enabled: false }) expect(a_put("#{host}/t/57/status")).to have_been_made end end @@ -31,12 +31,12 @@ end it "requests the correct resource" do - subject.invite_to_topic(12, email: "fake_user@example.com") + client.invite_to_topic(12, email: "fake_user@example.com") expect(a_post("#{host}/t/12/invite")).to have_been_made end it "returns success" do - response = subject.invite_to_topic(12, email: "fake_user@example.com") + response = client.invite_to_topic(12, email: "fake_user@example.com") expect(response).to be_a Hash expect(response["success"]).to be_truthy end @@ -53,18 +53,18 @@ end it "requests the correct resource" do - subject.latest_topics + client.latest_topics expect(a_get("#{host}/latest.json")).to have_been_made end it "returns the requested topics" do - topics = subject.latest_topics + topics = client.latest_topics expect(topics).to be_an Array expect(topics.first).to be_a Hash end it "can take a hash param" do - topics = subject.latest_topics({}) + topics = client.latest_topics({}) expect(topics).to be_an Array expect(topics.first).to be_a Hash end @@ -81,18 +81,18 @@ end it "requests the correct resource" do - subject.top_topics + client.top_topics expect(a_get("#{host}/top.json")).to have_been_made end it "returns the requested topics" do - topics = subject.top_topics + topics = client.top_topics expect(topics).to be_an Array expect(topics.first).to be_a Hash end it "can take a hash param" do - topics = subject.top_topics({}) + topics = client.top_topics({}) expect(topics).to be_an Array expect(topics.first).to be_a Hash end @@ -109,13 +109,13 @@ end it "requests the correct resource" do - subject.new_topics + client.new_topics expect(a_get("#{host}/new.json")).to have_been_made end it "returns the requested topics" do - subject.api_username = "test_user" - topics = subject.new_topics + client.api_username = "test_user" + topics = client.new_topics expect(topics).to be_an Array expect(topics.first).to be_a Hash end @@ -132,12 +132,12 @@ end it "requests the correct resource" do - subject.topic(57) + client.topic(57) expect(a_get("#{host}/t/57.json")).to have_been_made end it "returns the requested topic" do - topic = subject.topic(57) + topic = client.topic(57) expect(topic).to be_a Hash expect(topic["id"]).to eq(57) end @@ -154,12 +154,12 @@ end it "renames the topic" do - subject.rename_topic(57, "A new title!") + client.rename_topic(57, "A new title!") expect(a_put("#{host}/t/57.json")).to have_been_made end it "assigns the topic a new category" do - subject.recategorize_topic(57, 3) + client.recategorize_topic(57, 3) expect(a_put("#{host}/t/57.json")).to have_been_made end end @@ -175,12 +175,12 @@ end it "requests the correct resource" do - subject.topics_by("test") + client.topics_by("test") expect(a_get("#{host}/topics/created-by/test.json")).to have_been_made end it "returns the requested topics" do - topics = subject.topics_by("test") + topics = client.topics_by("test") expect(topics).to be_an Array expect(topics.first).to be_a Hash end @@ -197,24 +197,24 @@ end it "requests the correct resource" do - subject.topic_posts(57) + client.topic_posts(57) expect(a_get("#{host}/t/57/posts.json")).to have_been_made end it "allows scoping to specific post ids" do - subject.topic_posts(57, [123, 456]) + client.topic_posts(57, [123, 456]) expect(a_get("#{host}/t/57/posts.json?post_ids[]=123&post_ids[]=456")).to have_been_made end it "returns the requested topic posts" do - body = subject.topic_posts(57, [123]) + body = client.topic_posts(57, [123]) expect(body).to be_a Hash expect(body["post_stream"]["posts"]).to be_an Array expect(body["post_stream"]["posts"].first).to be_a Hash end it "can retrieve a topic posts' raw attribute" do - body = subject.topic_posts(57, [123], { include_raw: true }) + body = client.topic_posts(57, [123], { include_raw: true }) expect(body).to be_a Hash expect(body["post_stream"]["posts"]).to be_an Array expect(body["post_stream"]["posts"].first).to be_a Hash @@ -233,17 +233,17 @@ end it "makes the post request" do - subject.create_topic title: "Sample Topic Title", - raw: "Sample topic content body", - tags: %w[asdf fdsa] + client.create_topic title: "Sample Topic Title", + raw: "Sample topic content body", + tags: %w[asdf fdsa] expect(a_post("#{host}/posts")).to have_been_made end it "returns success" do response = - subject.create_topic title: "Sample Topic Title", - raw: "Sample topic content body", - tags: %w[asdf fdsa] + client.create_topic title: "Sample Topic Title", + raw: "Sample topic content body", + tags: %w[asdf fdsa] expect(response).to be_a Hash expect(response["topic_id"]).to eq 21 end @@ -260,7 +260,7 @@ end it "makes the post request" do - response = subject.topic_set_user_notification_level(1, notification_level: 3) + response = client.topic_set_user_notification_level(1, notification_level: 3) expect( a_post("#{host}/t/1/notifications").with(body: "notification_level=3"), ).to have_been_made @@ -279,7 +279,7 @@ end it "makes the put request" do - response = subject.bookmark_topic(1) + response = client.bookmark_topic(1) expect(a_put("#{host}/t/1/bookmark.json")).to have_been_made expect(response.body).to eq(nil) end @@ -296,7 +296,7 @@ end it "makes the put request" do - response = subject.remove_topic_bookmark(1) + response = client.remove_topic_bookmark(1) expect(a_put("#{host}/t/1/remove_bookmarks.json")).to have_been_made expect(response.body).to eq(nil) end diff --git a/spec/discourse_api/api/uploads_spec.rb b/spec/discourse_api/api/uploads_spec.rb index b97d369..6361592 100644 --- a/spec/discourse_api/api/uploads_spec.rb +++ b/spec/discourse_api/api/uploads_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Uploads do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#upload_file" do before do @@ -18,7 +18,7 @@ image = "https://meta-discourse.global.ssl.fastly.net/user_avatar/meta.discourse.org/sam/120/5243.png" args = { url: image } - response = subject.upload_file(args) + response = client.upload_file(args) expect(response["url"]).to eq( "/uploads/default/original/1X/417e624d2453925e6c68748b9aa67637c284b5aa.jpg", ) @@ -27,7 +27,7 @@ it "uploads a file" do file = Faraday::UploadIO.new("spec/fixtures/upload_file.json", "application/json") args = { file: file } - response = subject.upload_file(args) + response = client.upload_file(args) expect(response["url"]).to eq( "/uploads/default/original/1X/417e624d2453925e6c68748b9aa67637c284b5aa.jpg", ) diff --git a/spec/discourse_api/api/user_actions_spec.rb b/spec/discourse_api/api/user_actions_spec.rb index a99a0af..d0f3aa8 100644 --- a/spec/discourse_api/api/user_actions_spec.rb +++ b/spec/discourse_api/api/user_actions_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::UserActions do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#user_replies" do before do @@ -15,12 +15,12 @@ end it "requests the correct resource" do - subject.user_replies("testuser") + client.user_replies("testuser") expect(a_get("#{host}/user_actions.json?username=testuser&filter=5")).to have_been_made end it "returns the requested user" do - replies = subject.user_replies("testuser") + replies = client.user_replies("testuser") expect(replies).to be_an Array end end @@ -36,12 +36,12 @@ end it "requests the correct resource" do - subject.user_topics_and_replies("testuser") + client.user_topics_and_replies("testuser") expect(a_get("#{host}/user_actions.json?username=testuser&filter=4,5")).to have_been_made end it "returns the requested user" do - replies = subject.user_topics_and_replies("testuser") + replies = client.user_topics_and_replies("testuser") expect(replies).to be_an Array end end diff --git a/spec/discourse_api/api/users_spec.rb b/spec/discourse_api/api/users_spec.rb index 05cc367..38df760 100644 --- a/spec/discourse_api/api/users_spec.rb +++ b/spec/discourse_api/api/users_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::API::Users do - subject { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } + subject(:client) { DiscourseApi::Client.new("#{host}", "test_d7fd0429940", "test_user") } describe "#user" do before do @@ -15,17 +15,17 @@ end it "requests the correct resource" do - subject.user("test") + client.user("test") expect(a_get("#{host}/users/test.json")).to have_been_made end it "returns the requested user" do - user = subject.user("test") + user = client.user("test") expect(user).to be_a Hash end it "works with optional params" do - user = subject.user("test", {}) + user = client.user("test", {}) expect(user).to be_a Hash end end @@ -41,12 +41,12 @@ end it "requests the correct resource" do - subject.user_sso(15) + client.user_sso(15) expect(a_get("#{host}/admin/users/15.json")).to have_been_made end it "has single_sign_on_record" do - user_sso = subject.user_sso(15) + user_sso = client.user_sso(15) expect(user_sso).to be_a Hash expect(user_sso).to have_key("external_id") end @@ -72,7 +72,7 @@ sam = "https://meta-discourse.global.ssl.fastly.net/user_avatar/meta.discourse.org/sam/120/5243.png" args = { url: sam } - response = subject.update_avatar("test_user", args) + response = client.update_avatar("test_user", args) expect(response[:body]["success"]).to eq("OK") end end @@ -88,12 +88,12 @@ end it "makes the put request" do - subject.update_email("fake_user", "fake_user_2@example.com") + client.update_email("fake_user", "fake_user_2@example.com") expect(a_put("#{host}/u/fake_user/preferences/email")).to have_been_made end it "returns success" do - response = subject.update_email("fake_user", "fake_user_2@example.com") + response = client.update_email("fake_user", "fake_user_2@example.com") expect(response[:body]["success"]).to be_truthy end end @@ -109,16 +109,16 @@ end it "makes the put request" do - subject.api_key = "test_d7fd0429940" - subject.api_username = "test_user" - subject.update_user("fake_user", name: "Fake User 2") + client.api_key = "test_d7fd0429940" + client.api_username = "test_user" + client.update_user("fake_user", name: "Fake User 2") expect(a_put("#{host}/u/fake_user")).to have_been_made end it "returns success" do - subject.api_key = "test_d7fd0429940" - subject.api_username = "test_user" - response = subject.update_user("fake_user", name: "Fake User 2") + client.api_key = "test_d7fd0429940" + client.api_username = "test_user" + response = client.update_user("fake_user", name: "Fake User 2") expect(response[:body]["success"]).to be_truthy end end @@ -134,12 +134,12 @@ end it "makes the put request" do - subject.update_username("fake_user", "fake_user_2") + client.update_username("fake_user", "fake_user_2") expect(a_put("#{host}/u/fake_user/preferences/username")).to have_been_made end it "returns the updated username" do - response = subject.update_username("fake_user", "fake_user_2") + response = client.update_username("fake_user", "fake_user_2") expect(response[:body]["username"]).to eq("fake_user_2") end end @@ -155,19 +155,19 @@ end it "makes the post request" do - subject.create_user name: "Test User", - email: "test2@example.com", - password: "P@ssword", - username: "test2" + client.create_user name: "Test User", + email: "test2@example.com", + password: "P@ssword", + username: "test2" expect(a_post("#{host}/users")).to have_been_made end it "returns success" do response = - subject.create_user name: "Test User", - email: "test2@example.com", - password: "P@ssword", - username: "test2" + client.create_user name: "Test User", + email: "test2@example.com", + password: "P@ssword", + username: "test2" expect(response).to be_a Hash expect(response["success"]).to be_truthy end @@ -184,12 +184,12 @@ end it "makes the put request" do - subject.activate(15) + client.activate(15) expect(a_put("#{host}/admin/users/15/activate")).to have_been_made end it "returns success" do - response = subject.activate(15) + response = client.activate(15) expect(response[:body]["success"]).to eq("OK") end end @@ -205,12 +205,12 @@ end it "makes a post request" do - subject.log_out(4) + client.log_out(4) expect(a_post("#{host}/admin/users/4/log_out")).to have_been_made end it "returns success" do - response = subject.log_out(4) + response = client.log_out(4) expect(response).to be_a Hash expect(response["success"]).to eq("OK") end @@ -227,7 +227,7 @@ end it "Raises API Error" do - expect { subject.log_out(90) }.to raise_error DiscourseApi::NotFoundError + expect { client.log_out(90) }.to raise_error DiscourseApi::NotFoundError end end @@ -242,12 +242,12 @@ end it "requests the correct resource" do - subject.list_users("active") + client.list_users("active") expect(a_get("#{host}/admin/users/list/active.json")).to have_been_made end it "returns the requested users" do - users = subject.list_users("active") + users = client.list_users("active") expect(users).to be_an Array expect(users.first).to be_a Hash end @@ -266,14 +266,14 @@ it "makes the correct put request" do params = { level: 2 } - subject.update_trust_level(2, params) + client.update_trust_level(2, params) url = "#{host}/admin/users/2/trust_level" expect(a_put(url)).to have_been_made end it "updates the trust_level" do params = { level: 2 } - admin_user = subject.update_trust_level(2, params) + admin_user = client.update_trust_level(2, params) expect(admin_user).to be_a Hash expect(admin_user["admin_user"]).to have_key("trust_level") end @@ -291,13 +291,13 @@ end it "makes the correct put request" do - subject.grant_admin(11) + client.grant_admin(11) url = "#{host}/admin/users/11/grant_admin" expect(a_put(url)).to have_been_made end it "makes the user an admin" do - result = subject.grant_admin(11) + result = client.grant_admin(11) expect(result).to be_a Hash expect(result["admin_user"]["admin"]).to eq(true) end @@ -315,13 +315,13 @@ end it "makes the correct put request" do - subject.grant_moderation(11) + client.grant_moderation(11) url = "#{host}/admin/users/11/grant_moderation" expect(a_put(url)).to have_been_made end it "makes the user a moderator" do - result = subject.grant_moderation(11) + result = client.grant_moderation(11) expect(result).to be_a Hash expect(result["admin_user"]["moderator"]).to eq(true) end @@ -334,7 +334,7 @@ end it "makes the correct put request" do - result = subject.revoke_moderation(11) + result = client.revoke_moderation(11) url = "#{host}/admin/users/11/revoke_moderation" expect(a_put(url)).to have_been_made expect(result.status).to eq(200) @@ -352,12 +352,12 @@ end it "requests the correct resource" do - subject.by_external_id(1) + client.by_external_id(1) expect(a_get("#{host}/users/by-external/1")).to have_been_made end it "returns the requested user" do - user = subject.by_external_id(1) + user = client.by_external_id(1) expect(user["id"]).to eq 1 end end @@ -369,7 +369,7 @@ end it "makes the correct put request" do - result = subject.suspend(11, "2030-01-01", "no reason") + result = client.suspend(11, "2030-01-01", "no reason") url = "#{host}/admin/users/11/suspend" expect(a_put(url)).to have_been_made expect(result.status).to eq(200) @@ -383,7 +383,7 @@ end it "makes the correct put request" do - result = subject.unsuspend(11) + result = client.unsuspend(11) url = "#{host}/admin/users/11/unsuspend" expect(a_put(url)).to have_been_made expect(result.status).to eq(200) @@ -397,7 +397,7 @@ end it "makes the correct put request" do - result = subject.anonymize(11) + result = client.anonymize(11) url = "#{host}/admin/users/11/anonymize" expect(a_put(url)).to have_been_made expect(result.status).to eq(200) @@ -411,7 +411,7 @@ end it "makes the correct delete request" do - result = subject.delete_user(11, true) + result = client.delete_user(11, true) url = "#{host}/admin/users/11.json?delete_posts=true" expect(a_delete(url)).to have_been_made expect(result.body).to eq('{"deleted": true}') @@ -426,12 +426,12 @@ before { stub_get(url).to_return(body: body, headers: { content_type: "application/json" }) } it "requests the correct resource" do - subject.check_username("sparrow") + client.check_username("sparrow") expect(a_get(url)).to have_been_made end it "returns the result" do - result = subject.check_username("sparrow") + result = client.check_username("sparrow") expect(result["available"]).to eq false end @@ -440,12 +440,12 @@ let(:body) { '{"errors":["must only include numbers, letters, dashes, and underscores"]}' } it "escapes them" do - subject.check_username("1_[4]! @the$#?") + client.check_username("1_[4]! @the$#?") expect(a_get(url)).to have_been_made end it "returns the result" do - result = subject.check_username("1_[4]! @the$#?") + result = client.check_username("1_[4]! @the$#?") expect( result["errors"].first, ).to eq "must only include numbers, letters, dashes, and underscores" @@ -457,12 +457,12 @@ before { stub_put("#{host}/admin/users/15/deactivate").to_return(body: nil) } it "makes the put request" do - subject.deactivate(15) + client.deactivate(15) expect(a_put("#{host}/admin/users/15/deactivate")).to have_been_made end it "returns success" do - response = subject.deactivate(15) + response = client.deactivate(15) expect(response.status).to eq(200) end end diff --git a/spec/discourse_api/client_spec.rb b/spec/discourse_api/client_spec.rb index 0d927be..a8c960a 100644 --- a/spec/discourse_api/client_spec.rb +++ b/spec/discourse_api/client_spec.rb @@ -2,7 +2,7 @@ require "spec_helper" describe DiscourseApi::Client do - subject { DiscourseApi::Client.new(host) } + subject(:client) { DiscourseApi::Client.new(host) } describe ".new" do it "requires a host argument" do @@ -10,11 +10,11 @@ end it "defaults api key to nil" do - expect(subject.api_key).to be_nil + expect(client.api_key).to be_nil end it "defaults api username to nil" do - expect(subject.api_username).to be_nil + expect(client.api_username).to be_nil end it "accepts an api key argument" do @@ -31,55 +31,55 @@ describe "#timeout" do context "with a custom timeout" do it "is set to Faraday connection" do - expect(subject.send(:connection).options.timeout).to eq(30) + expect(client.send(:connection).options.timeout).to eq(30) end end context "with the default timeout" do it "is set to Faraday connection" do - subject.timeout = 25 - expect(subject.send(:connection).options.timeout).to eq(25) + client.timeout = 25 + expect(client.send(:connection).options.timeout).to eq(25) end end it "raises DiscourseApi::Timeout" do stub_get("#{host}/t/1.json").to_timeout - expect { subject.topic(1) }.to raise_error(DiscourseApi::Timeout) + expect { client.topic(1) }.to raise_error(DiscourseApi::Timeout) end end describe "#api_key" do it "is publicly accessible" do - subject.api_key = "test_d7fd0429940" - expect(subject.api_key).to eq("test_d7fd0429940") + client.api_key = "test_d7fd0429940" + expect(client.api_key).to eq("test_d7fd0429940") end end describe "#api_username" do it "is publicly accessible" do - subject.api_username = "test_user" - expect(subject.api_username).to eq("test_user") + client.api_username = "test_user" + expect(client.api_username).to eq("test_user") end end describe "#host" do it "is publicly readable" do - expect(subject.host).to eq("#{host}") + expect(client.host).to eq("#{host}") end it "is not publicly writeable" do - expect(subject).not_to respond_to(:host=) + expect(client).not_to respond_to(:host=) end end describe "#connection" do it "looks like a Faraday connection" do - expect(subject.send(:connection)).to respond_to :run_request + expect(client.send(:connection)).to respond_to :run_request end it "memorizes the connection" do - c1, c2 = subject.send(:connection), subject.send(:connection) + c1, c2 = client.send(:connection), client.send(:connection) expect(c1.object_id).to eq(c2.object_id) end end @@ -87,22 +87,22 @@ describe "#delete" do before do stub_delete("#{host}/test/delete").with(query: { deleted: "object" }) - subject.api_key = "test_d7fd0429940" - subject.api_username = "test_user" + client.api_key = "test_d7fd0429940" + client.api_username = "test_user" end it "allows custom delete requests" do - subject.delete("/test/delete", { deleted: "object" }) + client.delete("/test/delete", { deleted: "object" }) expect(a_delete("#{host}/test/delete").with(query: { deleted: "object" })).to have_been_made end context "when using a host with a subdirectory" do - subject { DiscourseApi::Client.new("#{host}/forum") } + subject(:client) { DiscourseApi::Client.new("#{host}/forum") } before { stub_delete("#{host}/forum/test/delete").with(query: { deleted: "object" }) } it "allows custom delete requests" do - subject.delete("/test/delete", { deleted: "object" }) + client.delete("/test/delete", { deleted: "object" }) expect( a_delete("#{host}/forum/test/delete").with(query: { deleted: "object" }), ).to have_been_made @@ -113,22 +113,22 @@ describe "#post" do before do stub_post("#{host}/test/post").with(body: { created: "object" }) - subject.api_key = "test_d7fd0429940" - subject.api_username = "test_user" + client.api_key = "test_d7fd0429940" + client.api_username = "test_user" end it "allows custom post requests" do - subject.post("/test/post", { created: "object" }) + client.post("/test/post", { created: "object" }) expect(a_post("#{host}/test/post").with(body: { created: "object" })).to have_been_made end context "when using a host with a subdirectory" do - subject { DiscourseApi::Client.new("#{host}/forum") } + subject(:client) { DiscourseApi::Client.new("#{host}/forum") } before { stub_post("#{host}/forum/test/post").with(body: { created: "object" }) } it "allows custom post requests" do - subject.post("/test/post", { created: "object" }) + client.post("/test/post", { created: "object" }) expect( a_post("#{host}/forum/test/post").with(body: { created: "object" }), ).to have_been_made @@ -139,22 +139,22 @@ describe "#put" do before do stub_put("#{host}/test/put").with(body: { updated: "object" }) - subject.api_key = "test_d7fd0429940" - subject.api_username = "test_user" + client.api_key = "test_d7fd0429940" + client.api_username = "test_user" end it "allows custom put requests" do - subject.put("/test/put", { updated: "object" }) + client.put("/test/put", { updated: "object" }) expect(a_put("#{host}/test/put").with(body: { updated: "object" })).to have_been_made end context "when using a host with a subdirectory" do - subject { DiscourseApi::Client.new("#{host}/forum") } + subject(:client) { DiscourseApi::Client.new("#{host}/forum") } before { stub_put("#{host}/forum/test/put").with(body: { updated: "object" }) } it "allows custom post requests" do - subject.put("/test/put", { updated: "object" }) + client.put("/test/put", { updated: "object" }) expect(a_put("#{host}/forum/test/put").with(body: { updated: "object" })).to have_been_made end end @@ -167,17 +167,17 @@ OpenStruct.new(env: { body: "error page html" }, status: 500), ) allow(Faraday).to receive(:new).and_return(connection) - expect { subject.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error + expect { client.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error end it "catches Faraday errors" do allow(Faraday).to receive(:new).and_raise(Faraday::ClientError.new("BOOM!")) - expect { subject.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error + expect { client.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error end it "catches JSON::ParserError errors" do allow(Faraday).to receive(:new).and_raise(JSON::ParserError.new("unexpected token")) - expect { subject.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error + expect { client.send(:request, :get, "/test") }.to raise_error DiscourseApi::Error end end end