Commit 5e0ff19955cb131dee196d756d864ac557877754

Authored by Caio Almeida
1 parent dfe77cb8

Ticket #45: Moving the environment tags API to the api/tags.rb file, fixing auth…

…entication and implementing tests for the tags API
lib/noosfero/api/v1/environments.rb
... ... @@ -10,11 +10,6 @@ module Noosfero
10 10 present environment.signup_person_fields
11 11 end
12 12  
13   - desc 'Return the tag counts for this environment'
14   - get '/tags' do
15   - present environment.tag_counts
16   - end
17   -
18 13 get ':id' do
19 14 local_environment = nil
20 15 if (params[:id] == "default")
... ...
lib/noosfero/api/v1/tags.rb
... ... @@ -2,12 +2,8 @@ module Noosfero
2 2 module API
3 3 module V1
4 4 class Tags < Grape::API
5   - before { authenticate! }
6   -
7 5 resource :articles do
8   -
9 6 resource ':id/tags' do
10   -
11 7 get do
12 8 article = find_article(environment.articles, params[:id])
13 9 present article.tag_list
... ... @@ -15,6 +11,7 @@ module Noosfero
15 11  
16 12 desc "Add a tag to an article"
17 13 post do
  14 + authenticate!
18 15 article = find_article(environment.articles, params[:id])
19 16 article.tag_list=params[:tags]
20 17 article.save
... ... @@ -22,6 +19,13 @@ module Noosfero
22 19 end
23 20 end
24 21 end
  22 +
  23 + resource :environment do
  24 + desc 'Return the tag counts for this environment'
  25 + get '/tags' do
  26 + present environment.tag_counts
  27 + end
  28 + end
25 29 end
26 30 end
27 31 end
... ...
test/api/environment_test.rb
... ... @@ -67,16 +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   - should 'return number of tags' do
72   - person = fast_create(Person)
73   - person.articles.create!(:name => 'article 1', :tag_list => 'first-tag')
74   - person.articles.create!(:name => 'article 2', :tag_list => 'first-tag, second-tag')
75   - person.articles.create!(:name => 'article 3', :tag_list => 'first-tag, second-tag, third-tag')
76   -
77   - get '/api/v1/environment/tags'
78   - json = JSON.parse(last_response.body)
79   - assert_equal({ 'first-tag' => 3, 'second-tag' => 2, 'third-tag' => 1 }, json)
80   - end
81   -
82 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
... ...