| 1 |  |  | require 'spec_helper.rb' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | require 'watchdog_server' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | require 'json' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | def app | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |   WatchDogServer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 9 |  | View Code Duplication | def test_user | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |   user = Hash.new | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |   user['email']                 = '[email protected]' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |   user['name']                  = 'Foo Bar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |   user['org']                   = 'Baz B.V.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |   user['org_website']           = 'http://baz.nl' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |   user['prize']                 = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |   user['programmingExperience'] = '1-2 years' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |   user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | def empty_user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |   user = Hash.new | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |   user['programmingExperience'] = nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |   user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | existing_user = nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | existing_project = nil | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 29 |  | View Code Duplication | def test_project(user_id) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |   project = Hash.new | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |   project['name']        = 'Foo Bar Proj' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |   project['role']        = 'Foo Barer' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |   project['belongToASingleSofware'] = true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |   project['usesJunit'] = true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |   project['usesOtherFrameworks'] = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |   project['productionPercentage'] = 50 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |   project['useJunitOnlyForUnitTesting'] = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |   project['followTestDrivenDesign'] = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |   project['userId'] = user_id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |   project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | end | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | def test_interval(from, to) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |   interval = Hash.new | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |   interval['ts'] = from | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |   interval['te'] = to | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |   interval | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  | end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | def test_event(timestamp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |   event = Hash.new | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |   event['ts'] = timestamp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |   event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | describe 'The WatchDog Server' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |   before(:each) do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     mongo = WatchDogServer.new.helpers.mongo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     mongo.close | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     # Disable mail sending during testing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     WatchDogServer.any_instance.stub(:send_registration_email) do  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |   it 'should woof' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     get '/' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     expect(last_response).to be_ok | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     expect(last_response.body).to eq('Woof Woof') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |   it 'should get client version' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     get '/client' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     expect(last_response).to be_ok | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     expect(last_response.body).to eq('"2.0.0"') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |   it 'should create a user when the details are correct' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     post '/user', test_user.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     last_response.status.should eql(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     expect(last_response.body).to match(/^[0-9a-z]{40}$/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     existing_user = last_response.body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |   it 'should return 400 on bad JSON request to /user' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     post '/user', 'foobar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     last_response.status.should eql(400) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |   it 'should create a project when the details are correct' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     post '/project', test_project(existing_user).to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     last_response.status.should eql(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     expect(last_response.body).to match(/^[0-9a-z]{40}$/) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     existing_project = last_response.body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |   it 'should return 400 on bad JSON request to /project' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     post '/project', 'foobar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     last_response.status.should eql(400) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |   it 'should not create a project when the user does not exist' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     post '/project', test_project(nil).to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     last_response.status.should eql(404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |   it 'should return 400 on bad JSON to /users/:id/intervals' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     post '/user/foobar/foobarproject/intervals', 'foobar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     last_response.status.should eql(400) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |   it 'should return 400 on non JSON array being sent to /users/:id/intervals' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     post '/user/foobar/foobarproject/intervals', '{"foo":"bar"}' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     last_response.status.should eql(400) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |   it 'should return 400 when negative intervals exist' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     intervals = (1..10).map{|x| test_interval(x + 1, x)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     post '/user/foobar/foobarproj/intervals', intervals.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     last_response.status.should eql(400) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |   it 'should return 400 on bad JSON to /users/:id/:pid/events' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     post '/user/foobar/foobarproject/events', 'foobar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     last_response.status.should eql(400) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |   it 'should return 400 on non JSON array being sent to /users/:id/:pid/events' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     post '/user/foobar/foobarproject/events', '{"foo":"bar"}' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     last_response.status.should eql(400) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |   it 'should return 404 for non-existing user' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     get '/user/noexistingfoobar' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     last_response.status.should eql(404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |   it 'should return 200 for existing user' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     get '/user/' + existing_user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     last_response.status.should eql(200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  it 'should return 200 for existing project' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     get '/project/' + existing_project | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     last_response.status.should eql(200) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |   it 'should return 404 when posting intervals for non-existing user' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     intervals = (1..10).map{|x| test_interval(x, x + 1)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     post '/user/foobar/foobarprojects/intervals', intervals.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     last_response.status.should eql(404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |   it 'should return 404 when posting intervals for non-existing project' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     intervals = (1..10).map{|x| test_interval(x, x + 1)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     post '/user/' + existing_user + '/intervals', intervals.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     last_response.status.should eql(404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |   it 'should return 404 when posting events for non-existing user' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     events = (1..10).map{|x| test_event(x)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     post '/user/noexistingfoobar/foobarprojects/events', events.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     last_response.status.should eql(404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |   it 'should return 404 when posting events for non-existing project' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     events = (1..10).map{|x| test_event(x)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     post '/user/' + existing_user + '/noexistingfoobarproject/events', events.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |     last_response.status.should eql(404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |    | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |   it 'should return 404 when trying to register a user with missing programming experience' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |     post '/user', empty_user.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 	last_response.status.should eql(404) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |   end   | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |   it 'should return the number of stored intervals on successful insert' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     intervals = (1..10).map{|x| test_interval(x, x + 1)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |     user = test_user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     post '/user', user.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |     user_id = last_response.body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |     project = test_project(user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     post '/project', project.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |     project_id = last_response.body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |     post "/user/#{user_id}/#{project_id}/intervals", intervals.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     last_response.status.should eql(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |     expect(last_response.body).to eq('10') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |   it 'should return the number of stored events on successful insert' do | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |     events = (1..10).map{|x| test_event(x)} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |     user = test_user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     post '/user', user.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     user_id = last_response.body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |     project = test_project(user_id) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |     post '/project', project.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |     project_id = last_response.body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     post "/user/#{user_id}/#{project_id}/events", events.to_json | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |     last_response.status.should eql(201) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |     expect(last_response.body).to eq('10') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |   end | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 213 |  |  | end | 
            
                                                        
            
                                    
            
            
                | 214 |  |  |  |