Commit 6512befb2a74c3838c6293ea02bd3ade6c056a43
Committed by
Caio
1 parent
1c94ea32
Exists in
master
and in
28 other branches
[Mezuro] Added validation to Project saving.
Showing
2 changed files
with
27 additions
and
19 deletions
Show diff stats
plugins/mezuro/lib/mezuro_plugin/project_content.rb
| 1 | 1 | class MezuroPlugin::ProjectContent < Article |
| 2 | + validate :validate_kalibro_project_name | |
| 2 | 3 | |
| 3 | 4 | def self.short_description |
| 4 | 5 | 'Kalibro project' |
| ... | ... | @@ -26,13 +27,13 @@ class MezuroPlugin::ProjectContent < Article |
| 26 | 27 | end |
| 27 | 28 | |
| 28 | 29 | def get_date_result(date) |
| 29 | - client = Kalibro::Client::ProjectResultClient.new | |
| 30 | + client = Kalibro::Client::ProjectResultClient.new | |
| 30 | 31 | @project_result ||= client.has_results_before(name, date) ? client.last_result_before(name, date) : client.first_result_after(name, date) |
| 31 | 32 | end |
| 32 | 33 | |
| 33 | 34 | def module_result(module_name) |
| 34 | 35 | module_name = project.name if module_name.nil? |
| 35 | - @module_client ||= module_result_client.module_result(project.name, module_name, project_result.date) | |
| 36 | + @module_client ||= module_result_client.module_result(project.name, module_name, project_result.date) | |
| 36 | 37 | end |
| 37 | 38 | |
| 38 | 39 | def result_history(module_name) |
| ... | ... | @@ -48,13 +49,20 @@ class MezuroPlugin::ProjectContent < Article |
| 48 | 49 | |
| 49 | 50 | private |
| 50 | 51 | |
| 52 | + def validate_kalibro_project_name | |
| 53 | + begin | |
| 54 | + Kalibro::Client::ProjectClient.project(name) | |
| 55 | + errors.add_to_base("Project name already exists in Kalibro") | |
| 56 | + rescue | |
| 57 | + end | |
| 58 | + end | |
| 59 | + | |
| 51 | 60 | def send_project_to_service |
| 52 | 61 | Kalibro::Client::ProjectClient.save(self) |
| 53 | - Kalibro::Client::KalibroClient.process_project(name, periodicity_in_days) | |
| 62 | + Kalibro::Client::KalibroClient.process_project(name, periodicity_in_days) | |
| 54 | 63 | end |
| 55 | 64 | |
| 56 | 65 | def remove_project_from_service |
| 57 | 66 | Kalibro::Client::ProjectClient.remove(name) |
| 58 | 67 | end |
| 59 | - | |
| 60 | 68 | end | ... | ... |
plugins/mezuro/test/unit/mezuro_plugin/project_content_test.rb
| ... | ... | @@ -14,6 +14,15 @@ class ProjectContentTest < ActiveSupport::TestCase |
| 14 | 14 | @content.repository_url = @project.repository.address |
| 15 | 15 | @content.configuration_name = @project.configuration_name |
| 16 | 16 | @content.periodicity_in_days = 1 |
| 17 | + | |
| 18 | + @content2 = MezuroPlugin::ProjectContent.new | |
| 19 | + @content2.name = @content.name | |
| 20 | + @content2.license = @project.license | |
| 21 | + @content2.description = @project.description | |
| 22 | + @content2.repository_type = @project.repository.type | |
| 23 | + @content2.repository_url = @project.repository.address | |
| 24 | + @content2.configuration_name = @project.configuration_name | |
| 25 | + @content2.periodicity_in_days = 1 | |
| 17 | 26 | end |
| 18 | 27 | |
| 19 | 28 | should 'be an article' do |
| ... | ... | @@ -91,6 +100,12 @@ returns(module_result) |
| 91 | 100 | @content.send :remove_project_from_service |
| 92 | 101 | end |
| 93 | 102 | |
| 103 | + should 'not save a project with an existing project name in kalibro' do | |
| 104 | + Kalibro::Client::ProjectClient.expects(:project).with(@content.name).returns(mock) | |
| 105 | + @content.send :validate_kalibro_project_name | |
| 106 | + assert_equal @content.errors.on_base, "Project name already exists in Kalibro" | |
| 107 | + end | |
| 108 | + | |
| 94 | 109 | private |
| 95 | 110 | def mock_project_client |
| 96 | 111 | Kalibro::Client::ProjectClient.expects(:project).with(@content.name).returns(@project) |
| ... | ... | @@ -103,18 +118,3 @@ returns(module_result) |
| 103 | 118 | end |
| 104 | 119 | |
| 105 | 120 | end |
| 106 | - | |
| 107 | - | |
| 108 | - | |
| 109 | - | |
| 110 | - | |
| 111 | - | |
| 112 | - | |
| 113 | - | |
| 114 | - | |
| 115 | - | |
| 116 | - | |
| 117 | - | |
| 118 | - | |
| 119 | - | |
| 120 | - | ... | ... |