Commit 54bc457e3042dee50f37913f5709bd91f9592eda
Exists in
ratings_minor_fixes
and in
4 other branches
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
Showing
3 changed files
with
56 additions
and
5 deletions
Show diff stats
app/api/v1/tags.rb
| 1 | module Api | 1 | module Api |
| 2 | module V1 | 2 | module V1 |
| 3 | class Tags < Grape::API | 3 | class Tags < Grape::API |
| 4 | - before { authenticate! } | ||
| 5 | - | ||
| 6 | resource :articles do | 4 | resource :articles do |
| 7 | - | ||
| 8 | resource ':id/tags' do | 5 | resource ':id/tags' do |
| 9 | - | ||
| 10 | get do | 6 | get do |
| 11 | article = find_article(environment.articles, params[:id]) | 7 | article = find_article(environment.articles, params[:id]) |
| 12 | present article.tag_list | 8 | present article.tag_list |
| @@ -14,6 +10,7 @@ module Api | @@ -14,6 +10,7 @@ module Api | ||
| 14 | 10 | ||
| 15 | desc "Add a tag to an article" | 11 | desc "Add a tag to an article" |
| 16 | post do | 12 | post do |
| 13 | + authenticate! | ||
| 17 | article = find_article(environment.articles, params[:id]) | 14 | article = find_article(environment.articles, params[:id]) |
| 18 | article.tag_list=params[:tags] | 15 | article.tag_list=params[:tags] |
| 19 | article.save | 16 | article.save |
| @@ -21,6 +18,13 @@ module Api | @@ -21,6 +18,13 @@ module Api | ||
| 21 | end | 18 | end |
| 22 | end | 19 | end |
| 23 | end | 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 | end | 28 | end |
| 25 | end | 29 | end |
| 26 | end | 30 | end |
test/api/environment_test.rb
| @@ -67,5 +67,4 @@ class EnvironmentTest < ActiveSupport::TestCase | @@ -67,5 +67,4 @@ class EnvironmentTest < ActiveSupport::TestCase | ||
| 67 | json = JSON.parse(last_response.body) | 67 | json = JSON.parse(last_response.body) |
| 68 | assert_equal context_env.id, json['id'] | 68 | assert_equal context_env.id, json['id'] |
| 69 | end | 69 | end |
| 70 | - | ||
| 71 | end | 70 | end |
| @@ -0,0 +1,48 @@ | @@ -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 |