Commit 822dac87eccad5044e0e82fd671e8b71f21942a5
1 parent
882029d9
Exists in
master
and in
4 other branches
Extract tests for project hooks API
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Showing
2 changed files
with
132 additions
and
116 deletions
Show diff stats
@@ -0,0 +1,132 @@ | @@ -0,0 +1,132 @@ | ||
1 | +require 'spec_helper' | ||
2 | + | ||
3 | +describe API::API, 'ProjectHooks' do | ||
4 | + include ApiHelpers | ||
5 | + before(:each) { enable_observers } | ||
6 | + after(:each) { disable_observers } | ||
7 | + | ||
8 | + let(:user) { create(:user) } | ||
9 | + let(:user3) { create(:user) } | ||
10 | + let!(:project) { create(:project_with_code, creator_id: user.id, namespace: user.namespace) } | ||
11 | + let!(:hook) { create(:project_hook, project: project, url: "http://example.com") } | ||
12 | + | ||
13 | + before do | ||
14 | + project.team << [user, :master] | ||
15 | + project.team << [user3, :developer] | ||
16 | + end | ||
17 | + | ||
18 | + describe "GET /projects/:id/hooks" do | ||
19 | + context "authorized user" do | ||
20 | + it "should return project hooks" do | ||
21 | + get api("/projects/#{project.id}/hooks", user) | ||
22 | + response.status.should == 200 | ||
23 | + | ||
24 | + json_response.should be_an Array | ||
25 | + json_response.count.should == 1 | ||
26 | + json_response.first['url'].should == "http://example.com" | ||
27 | + end | ||
28 | + end | ||
29 | + | ||
30 | + context "unauthorized user" do | ||
31 | + it "should not access project hooks" do | ||
32 | + get api("/projects/#{project.id}/hooks", user3) | ||
33 | + response.status.should == 403 | ||
34 | + end | ||
35 | + end | ||
36 | + end | ||
37 | + | ||
38 | + describe "GET /projects/:id/hooks/:hook_id" do | ||
39 | + context "authorized user" do | ||
40 | + it "should return a project hook" do | ||
41 | + get api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
42 | + response.status.should == 200 | ||
43 | + json_response['url'].should == hook.url | ||
44 | + end | ||
45 | + | ||
46 | + it "should return a 404 error if hook id is not available" do | ||
47 | + get api("/projects/#{project.id}/hooks/1234", user) | ||
48 | + response.status.should == 404 | ||
49 | + end | ||
50 | + end | ||
51 | + | ||
52 | + context "unauthorized user" do | ||
53 | + it "should not access an existing hook" do | ||
54 | + get api("/projects/#{project.id}/hooks/#{hook.id}", user3) | ||
55 | + response.status.should == 403 | ||
56 | + end | ||
57 | + end | ||
58 | + | ||
59 | + it "should return a 404 error if hook id is not available" do | ||
60 | + get api("/projects/#{project.id}/hooks/1234", user) | ||
61 | + response.status.should == 404 | ||
62 | + end | ||
63 | + end | ||
64 | + | ||
65 | + describe "POST /projects/:id/hooks" do | ||
66 | + it "should add hook to project" do | ||
67 | + expect { | ||
68 | + post api("/projects/#{project.id}/hooks", user), | ||
69 | + url: "http://example.com" | ||
70 | + }.to change {project.hooks.count}.by(1) | ||
71 | + response.status.should == 201 | ||
72 | + end | ||
73 | + | ||
74 | + it "should return a 400 error if url not given" do | ||
75 | + post api("/projects/#{project.id}/hooks", user) | ||
76 | + response.status.should == 400 | ||
77 | + end | ||
78 | + | ||
79 | + it "should return a 422 error if url not valid" do | ||
80 | + post api("/projects/#{project.id}/hooks", user), "url" => "ftp://example.com" | ||
81 | + response.status.should == 422 | ||
82 | + end | ||
83 | + end | ||
84 | + | ||
85 | + describe "PUT /projects/:id/hooks/:hook_id" do | ||
86 | + it "should update an existing project hook" do | ||
87 | + put api("/projects/#{project.id}/hooks/#{hook.id}", user), | ||
88 | + url: 'http://example.org' | ||
89 | + response.status.should == 200 | ||
90 | + json_response['url'].should == 'http://example.org' | ||
91 | + end | ||
92 | + | ||
93 | + it "should return 404 error if hook id not found" do | ||
94 | + put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org' | ||
95 | + response.status.should == 404 | ||
96 | + end | ||
97 | + | ||
98 | + it "should return 400 error if url is not given" do | ||
99 | + put api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
100 | + response.status.should == 400 | ||
101 | + end | ||
102 | + | ||
103 | + it "should return a 422 error if url is not valid" do | ||
104 | + put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com' | ||
105 | + response.status.should == 422 | ||
106 | + end | ||
107 | + end | ||
108 | + | ||
109 | + describe "DELETE /projects/:id/hooks/:hook_id" do | ||
110 | + it "should delete hook from project" do | ||
111 | + expect { | ||
112 | + delete api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
113 | + }.to change {project.hooks.count}.by(-1) | ||
114 | + response.status.should == 200 | ||
115 | + end | ||
116 | + | ||
117 | + it "should return success when deleting hook" do | ||
118 | + delete api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
119 | + response.status.should == 200 | ||
120 | + end | ||
121 | + | ||
122 | + it "should return success when deleting non existent hook" do | ||
123 | + delete api("/projects/#{project.id}/hooks/42", user) | ||
124 | + response.status.should == 200 | ||
125 | + end | ||
126 | + | ||
127 | + it "should return a 405 error if hook id not given" do | ||
128 | + delete api("/projects/#{project.id}/hooks", user) | ||
129 | + response.status.should == 405 | ||
130 | + end | ||
131 | + end | ||
132 | +end |
spec/requests/api/projects_spec.rb
@@ -10,7 +10,6 @@ describe API::API do | @@ -10,7 +10,6 @@ describe API::API do | ||
10 | let(:user3) { create(:user) } | 10 | let(:user3) { create(:user) } |
11 | let(:admin) { create(:admin) } | 11 | let(:admin) { create(:admin) } |
12 | let!(:project) { create(:project_with_code, creator_id: user.id, namespace: user.namespace) } | 12 | let!(:project) { create(:project_with_code, creator_id: user.id, namespace: user.namespace) } |
13 | - let!(:hook) { create(:project_hook, project: project, url: "http://example.com") } | ||
14 | let!(:snippet) { create(:project_snippet, author: user, project: project, title: 'example') } | 13 | let!(:snippet) { create(:project_snippet, author: user, project: project, title: 'example') } |
15 | let!(:users_project) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) } | 14 | let!(:users_project) { create(:users_project, user: user, project: project, project_access: UsersProject::MASTER) } |
16 | let!(:users_project2) { create(:users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER) } | 15 | let!(:users_project2) { create(:users_project, user: user3, project: project, project_access: UsersProject::DEVELOPER) } |
@@ -439,121 +438,6 @@ describe API::API do | @@ -439,121 +438,6 @@ describe API::API do | ||
439 | end | 438 | end |
440 | end | 439 | end |
441 | 440 | ||
442 | - describe "GET /projects/:id/hooks" do | ||
443 | - context "authorized user" do | ||
444 | - it "should return project hooks" do | ||
445 | - get api("/projects/#{project.id}/hooks", user) | ||
446 | - response.status.should == 200 | ||
447 | - | ||
448 | - json_response.should be_an Array | ||
449 | - json_response.count.should == 1 | ||
450 | - json_response.first['url'].should == "http://example.com" | ||
451 | - end | ||
452 | - end | ||
453 | - | ||
454 | - context "unauthorized user" do | ||
455 | - it "should not access project hooks" do | ||
456 | - get api("/projects/#{project.id}/hooks", user3) | ||
457 | - response.status.should == 403 | ||
458 | - end | ||
459 | - end | ||
460 | - end | ||
461 | - | ||
462 | - describe "GET /projects/:id/hooks/:hook_id" do | ||
463 | - context "authorized user" do | ||
464 | - it "should return a project hook" do | ||
465 | - get api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
466 | - response.status.should == 200 | ||
467 | - json_response['url'].should == hook.url | ||
468 | - end | ||
469 | - | ||
470 | - it "should return a 404 error if hook id is not available" do | ||
471 | - get api("/projects/#{project.id}/hooks/1234", user) | ||
472 | - response.status.should == 404 | ||
473 | - end | ||
474 | - end | ||
475 | - | ||
476 | - context "unauthorized user" do | ||
477 | - it "should not access an existing hook" do | ||
478 | - get api("/projects/#{project.id}/hooks/#{hook.id}", user3) | ||
479 | - response.status.should == 403 | ||
480 | - end | ||
481 | - end | ||
482 | - | ||
483 | - it "should return a 404 error if hook id is not available" do | ||
484 | - get api("/projects/#{project.id}/hooks/1234", user) | ||
485 | - response.status.should == 404 | ||
486 | - end | ||
487 | - end | ||
488 | - | ||
489 | - describe "POST /projects/:id/hooks" do | ||
490 | - it "should add hook to project" do | ||
491 | - expect { | ||
492 | - post api("/projects/#{project.id}/hooks", user), | ||
493 | - url: "http://example.com" | ||
494 | - }.to change {project.hooks.count}.by(1) | ||
495 | - response.status.should == 201 | ||
496 | - end | ||
497 | - | ||
498 | - it "should return a 400 error if url not given" do | ||
499 | - post api("/projects/#{project.id}/hooks", user) | ||
500 | - response.status.should == 400 | ||
501 | - end | ||
502 | - | ||
503 | - it "should return a 422 error if url not valid" do | ||
504 | - post api("/projects/#{project.id}/hooks", user), "url" => "ftp://example.com" | ||
505 | - response.status.should == 422 | ||
506 | - end | ||
507 | - end | ||
508 | - | ||
509 | - describe "PUT /projects/:id/hooks/:hook_id" do | ||
510 | - it "should update an existing project hook" do | ||
511 | - put api("/projects/#{project.id}/hooks/#{hook.id}", user), | ||
512 | - url: 'http://example.org' | ||
513 | - response.status.should == 200 | ||
514 | - json_response['url'].should == 'http://example.org' | ||
515 | - end | ||
516 | - | ||
517 | - it "should return 404 error if hook id not found" do | ||
518 | - put api("/projects/#{project.id}/hooks/1234", user), url: 'http://example.org' | ||
519 | - response.status.should == 404 | ||
520 | - end | ||
521 | - | ||
522 | - it "should return 400 error if url is not given" do | ||
523 | - put api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
524 | - response.status.should == 400 | ||
525 | - end | ||
526 | - | ||
527 | - it "should return a 422 error if url is not valid" do | ||
528 | - put api("/projects/#{project.id}/hooks/#{hook.id}", user), url: 'ftp://example.com' | ||
529 | - response.status.should == 422 | ||
530 | - end | ||
531 | - end | ||
532 | - | ||
533 | - describe "DELETE /projects/:id/hooks/:hook_id" do | ||
534 | - it "should delete hook from project" do | ||
535 | - expect { | ||
536 | - delete api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
537 | - }.to change {project.hooks.count}.by(-1) | ||
538 | - response.status.should == 200 | ||
539 | - end | ||
540 | - | ||
541 | - it "should return success when deleting hook" do | ||
542 | - delete api("/projects/#{project.id}/hooks/#{hook.id}", user) | ||
543 | - response.status.should == 200 | ||
544 | - end | ||
545 | - | ||
546 | - it "should return success when deleting non existent hook" do | ||
547 | - delete api("/projects/#{project.id}/hooks/42", user) | ||
548 | - response.status.should == 200 | ||
549 | - end | ||
550 | - | ||
551 | - it "should return a 405 error if hook id not given" do | ||
552 | - delete api("/projects/#{project.id}/hooks", user) | ||
553 | - response.status.should == 405 | ||
554 | - end | ||
555 | - end | ||
556 | - | ||
557 | describe "GET /projects/:id/snippets" do | 441 | describe "GET /projects/:id/snippets" do |
558 | it "should return an array of project snippets" do | 442 | it "should return an array of project snippets" do |
559 | get api("/projects/#{project.id}/snippets", user) | 443 | get api("/projects/#{project.id}/snippets", user) |