From b1ad474b8b899082a4cebdb2c4cfdfb17304476c Mon Sep 17 00:00:00 2001 From: natashabuckham Date: Fri, 14 Apr 2023 16:05:44 +0100 Subject: [PATCH 1/7] add method tested in DM repository --- app.rb | 6 + coverage/.last_run.json | 2 +- coverage/.resultset.json | 288 +++-------------------------------- lib/dm_repository.rb | 12 +- spec/dm_repository_spec.rb | 34 +++-- spec/integration/app_spec.rb | 20 +++ views/send_message.erb | 13 ++ 7 files changed, 90 insertions(+), 285 deletions(-) create mode 100644 views/send_message.erb diff --git a/app.rb b/app.rb index 1925896..f7bf355 100644 --- a/app.rb +++ b/app.rb @@ -45,4 +45,10 @@ def check_handle_exists(handle) end return erb(:index) end + + get '/send_message' do + return erb(:send_message) + end + + post '/' end diff --git a/coverage/.last_run.json b/coverage/.last_run.json index a6eee7c..52d2bf2 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "line": 89.23 + "line": 100.0 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 94e3d26..5e3e90e 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -7,95 +7,17 @@ 1, null, 1, - 1, - 1, - 1, - null, - null, - 1, - 1, - 1, - null, - null, - 1, - 1, - null, - 1, - 1, - null, - 1, - 1, + 2, + 2, + 2, null, - 1, - 1, null, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm_repository.rb": { - "lines": [ - 1, - null, - 1, 1, 3, null, - 3, - 3, - null, - 3, - null, - 3, - 4, - null, - null, - 3, - null, - null, - 1, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - null, - null, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm.rb": { - "lines": [ - 1, - 1, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/spec/integration/app_spec.rb": { - "lines": [ - 1, - 1, - 1, - null, - 1, - 1, - null, - 4, - null, 1, - 3, - 3, - 3, - null, - null, 1, - 3, null, - null, - 1, - 1, - 1, 1, 1, null, @@ -104,249 +26,73 @@ null, 1, 1, - 1, null, null, 1, 1, 1, 1, - null, - 1, 1, null, 1, 1, - 1, - null, - null, - 1, - 1, null, 1, - 1, - null, null, null ] }, - "/Users/natashabuckham/Projects/Chitter-DMs/app.rb": { + "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm_repository.rb": { "lines": [ 1, - 1, - 1, - 1, - 1, - 1, - null, - 1, - null, - 1, - null, null, 1, 1, - 1, - 1, - 1, - null, - null, - 1, - 2, - 2, - null, + 3, null, - 1, - 2, - 2, - 1, - 1, + 3, + 3, null, - 1, - 1, + 3, null, + 3, + 5, null, null, - 1, - 0, - 0, + 3, null, null, 1, 5, 5, - 1, - null, + 5, + 5, + 5, + 5, 5, null, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/user_repository.rb": { - "lines": [ - 1, - null, - 1, - 1, - 1, - 1, - null, - 1, - null, - 8, - 1, - null, - null, - 1, - 2, - 2, - null, - 2, - 16, - 2, - null, - null, - null, - null, - null, - null, - null, - null, - 1, - 6, - 6, - 6, - 6, - 6, - 6, - null, - null, - 1, - 4, - null, - null, - 4, - 4, - null, - 4, - null, - 8, - 4, - null, - null, - 1, - 1, null, 1, 1, null, null, - null, - 1, 1, null, 1, - 1, null, null, null, - 1, - null, - 1, - 7, - 7, - 7, - 7, - 7, - null, - null, null ] }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/user.rb": { + "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm.rb": { "lines": [ - 1, - null, - 1, - 6, - 6, - 6, - null, - null, - 1, - 1, - 6, - null, - null, - 1, 1, 1, - 1, - null, - 1, - 1, - 1, - 1, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - null, - 1, - null, - 1, - 1, - null, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - null, - 1, - 1, - null, - null, - 1, - 1, - 1, - null, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - 1, - null, - 1, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - 1, - null, - 1, - 1, - null, - null, null ] } }, - "timestamp": 1681473835 + "timestamp": 1681484652 } } diff --git a/lib/dm_repository.rb b/lib/dm_repository.rb index cc3d774..4471eed 100644 --- a/lib/dm_repository.rb +++ b/lib/dm_repository.rb @@ -26,4 +26,14 @@ def result_set_to_dm(result_set) return dm end -end \ No newline at end of file + def add(dm) + sql = "INSERT INTO dms + (contents, time, sender_handle, recipient_handle) + VALUES($1, $2, $3, $4);" + params = [dm.contents, dm.time, dm.sender_handle, dm.recipient_handle] + + result_set = DatabaseConnection.exec_params(sql, params) + + end + +end diff --git a/spec/dm_repository_spec.rb b/spec/dm_repository_spec.rb index 6ed6dc0..97ff67b 100644 --- a/spec/dm_repository_spec.rb +++ b/spec/dm_repository_spec.rb @@ -1,27 +1,37 @@ -require 'dm_repository' -require 'dm' +require "dm_repository" +require "dm" def reset_tables - sql = File.read('spec/seeds.sql') - connection = PG.connect({ host: '127.0.0.1', dbname: 'chitter-dms-test' }) + sql = File.read("spec/seeds.sql") + connection = PG.connect({ host: "127.0.0.1", dbname: "chitter-dms-test" }) connection.exec(sql) end RSpec.describe DMRepository do - before(:each) do - reset_tables - end + before(:each) { reset_tables } - it 'returns the inbox for a user' do + it "returns the inbox for a user" do repo = DMRepository.new - dms_bob = repo.find_inbox('Bob') - dms_george = repo.find_inbox('George') + dms_bob = repo.find_inbox("Bob") + dms_george = repo.find_inbox("George") expect(dms_bob.length).to eq 1 - expect(dms_bob.first.contents).to eq 'Hello Bob' + expect(dms_bob.first.contents).to eq "Hello Bob" expect(dms_george.length).to eq 2 - expect(dms_george.first.contents).to eq 'How are you?' + expect(dms_george.first.contents).to eq "How are you?" + end + + it "adds a new DM to the database" do + dm = DM.new + dm.contents = "I am a new DM" + dm.sender_handle = "Bob" + dm.recipient_handle = "Sam" + + repo = DMRepository.new + repo.add(dm) + + expect(repo.find_inbox("Sam").last.contents).to eq "I am a new DM" end end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index bfa46f5..800feb2 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -51,4 +51,24 @@ def reset_tables expect(response.body).to include '

You are not logged in

' end end + + context "get /send_message" do + it "returns the send message view" do + response = get('/send_message') + + expect(response.status).to eq 200 + expect(response.body).to include 'Send Message' + expect(response.body).to include 'Back to inbox' + end + end + + context "post /send_message" do + it "returns 'Message sent'" do + response = post('/send_message') + + expect(response.status).to eq 302 + expect(response.body).to include 'Message Sent' + expect(response.body).to include 'Back to inbox' + end + end end diff --git a/views/send_message.erb b/views/send_message.erb new file mode 100644 index 0000000..68c3eea --- /dev/null +++ b/views/send_message.erb @@ -0,0 +1,13 @@ + + + + + + +
+
+ + +
+ + From e0f4a1d6991dbefcf90216eb8efdfa1f806d64d2 Mon Sep 17 00:00:00 2001 From: natashabuckham Date: Sat, 15 Apr 2023 14:52:43 +0100 Subject: [PATCH 2/7] get send message and post send message routes tested --- app.rb | 12 +- coverage/.last_run.json | 2 +- coverage/.resultset.json | 349 +++++++++++++++++++++++++++++++++-- spec/integration/app_spec.rb | 52 +++--- views/send_message.erb | 27 ++- 5 files changed, 398 insertions(+), 44 deletions(-) diff --git a/app.rb b/app.rb index f7bf355..7ae6160 100644 --- a/app.rb +++ b/app.rb @@ -50,5 +50,15 @@ def check_handle_exists(handle) return erb(:send_message) end - post '/' + post '/send_message' do + new_dm = DM.new + new_dm.recipient_handle = params[:recipient_handle] + new_dm.contents = params[:contents] + new_dm.sender_handle = session[:handle] + + @dm_repo = DMRepository.new + @dm_repo.add(new_dm) + flash[:message_sent] = "Message sent" + redirect '/send_message' + end end diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 52d2bf2..7f06b9e 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "line": 100.0 + "line": 99.15 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 5e3e90e..1f533fa 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -48,51 +48,378 @@ null, 1, 1, + 5, + null, + 5, + 5, + null, + 5, + null, + 5, + 7, + null, + null, + 5, + null, + null, + 1, + 7, + 7, + 7, + 7, + 7, + 7, + 7, + null, + null, + 1, + 2, + null, + null, + 2, + null, + 2, + null, + null, + null, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm.rb": { + "lines": [ + 1, + 1, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/spec/integration/app_spec.rb": { + "lines": [ + 1, + 1, + 1, + null, + 1, + 1, + null, + 6, + null, + 1, + 5, + 5, + 5, + null, + null, + 6, + null, + 1, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + 1, + 1, + null, + 1, + 1, + null, + null, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, + null, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/app.rb": { + "lines": [ + 1, + 1, + 1, + 1, + 1, + 1, + null, + 1, + null, + 1, + null, + null, + 1, + 1, + 1, + 1, + 1, + null, + null, + 1, + 3, 3, null, + null, + 1, 3, 3, + 2, + 2, + null, + 1, + 1, + null, + null, null, + 1, + 0, + 0, + null, + null, + 1, + 7, + 7, + 2, + null, + 7, + null, + null, + 1, + 2, + null, + null, + 1, + 1, + 1, + 1, + 1, + null, + 1, + 1, + 1, + 1, + null, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/lib/user_repository.rb": { + "lines": [ + 1, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + 8, + 1, + null, + null, + 1, + 3, 3, null, 3, - 5, + 24, + 3, + null, + null, + null, + null, null, null, - 3, null, null, 1, - 5, - 5, - 5, - 5, - 5, - 5, - 5, + 6, + 6, + 6, + 6, + 6, + 6, + null, + null, + 1, + 4, + null, + null, + 4, + 4, + null, + 4, + null, + 8, + 4, null, null, 1, 1, null, + 1, + 1, + null, null, + null, + 1, 1, null, 1, + 1, + null, null, null, + 1, + null, + 1, + 7, + 7, + 7, + 7, + 7, + null, null, null ] }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm.rb": { + "/Users/natashabuckham/Projects/Chitter-DMs/lib/user.rb": { + "lines": [ + 1, + 1, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/spec/user_repository_spec.rb": { "lines": [ + 1, + null, + 1, + 6, + 6, + 6, + null, + null, + 1, + 1, + 6, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + 1, + 1, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + 1, + 1, + 1, + null, + 1, + null, + null, + null, 1, 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, null ] } }, - "timestamp": 1681484652 + "timestamp": 1681566610 } } diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 800feb2..9cae550 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -1,6 +1,6 @@ -require 'spec_helper' -require 'rack/test' -require_relative '../../app' +require "spec_helper" +require "rack/test" +require_relative "../../app" describe Application do include Rack::Test::Methods @@ -8,67 +8,77 @@ let(:app) { Application.new } def reset_tables - seed_sql = File.read('spec/seeds.sql') - connection = PG.connect({ host: '127.0.0.1', dbname: 'chitter-dms-test' }) + seed_sql = File.read("spec/seeds.sql") + connection = PG.connect({ host: "127.0.0.1", dbname: "chitter-dms-test" }) connection.exec(seed_sql) end - before(:each) do - reset_tables - end + before(:each) { reset_tables } context "GET /" do it "returns the homepage with list of DMs if user is logged in with valid handle" do response = get("/") expect(response.status).to eq 200 - expect(response.body).to include '

You are not logged in

' + expect(response.body).to include "

You are not logged in

" - response = post("/login", handle: 'Bob') + response = post("/login", handle: "Bob") expect(response.status).to eq 302 response = get("/") expect(response.status).to eq 200 - expect(response.body).to include 'You are logged in' + expect(response.body).to include "You are logged in" end it "returns homepage with flash message if user tries to log in with invalid handle" do response = get("/") expect(response.status).to eq 200 - expect(response.body).to include '

You are not logged in

' + expect(response.body).to include "

You are not logged in

" - response = post("/login", handle: 'Natasha') + response = post("/login", handle: "Natasha") expect(response.status).to eq 302 response = get("/") expect(response.status).to eq 200 - expect(response.body).to include 'Handle Does Not Exist' + expect(response.body).to include "Handle Does Not Exist" end it "returns homepage with log in form if user is not logged in" do response = get("/") expect(response.status).to eq 200 - expect(response.body).to include '

You are not logged in

' + expect(response.body).to include "

You are not logged in

" end end context "get /send_message" do it "returns the send message view" do - response = get('/send_message') + response = get("/send_message") expect(response.status).to eq 200 - expect(response.body).to include 'Send Message' - expect(response.body).to include 'Back to inbox' + expect(response.body).to include "Send Message" end end context "post /send_message" do it "returns 'Message sent'" do - response = post('/send_message') + response = get("/") + expect(response.status).to eq 200 + expect(response.body).to include "

You are not logged in

" + response = post("/login", handle: "Bob") expect(response.status).to eq 302 - expect(response.body).to include 'Message Sent' - expect(response.body).to include 'Back to inbox' + + response = get("/") + expect(response.status).to eq 200 + expect(response.body).to include "You are logged in" + + response = post("/send_message", recipient_handle: "Sam", contents: "Hello") + expect(response.status).to eq 302 + + response = get("/send_message") + expect(response.status).to eq 200 + expect(response.body).to include "Message sent" end end end + diff --git a/views/send_message.erb b/views/send_message.erb index 68c3eea..b15ed77 100644 --- a/views/send_message.erb +++ b/views/send_message.erb @@ -1,13 +1,20 @@ - - - - -
-
- - -
- + + + <% flash.each do |type, message| %> +
+ <%= message %> + × +
+ <% end %> +
+
+ + + + + +
+ From bf30f3a1c83873eed6544fd87bf2cfcaf55b4a44 Mon Sep 17 00:00:00 2001 From: natashabuckham Date: Sat, 15 Apr 2023 15:09:45 +0100 Subject: [PATCH 3/7] find id method written and tested for user repo --- app.rb | 25 ++-- coverage/.last_run.json | 2 +- coverage/.resultset.json | 255 ++--------------------------------- lib/user_repository.rb | 31 +++-- spec/integration/app_spec.rb | 22 ++- spec/user_repository_spec.rb | 15 ++- 6 files changed, 84 insertions(+), 266 deletions(-) diff --git a/app.rb b/app.rb index 7ae6160..364933e 100644 --- a/app.rb +++ b/app.rb @@ -15,6 +15,7 @@ class Application < Sinatra::Base register Sinatra::Flash enable :sessions also_reload './lib/user_repository' + also_reload './lib/dm_repository' end def check_handle_exists(handle) @@ -51,14 +52,22 @@ def check_handle_exists(handle) end post '/send_message' do - new_dm = DM.new - new_dm.recipient_handle = params[:recipient_handle] - new_dm.contents = params[:contents] - new_dm.sender_handle = session[:handle] + @user_repo = UserRepository.new + recipient_id = @user_repo.find_id(params[:recipient_handle]) + sender_id = @user_repo.find_id(session[:handle]) + if @user_repo.find_blocked(recipient_id).include? sender_id + flash[:blocked] = "You are blocked by this user, message can't be sent" + redirect '/send_message' + else + new_dm = DM.new + new_dm.recipient_handle = params[:recipient_handle] + new_dm.contents = params[:contents] + new_dm.sender_handle = session[:handle] - @dm_repo = DMRepository.new - @dm_repo.add(new_dm) - flash[:message_sent] = "Message sent" - redirect '/send_message' + @dm_repo = DMRepository.new + @dm_repo.add(new_dm) + flash[:message_sent] = "Message sent" + redirect '/send_message' + end end end diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 7f06b9e..66c8434 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "line": 99.15 + "line": 95.28 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 1f533fa..13ec2ce 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -1,157 +1,71 @@ { "RSpec": { "coverage": { - "/Users/natashabuckham/Projects/Chitter-DMs/spec/dm_repository_spec.rb": { + "/Users/natashabuckham/Projects/Chitter-DMs/spec/user_repository_spec.rb": { "lines": [ - 1, 1, null, 1, - 2, - 2, - 2, - null, + 7, + 7, + 7, null, - 1, - 3, null, 1, 1, + 7, null, - 1, - 1, null, 1, 1, - null, 1, 1, null, - null, 1, 1, 1, 1, 1, null, - 1, - 1, null, - 1, null, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm_repository.rb": { - "lines": [ 1, - null, 1, 1, - 5, - null, - 5, - 5, - null, - 5, - null, - 5, - 7, - null, - null, - 5, - null, - null, 1, - 7, - 7, - 7, - 7, - 7, - 7, - 7, - null, - null, - 1, - 2, - null, - null, - 2, - null, - 2, - null, null, - null, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm.rb": { - "lines": [ - 1, - 1, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/spec/integration/app_spec.rb": { - "lines": [ - 1, - 1, 1, null, 1, 1, null, - 6, - null, 1, - 5, - 5, - 5, null, null, - 6, null, 1, 1, 1, 1, - 1, - null, - 1, - 1, null, 1, - 1, - 1, null, null, - 1, - 1, - 1, - 1, - null, - 1, - 1, null, 1, 1, 1, - null, - null, - 1, 1, null, 1, 1, null, null, - null, 1, 1, 1, null, 1, - 1, null, null, null, @@ -164,118 +78,58 @@ 1, 1, null, + null, + null, 1, 1, 1, - null, 1, 1, null, 1, 1, - 1, - null, null, null, null ] }, - "/Users/natashabuckham/Projects/Chitter-DMs/app.rb": { + "/Users/natashabuckham/Projects/Chitter-DMs/lib/user_repository.rb": { "lines": [ 1, - 1, - 1, - 1, - 1, - 1, - null, - 1, - null, - 1, - null, null, 1, 1, 1, 1, - 1, - null, - null, - 1, - 3, - 3, - null, null, 1, - 3, - 3, - 2, - 2, null, - 1, + 8, 1, null, null, - null, 1, 0, 0, null, + 0, + 0, + 0, null, - 1, - 7, - 7, - 2, null, - 7, null, null, - 1, - 2, null, null, - 1, - 1, - 1, - 1, - 1, - null, - 1, - 1, - 1, - 1, null, - null - ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/user_repository.rb": { - "lines": [ - 1, null, 1, 1, 1, 1, - null, 1, - null, - 8, 1, - null, - null, 1, - 3, - 3, - null, - 3, - 24, - 3, - null, - null, - null, - null, - null, - null, null, null, 1, @@ -335,91 +189,8 @@ 1, null ] - }, - "/Users/natashabuckham/Projects/Chitter-DMs/spec/user_repository_spec.rb": { - "lines": [ - 1, - null, - 1, - 6, - 6, - 6, - null, - null, - 1, - 1, - 6, - null, - null, - 1, - 1, - 1, - 1, - null, - 1, - 1, - 1, - 1, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - null, - 1, - null, - 1, - 1, - null, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - null, - 1, - 1, - null, - null, - 1, - 1, - 1, - null, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - 1, - null, - 1, - 1, - null, - null, - null, - 1, - 1, - 1, - 1, - 1, - null, - 1, - 1, - null, - null, - null - ] } }, - "timestamp": 1681566610 + "timestamp": 1681567755 } } diff --git a/lib/user_repository.rb b/lib/user_repository.rb index 4964b5b..6795e2f 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -4,9 +4,9 @@ class UserRepository def all sql = 'SELECT * FROM users;' results = DatabaseConnection.exec_params(sql, []) - + users = [] - + results.each{ |record| users << user_builder(record)} return users end @@ -19,29 +19,38 @@ def list_handles results.each{ |record| handles << record['handle'] } return handles end - + # def updated_verified(user) # sql = 'UPDATE users SET verified = $1 WHERE id = $2;' # sql_params = [user.verified, user.id] - # DatabaseConnection.exec_params(sql, sql_params) + # DatabaseConnection.exec_params(sql, sql_params) # end + def find_id(handle) + sql = 'SELECT id FROM users WHERE handle = $1;' + params = [handle] + results = DatabaseConnection.exec_params(sql, params) + user = User.new + user.id = results[0]['id'] + return user + end + def find_handle(id) sql = 'SELECT handle FROM users WHERE id = $1;' params = [id] results = DatabaseConnection.exec_params(sql, params) user = User.new - user.handle = results[0]['handle'] + user.handle = results[0]['handle'] return user end - + def find_blocked(id) sql = 'SELECT blocked.blocked_id FROM users JOIN blocked ON users.id = blocked.blocker_id WHERE blocked.blocker_id = $1;' params = [id] results = DatabaseConnection.exec_params(sql, params) - + block_list = [] results.each { |record| block_list << find_handle(record['blocked_id']) } @@ -55,7 +64,7 @@ def remove_from_blocked_list(blocker_id, blocked_id) DatabaseConnection.exec_params(sql, sql_params) return nil end - + def add_to_blocked_list(blocker_id, blocked_id) sql = 'INSERT INTO blocked (blocker_id, blocked_id) VALUES ($1, $2);' @@ -64,7 +73,7 @@ def add_to_blocked_list(blocker_id, blocked_id) return nil end - private + private def user_builder(record) user = User.new @@ -73,5 +82,5 @@ def user_builder(record) user.verified = record['verified'].eql?('t') ? true : false return user end - -end \ No newline at end of file + +end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 9cae550..364be60 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -60,7 +60,7 @@ def reset_tables end context "post /send_message" do - it "returns 'Message sent'" do + it "returns 'Message sent' when message sent successfully" do response = get("/") expect(response.status).to eq 200 expect(response.body).to include "

You are not logged in

" @@ -79,6 +79,26 @@ def reset_tables expect(response.status).to eq 200 expect(response.body).to include "Message sent" end + + it "tells user they can't send a message to a user who has blocked them" do + response = get("/") + expect(response.status).to eq 200 + expect(response.body).to include "

You are not logged in

" + + response = post("/login", handle: "Bob") + expect(response.status).to eq 302 + + response = get("/") + expect(response.status).to eq 200 + expect(response.body).to include "You are logged in" + + response = post("/send_message", recipient_handle: "Lucy", contents: "Hello") + expect(response.status).to eq 302 + + response = get("/send_message") + expect(response.status).to eq 200 + expect(response.body).to include "You are blocked by this user, message can't be sent" + end end end diff --git a/spec/user_repository_spec.rb b/spec/user_repository_spec.rb index 33aeb6c..156d947 100644 --- a/spec/user_repository_spec.rb +++ b/spec/user_repository_spec.rb @@ -7,10 +7,10 @@ def reset_users_table end describe UserRepository do - before(:each) do + before(:each) do reset_users_table end - + context "#all" do it "returns all users" do repo = UserRepository.new @@ -38,6 +38,15 @@ def reset_users_table end end + context "#find_id" do + it "finds the id based on handle" do + repo = UserRepository.new + user = repo.find_id("Bob") + + expect(user.id).to eq("1") + end + end + context "#find_blocked" do it "returns an array of blocked users based on user_id" do repo = UserRepository.new @@ -76,4 +85,4 @@ def reset_users_table expect(blocked_list.first.handle).to include('Irene') end end -end \ No newline at end of file +end From 16f54c2a24cdb6c9aea3801c0b733f75b6f8996e Mon Sep 17 00:00:00 2001 From: natashabuckham Date: Sat, 15 Apr 2023 15:36:31 +0100 Subject: [PATCH 4/7] post send message route updated to check if user is blocked by recipient --- app.rb | 15 +- coverage/.last_run.json | 2 +- coverage/.resultset.json | 324 +++++++++++++++++++++++++++++++++-- lib/user_repository.rb | 2 +- spec/integration/app_spec.rb | 1 - spec/user_repository_spec.rb | 4 +- 6 files changed, 322 insertions(+), 26 deletions(-) diff --git a/app.rb b/app.rb index 364933e..aaea845 100644 --- a/app.rb +++ b/app.rb @@ -54,8 +54,19 @@ def check_handle_exists(handle) post '/send_message' do @user_repo = UserRepository.new recipient_id = @user_repo.find_id(params[:recipient_handle]) - sender_id = @user_repo.find_id(session[:handle]) - if @user_repo.find_blocked(recipient_id).include? sender_id + + blocked_users = @user_repo.find_blocked(recipient_id.to_i) + + @blocked = false + blocked_users.each do |user| + if user.handle == session[:handle] + @blocked = true + else + next + end + end + + if @blocked flash[:blocked] = "You are blocked by this user, message can't be sent" redirect '/send_message' else diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 66c8434..687541d 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "line": 95.28 + "line": 99.27 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 13ec2ce..213477b 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -1,59 +1,143 @@ { "RSpec": { "coverage": { - "/Users/natashabuckham/Projects/Chitter-DMs/spec/user_repository_spec.rb": { + "/Users/natashabuckham/Projects/Chitter-DMs/spec/dm_repository_spec.rb": { "lines": [ + 1, 1, null, 1, - 7, - 7, - 7, + 2, + 2, + 2, null, null, 1, + 3, + null, + 1, 1, - 7, null, + 1, + 1, null, 1, 1, + null, 1, 1, null, + null, 1, 1, 1, 1, 1, null, + 1, + 1, null, + 1, + null, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm_repository.rb": { + "lines": [ + 1, null, 1, 1, + 6, + null, + 6, + 6, + null, + 6, + null, + 6, + 8, + null, + null, + 6, + null, + null, 1, + 8, + 8, + 8, + 8, + 8, + 8, + 8, + null, + null, 1, + 2, + null, null, + 2, + null, + 2, + null, + null, + null, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/lib/dm.rb": { + "lines": [ + 1, + 1, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/spec/integration/app_spec.rb": { + "lines": [ + 1, + 1, 1, null, 1, 1, null, + 7, + null, 1, + 6, + 6, + 6, null, null, + 7, null, 1, 1, 1, 1, + 1, null, 1, + 1, + null, + 1, + 1, + 1, null, null, + 1, + 1, + 1, + 1, null, 1, 1, + null, + 1, + 1, + 1, + null, + null, 1, 1, null, @@ -61,97 +145,207 @@ 1, null, null, + null, 1, 1, 1, null, 1, + 1, + null, null, null, + 1, + 1, + 1, + 1, + 1, + null, + 1, + 1, null, 1, 1, 1, + null, 1, 1, null, 1, 1, + 1, + null, null, + 1, + 1, + 1, + 1, null, + 1, + 1, null, 1, 1, 1, + null, 1, 1, null, 1, 1, + 1, null, null, null ] }, - "/Users/natashabuckham/Projects/Chitter-DMs/lib/user_repository.rb": { + "/Users/natashabuckham/Projects/Chitter-DMs/app.rb": { "lines": [ + 1, + 1, + 1, + 1, + 1, + 1, + null, 1, null, 1, + null, + null, + 1, + 1, 1, 1, 1, + 1, + null, null, 1, + 4, + 4, null, - 8, + null, + 1, + 4, + 4, + 3, + 3, + null, + 1, 1, null, null, + null, 1, 0, 0, null, - 0, - 0, - 0, + null, + 1, + 9, + 9, + 3, + null, + 9, null, null, + 1, + 3, null, null, + 1, + 2, + 2, null, + 2, null, + 2, + 2, + 2, + 1, + null, + 1, null, null, + null, + 2, + 1, + 1, + null, 1, 1, 1, 1, + null, + 1, 1, 1, 1, null, null, + null + ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/lib/user_repository.rb": { + "lines": [ 1, - 6, - 6, - 6, - 6, - 6, - 6, null, + 1, + 1, + 1, + 1, null, 1, - 4, + null, + 8, + 1, null, null, + 1, 4, 4, null, 4, + 32, + 4, + null, + null, + null, + null, null, + null, + null, + null, + 1, + 3, + 3, + 3, + 3, + 3, + 3, + null, + null, + 1, 8, - 4, + 8, + 8, + 8, + 8, + 8, + null, + null, + 1, + 6, + null, + null, + 6, + 6, + null, + 6, + null, + 12, + 6, null, null, 1, @@ -189,8 +383,100 @@ 1, null ] + }, + "/Users/natashabuckham/Projects/Chitter-DMs/spec/user_repository_spec.rb": { + "lines": [ + 1, + null, + 1, + 7, + 7, + 7, + null, + null, + 1, + 1, + 7, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + 1, + 1, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + 1, + 1, + 1, + null, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + null, + 1, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + null, + null + ] } }, - "timestamp": 1681567755 + "timestamp": 1681569340 } } diff --git a/lib/user_repository.rb b/lib/user_repository.rb index 6795e2f..abf1f0c 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -32,7 +32,7 @@ def find_id(handle) results = DatabaseConnection.exec_params(sql, params) user = User.new user.id = results[0]['id'] - return user + return user.id end def find_handle(id) diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 364be60..f11abc6 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -101,4 +101,3 @@ def reset_tables end end end - diff --git a/spec/user_repository_spec.rb b/spec/user_repository_spec.rb index 156d947..f1965a1 100644 --- a/spec/user_repository_spec.rb +++ b/spec/user_repository_spec.rb @@ -41,9 +41,9 @@ def reset_users_table context "#find_id" do it "finds the id based on handle" do repo = UserRepository.new - user = repo.find_id("Bob") + user_id = repo.find_id("Bob") - expect(user.id).to eq("1") + expect(user_id).to eq("1") end end From 52ec8c9a855aaa43a1be1cfdf1c461026eecc3ff Mon Sep 17 00:00:00 2001 From: natashabuckham Date: Sat, 15 Apr 2023 15:37:33 +0100 Subject: [PATCH 5/7] added link to go back to homepage from send message page --- views/send_message.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/views/send_message.erb b/views/send_message.erb index b15ed77..efa3020 100644 --- a/views/send_message.erb +++ b/views/send_message.erb @@ -16,5 +16,6 @@ +

Go back to the homepage.

From 5c0eab805fb160129d31104ee5eae8b611601ecc Mon Sep 17 00:00:00 2001 From: natashabuckham Date: Sat, 15 Apr 2023 15:57:22 +0100 Subject: [PATCH 6/7] added recipient handle validation to send message --- app.rb | 27 ++++++---- coverage/.last_run.json | 2 +- coverage/.resultset.json | 101 ++++++++++++++++++++++------------- spec/integration/app_spec.rb | 20 +++++++ 4 files changed, 102 insertions(+), 48 deletions(-) diff --git a/app.rb b/app.rb index aaea845..ef423a3 100644 --- a/app.rb +++ b/app.rb @@ -52,24 +52,28 @@ def check_handle_exists(handle) end post '/send_message' do - @user_repo = UserRepository.new - recipient_id = @user_repo.find_id(params[:recipient_handle]) + @blocked = false - blocked_users = @user_repo.find_blocked(recipient_id.to_i) + if check_handle_exists(params[:recipient_handle]) + @user_repo = UserRepository.new + recipient_id = @user_repo.find_id(params[:recipient_handle]) - @blocked = false - blocked_users.each do |user| - if user.handle == session[:handle] - @blocked = true - else - next + blocked_users = @user_repo.find_blocked(recipient_id.to_i) + + + blocked_users.each do |user| + if user.handle == session[:handle] + @blocked = true + else + next + end end end if @blocked flash[:blocked] = "You are blocked by this user, message can't be sent" redirect '/send_message' - else + elsif check_handle_exists(params[:recipient_handle]) new_dm = DM.new new_dm.recipient_handle = params[:recipient_handle] new_dm.contents = params[:contents] @@ -79,6 +83,9 @@ def check_handle_exists(handle) @dm_repo.add(new_dm) flash[:message_sent] = "Message sent" redirect '/send_message' + else + flash[:invalid_handle] = "Handle Does Not Exist" + redirect '/send_message' end end end diff --git a/coverage/.last_run.json b/coverage/.last_run.json index 687541d..7c979d6 100644 --- a/coverage/.last_run.json +++ b/coverage/.last_run.json @@ -1,5 +1,5 @@ { "result": { - "line": 99.27 + "line": 99.31 } } diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 213477b..2d3a7e7 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -48,28 +48,28 @@ null, 1, 1, - 6, + 7, null, - 6, - 6, + 7, + 7, null, - 6, + 7, null, - 6, - 8, + 7, + 9, null, null, - 6, + 7, null, null, 1, - 8, - 8, - 8, - 8, - 8, - 8, - 8, + 9, + 9, + 9, + 9, + 9, + 9, + 9, null, null, 1, @@ -101,15 +101,15 @@ 1, 1, null, - 7, + 8, null, 1, - 6, - 6, - 6, + 7, + 7, + 7, null, null, - 7, + 8, null, 1, 1, @@ -196,6 +196,26 @@ 1, null, null, + 1, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + 1, + 1, + null, + 1, + 1, + 1, + null, + null, null ] }, @@ -222,15 +242,15 @@ null, null, 1, - 4, - 4, + 10, + 10, null, null, 1, + 5, + 5, 4, 4, - 3, - 3, null, 1, 1, @@ -243,24 +263,27 @@ null, null, 1, - 9, - 9, - 3, + 11, + 11, + 4, null, - 9, + 11, null, null, 1, - 3, + 4, null, null, 1, + 3, + null, + 3, 2, 2, null, 2, null, - 2, + null, 2, 2, 1, @@ -269,10 +292,11 @@ null, null, null, - 2, + null, + 3, 1, 1, - null, + 2, 1, 1, 1, @@ -283,6 +307,9 @@ 1, 1, null, + 1, + 1, + null, null, null ] @@ -303,12 +330,12 @@ null, null, 1, - 4, - 4, + 10, + 10, null, - 4, - 32, - 4, + 10, + 80, + 10, null, null, null, @@ -477,6 +504,6 @@ ] } }, - "timestamp": 1681569340 + "timestamp": 1681570582 } } diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index f11abc6..5f6a9c7 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -99,5 +99,25 @@ def reset_tables expect(response.status).to eq 200 expect(response.body).to include "You are blocked by this user, message can't be sent" end + + it "checks if recipient handle is valid" do + response = get("/") + expect(response.status).to eq 200 + expect(response.body).to include "

You are not logged in

" + + response = post("/login", handle: "Bob") + expect(response.status).to eq 302 + + response = get("/") + expect(response.status).to eq 200 + expect(response.body).to include "You are logged in" + + response = post("/send_message", recipient_handle: "Natasha", contents: "Hello") + expect(response.status).to eq 302 + + response = get("/send_message") + expect(response.status).to eq 200 + expect(response.body).to include "Handle Does Not Exist" + end end end From cc756a494a5fd73d309a633d8439da9cc2fd7d3e Mon Sep 17 00:00:00 2001 From: natashabuckham Date: Sat, 15 Apr 2023 16:12:30 +0100 Subject: [PATCH 7/7] refactored post send_message route to move blocked? into a helper method --- app.rb | 93 +++++++++++++++++------------------- coverage/.resultset.json | 55 ++++++++++----------- lib/user_repository.rb | 2 +- spec/user_repository_spec.rb | 2 +- 4 files changed, 71 insertions(+), 81 deletions(-) diff --git a/app.rb b/app.rb index ef423a3..2097c1e 100644 --- a/app.rb +++ b/app.rb @@ -1,9 +1,9 @@ -require 'sinatra/base' -require 'sinatra/reloader' -require_relative './lib/user_repository' -require_relative './lib/dm_repository' -require 'sinatra/flash' -require_relative './lib/database_connection' +require "sinatra/base" +require "sinatra/reloader" +require_relative "./lib/user_repository" +require_relative "./lib/dm_repository" +require "sinatra/flash" +require_relative "./lib/database_connection" DatabaseConnection.connect @@ -14,66 +14,44 @@ class Application < Sinatra::Base register Sinatra::Reloader register Sinatra::Flash enable :sessions - also_reload './lib/user_repository' - also_reload './lib/dm_repository' + also_reload "./lib/user_repository" + also_reload "./lib/dm_repository" end - def check_handle_exists(handle) - user_repo = UserRepository.new - return user_repo.list_handles.include?(handle) - end - - post '/login' do + post "/login" do handle = params[:handle] if check_handle_exists(handle) session[:handle] = handle - redirect '/' + redirect "/" else flash[:invalid_handle] = "Handle Does Not Exist" - redirect '/' + redirect "/" end end - get '/logout' do + get "/logout" do session[:handle] = nil - redirect '/' + redirect "/" end - get '/' do + get "/" do @dm_repo = DMRepository.new - if session[:handle] - @inbox = @dm_repo.find_inbox(session[:handle]) - end + @inbox = @dm_repo.find_inbox(session[:handle]) if session[:handle] return erb(:index) end - get '/send_message' do + get "/send_message" do return erb(:send_message) end - post '/send_message' do - @blocked = false - - if check_handle_exists(params[:recipient_handle]) - @user_repo = UserRepository.new - recipient_id = @user_repo.find_id(params[:recipient_handle]) - - blocked_users = @user_repo.find_blocked(recipient_id.to_i) - - - blocked_users.each do |user| - if user.handle == session[:handle] - @blocked = true - else - next - end - end - end - - if @blocked + post "/send_message" do + if !check_handle_exists(params[:recipient_handle]) + flash[:invalid_handle] = "Handle Does Not Exist" + redirect "/send_message" + elsif blocked?(session[:handle], params[:recipient_handle]) flash[:blocked] = "You are blocked by this user, message can't be sent" - redirect '/send_message' - elsif check_handle_exists(params[:recipient_handle]) + redirect "/send_message" + else new_dm = DM.new new_dm.recipient_handle = params[:recipient_handle] new_dm.contents = params[:contents] @@ -82,10 +60,27 @@ def check_handle_exists(handle) @dm_repo = DMRepository.new @dm_repo.add(new_dm) flash[:message_sent] = "Message sent" - redirect '/send_message' - else - flash[:invalid_handle] = "Handle Does Not Exist" - redirect '/send_message' + redirect "/send_message" + end + end + + helpers do + def check_handle_exists(handle) + user_repo = UserRepository.new + return user_repo.list_handles.include?(handle) + end + + def blocked?(sender_handle, recipient_handle) + user_repo = UserRepository.new + recipient_id = user_repo.find_id(recipient_handle) + blocked_users = user_repo.find_blocked(recipient_id) + + blocked = false + blocked_users.each do |user| + user.handle == sender_handle ? blocked = true : next + end + + return blocked end end end diff --git a/coverage/.resultset.json b/coverage/.resultset.json index 2d3a7e7..681fdf4 100644 --- a/coverage/.resultset.json +++ b/coverage/.resultset.json @@ -242,11 +242,6 @@ null, null, 1, - 10, - 10, - null, - null, - 1, 5, 5, 4, @@ -265,8 +260,6 @@ 1, 11, 11, - 4, - null, 11, null, null, @@ -276,27 +269,12 @@ null, 1, 3, - null, - 3, - 2, - 2, - null, - 2, - null, - null, - 2, - 2, 1, - null, 1, - null, - null, - null, - null, - 3, + 2, 1, 1, - 2, + null, 1, 1, 1, @@ -307,8 +285,25 @@ 1, 1, null, + null, + null, 1, 1, + 8, + 8, + null, + null, + 1, + 2, + 2, + 2, + null, + 2, + 2, + 2, + null, + null, + 2, null, null, null @@ -330,12 +325,12 @@ null, null, 1, - 10, - 10, + 8, + 8, null, - 10, - 80, - 10, + 8, + 64, + 8, null, null, null, @@ -504,6 +499,6 @@ ] } }, - "timestamp": 1681570582 + "timestamp": 1681571505 } } diff --git a/lib/user_repository.rb b/lib/user_repository.rb index abf1f0c..0e2f527 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -31,7 +31,7 @@ def find_id(handle) params = [handle] results = DatabaseConnection.exec_params(sql, params) user = User.new - user.id = results[0]['id'] + user.id = results[0]['id'].to_i return user.id end diff --git a/spec/user_repository_spec.rb b/spec/user_repository_spec.rb index f1965a1..4cbf234 100644 --- a/spec/user_repository_spec.rb +++ b/spec/user_repository_spec.rb @@ -43,7 +43,7 @@ def reset_users_table repo = UserRepository.new user_id = repo.find_id("Bob") - expect(user_id).to eq("1") + expect(user_id).to eq(1) end end