- grant / revoke moderation (#182)

  • grant / revoke moderation (#182)

  • tests

diff --git a/lib/discourse_api/api/users.rb b/lib/discourse_api/api/users.rb
index 52a814b..da9b39d 100644
--- a/lib/discourse_api/api/users.rb
+++ b/lib/discourse_api/api/users.rb
@@ -80,6 +80,15 @@ module DiscourseApi
         response[:body]
       end
 
+      def grant_moderation(user_id)
+        response = put("admin/users/#{user_id}/grant_moderation")
+        response[:body]
+      end
+
+      def revoke_moderation(user_id)
+        put("admin/users/#{user_id}/revoke_moderation")
+      end
+
       def by_external_id(external_id)
         response = get("/users/by-external/#{external_id}")
         response[:body]['user']
diff --git a/spec/discourse_api/api/users_spec.rb b/spec/discourse_api/api/users_spec.rb
index 3618c80..43c9321 100644
--- a/spec/discourse_api/api/users_spec.rb
+++ b/spec/discourse_api/api/users_spec.rb
@@ -226,6 +226,41 @@ describe DiscourseApi::API::Users do
     end
   end
 
+  describe "#grant moderation" do
+    before do
+      url = "http://localhost:3000/admin/users/11/grant_moderation"
+      stub_put(url).to_return(body: fixture("user_grant_moderator.json"),
+                              headers: { content_type: "application/json" })
+    end
+
+    it "makes the correct put request" do
+      subject.grant_moderation(11)
+      url = "http://localhost:3000/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)
+      expect(result).to be_a Hash
+      expect(result['admin_user']['moderator']).to eq(true)
+    end
+  end
+
+  describe "#revoke moderation" do
+    before do
+      url = "http://localhost:3000/admin/users/11/revoke_moderation"
+      stub_put(url).to_return(body: '', status: 200)
+    end
+
+    it "makes the correct put request" do
+      result = subject.revoke_moderation(11)
+      url = "http://localhost:3000/admin/users/11/revoke_moderation"
+      expect(a_put(url)).to have_been_made
+      expect(result.status).to eq(200)
+    end
+
+  end
+
   describe "#by_external_id" do
     before do
       stub_get("http://localhost:3000/users/by-external/1").to_return(body: fixture("user.json"), headers: { content_type: "application/json" })
diff --git a/spec/fixtures/user_grant_moderator.json b/spec/fixtures/user_grant_moderator.json
new file mode 100644
index 0000000..d74b1cf
--- /dev/null
+++ b/spec/fixtures/user_grant_moderator.json
@@ -0,0 +1,37 @@
+{
+    "admin_user": {
+        "id": 11,
+        "username": "dave4",
+        "uploaded_avatar_id": null,
+        "avatar_template": "/letter_avatar/dave4/{size}/2.png",
+        "active": true,
+        "admin": false,
+        "moderator": true,
+        "last_seen_at": null,
+        "last_emailed_at": "2014-12-31T17:11:51.189Z",
+        "created_at": "2014-12-08T14:32:29.906Z",
+        "last_seen_age": null,
+        "last_emailed_age": "20d",
+        "created_at_age": "43d",
+        "username_lower": "dave4",
+        "trust_level": 0,
+        "trust_level_locked": false,
+        "flag_level": 0,
+        "title": null,
+        "suspended_at": null,
+        "suspended_till": null,
+        "suspended": null,
+        "blocked": false,
+        "time_read": "< 1m",
+        "days_visited": 0,
+        "posts_read_count": 0,
+        "topics_entered": 0,
+        "post_count": 0,
+        "can_send_activation_email": true,
+        "can_activate": false,
+        "can_deactivate": false,
+        "ip_address": "127.0.0.1",
+        "registration_ip_address": "127.0.0.1",
+        "single_sign_on_record": null
+    }
+}

GitHub sha: 0c9dfd06

1 Like