Commit 54bc457e3042dee50f37913f5709bd91f9592eda

Authored by Leandro Santos
2 parents d8b7fec7 8898935c

Merge branch 'tags-api' into 'master'

Improvements to tags API

* Get environment tags
* Require authentication only to post article tags
* Tests for the whole tags API

See merge request !918
app/api/v1/tags.rb
1 1 module Api
2 2 module V1
3 3 class Tags < Grape::API
4   - before { authenticate! }
5   -
6 4 resource :articles do
7   -
8 5 resource ':id/tags' do
9   -
10 6 get do
11 7 article = find_article(environment.articles, params[:id])
12 8 present article.tag_list
... ... @@ -14,6 +10,7 @@ module Api
14 10  
15 11 desc "Add a tag to an article"
16 12 post do
  13 + authenticate!
17 14 article = find_article(environment.articles, params[:id])
18 15 article.tag_list=params[:tags]
19 16 article.save
... ... @@ -21,6 +18,13 @@ module Api
21 18 end
22 19 end
23 20 end
  21 +
  22 + resource :environment do
  23 + desc 'Return the tag counts for this environment'
  24 + get '/tags' do
  25 + present environment.tag_counts
  26 + end
  27 + end
24 28 end
25 29 end
26 30 end
... ...
test/api/environment_test.rb
... ... @@ -67,5 +67,4 @@ class EnvironmentTest &lt; ActiveSupport::TestCase
67 67 json = JSON.parse(last_response.body)
68 68 assert_equal context_env.id, json['id']
69 69 end
70   -
71 70 end
... ...
test/api/tags_test.rb 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +require_relative 'test_helper'
  2 +
  3 +class TagsTest < ActiveSupport::TestCase
  4 +
  5 + def setup
  6 + create_and_activate_user
  7 + end
  8 +
  9 + should 'get article tags' do
  10 + profile = fast_create(Profile)
  11 + a = profile.articles.create(name: 'Test')
  12 + a.tags.create! name: 'foo'
  13 +
  14 + get "/api/v1/articles/#{a.id}/tags?#{params.to_query}"
  15 + json = JSON.parse(last_response.body)
  16 + assert_equal ['foo'], json
  17 + end
  18 +
  19 + should 'post article tags' do
  20 + login_api
  21 + profile = fast_create(Profile)
  22 + a = profile.articles.create(name: 'Test')
  23 +
  24 + post "/api/v1/articles/#{a.id}/tags?#{params.to_query}&tags=foo"
  25 + assert_equal 201, last_response.status
  26 + assert_equal ['foo'], a.reload.tag_list
  27 + end
  28 +
  29 + should 'not post article tags if not authenticated' do
  30 + profile = fast_create(Profile)
  31 + a = profile.articles.create(name: 'Test')
  32 +
  33 + post "/api/v1/articles/#{a.id}/tags?#{params.to_query}&tags=foo"
  34 + assert_equal 401, last_response.status
  35 + assert_equal [], a.reload.tag_list
  36 + end
  37 +
  38 + should 'get environment tags' do
  39 + person = fast_create(Person)
  40 + person.articles.create!(:name => 'article 1', :tag_list => 'first-tag')
  41 + person.articles.create!(:name => 'article 2', :tag_list => 'first-tag, second-tag')
  42 + person.articles.create!(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag')
  43 +
  44 + get '/api/v1/environment/tags'
  45 + json = JSON.parse(last_response.body)
  46 + assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, json)
  47 + end
  48 +end
... ...