11describe CC ::Service ::Asana , type : :service do
2- it "quality" do
3- assert_asana_receives (
4- event ( :quality , to : "D" , from : "C" ) ,
5- "Refactor User from a D on Code Climate - https://codeclimate.com/repos/1/feed" ,
6- )
7- end
2+ it "requires an authorization key or token, and nudges users toward personal_access_token" do
3+ config = CC ::Service ::Asana ::Config . new ( workspace_id : '1234' )
4+ expect ( config ) . to_not be_valid
5+ expect ( config . errors [ :personal_access_token ] ) . to eq [ "can't be blank" ]
86
9- it "vulnerability" do
10- assert_asana_receives (
11- event ( :vulnerability , vulnerabilities : [ {
12- "warning_type" => "critical" ,
13- "location" => "app/user.rb line 120" ,
14- } ] ) ,
15- "New critical issue found in app/user.rb line 120 - https://codeclimate.com/repos/1/feed" ,
16- )
17- end
7+ config . api_key = "foo"
8+ expect ( config ) . to be_valid
189
19- it "issue" do
20- payload = {
21- issue : {
22- "check_name" => "Style/LongLine" ,
23- "description" => "Line is too long [1000/80]" ,
24- } ,
25- constant_name : "foo.rb" ,
26- details_url : "http://example.com/repos/id/foo.rb#issue_123" ,
27- }
28-
29- assert_asana_receives (
30- event ( :issue , payload ) ,
31- "Fix \" Style/LongLine\" issue in foo.rb" ,
32- "Line is too long [1000/80]\n \n http://example.com/repos/id/foo.rb#issue_123" ,
33- )
10+ config . api_key = nil
11+ config . personal_access_token = "bar"
12+ expect ( config ) . to be_valid
3413 end
3514
36- it "successful post" do
37- http_stubs . post "/api/1.0/tasks" do |_env |
38- [ 200 , { } , '{"data":{"id":"2"}}' ]
15+ shared_examples "Asana integration" do |authorization |
16+ it "creates a ticket for quality changes" do
17+ assert_asana_receives (
18+ event ( :quality , to : "D" , from : "C" ) ,
19+ "Refactor User from a D on Code Climate - https://codeclimate.com/repos/1/feed" ,
20+ authorization ,
21+ )
3922 end
4023
41- response = receive_event
24+ it "creates a ticket for vulnerability changes" do
25+ assert_asana_receives (
26+ event ( :vulnerability , vulnerabilities : [ {
27+ "warning_type" => "critical" ,
28+ "location" => "app/user.rb line 120" ,
29+ } ] ) ,
30+ "New critical issue found in app/user.rb line 120 - https://codeclimate.com/repos/1/feed" ,
31+ authorization ,
32+ )
33+ end
4234
43- expect ( response [ :id ] ) . to eq ( "2" )
44- expect ( response [ :url ] ) . to eq ( "https://app.asana.com/0/1/2" )
45- end
35+ it "creates a ticket for a new issue" do
36+ payload = {
37+ issue : {
38+ "check_name" => "Style/LongLine" ,
39+ "description" => "Line is too long [1000/80]" ,
40+ } ,
41+ constant_name : "foo.rb" ,
42+ details_url : "http://example.com/repos/id/foo.rb#issue_123" ,
43+ }
4644
47- it "receive test" do
48- http_stubs . post "/api/1.0/tasks" do |_env |
49- [ 200 , { } , '{"data":{"id":"4"}}' ]
45+ assert_asana_receives (
46+ event ( :issue , payload ) ,
47+ "Fix \" Style/LongLine\" issue in foo.rb" ,
48+ authorization ,
49+ "Line is too long [1000/80]\n \n http://example.com/repos/id/foo.rb#issue_123" ,
50+ )
5051 end
5152
52- response = receive_event ( name : "test" )
53+ it "can make a successful POST request" do
54+ http_stubs . post "/api/1.0/tasks" do |_env |
55+ [ 200 , { } , '{"data":{"id":"2"}}' ]
56+ end
57+
58+ response = receive_event ( authorization )
5359
54- expect ( response [ :message ] ) . to eq ( "Ticket <a href='https://app.asana.com/0/1/4'>4</a> created." )
60+ expect ( response [ :id ] ) . to eq ( "2" )
61+ expect ( response [ :url ] ) . to eq ( "https://app.asana.com/0/1/2" )
62+ end
63+
64+ it "can make a test request" do
65+ http_stubs . post "/api/1.0/tasks" do |_env |
66+ [ 200 , { } , '{"data":{"id":"4"}}' ]
67+ end
68+
69+ response = receive_event ( authorization , name : "test" )
70+
71+ expect ( response [ :message ] ) . to eq ( "Ticket <a href='https://app.asana.com/0/1/4'>4</a> created." )
72+ end
5573 end
5674
75+ it_behaves_like "Asana integration" , :api_key
76+ it_behaves_like "Asana integration" , :personal_access_token
77+ it_behaves_like "Asana integration" , :both
78+
5779 private
5880
59- def assert_asana_receives ( event_data , name , notes = "" )
81+ def assert_asana_receives ( event_data , name , authorization , notes = "" )
6082 http_stubs . post "/api/1.0/tasks" do |env |
83+ case authorization
84+ when :api_key
85+ expect ( env [ :request_headers ] [ "Authorization" ] ) . to include ( "Basic" )
86+ when :personal_access_token
87+ expect ( env [ :request_headers ] [ "Authorization" ] ) . to eq ( "Bearer def456" )
88+ when :both
89+ # prefer the personal access token
90+ expect ( env [ :request_headers ] [ "Authorization" ] ) . to eq ( "Bearer def456" )
91+ else
92+ raise ArgumentError
93+ end
6194 body = JSON . parse ( env [ :body ] )
6295 data = body [ "data" ]
6396
@@ -70,13 +103,24 @@ def assert_asana_receives(event_data, name, notes = "")
70103 [ 200 , { } , '{"data":{"id":4}}' ]
71104 end
72105
73- receive_event ( event_data )
106+ receive_event ( authorization , event_data )
74107 end
75108
76- def receive_event ( event_data = nil )
109+ def receive_event ( authorization , event_data = nil )
110+ service_configuration = { workspace_id : "1" , project_id : "2" , assignee : "jim@asana.com" }
111+ case authorization
112+ when :api_key
113+ service_configuration [ :api_key ] = "abc123"
114+ when :personal_access_token
115+ service_configuration [ :personal_access_token ] = "def456"
116+ when :both
117+ service_configuration [ :api_key ] = "abc123"
118+ service_configuration [ :personal_access_token ] = "def456"
119+ else raise ArgumentError
120+ end
77121 service_receive (
78122 CC ::Service ::Asana ,
79- { api_key : "abc123" , workspace_id : "1" , project_id : "2" , assignee : "jim@asana.com" } ,
123+ service_configuration ,
80124 event_data || event ( :quality , to : "D" , from : "C" ) ,
81125 )
82126 end
0 commit comments