From 58aa6f734860d2e57697ef31eb049de93f048ab9 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Sun, 9 Apr 2023 11:19:48 +0100 Subject: [PATCH 01/28] Project setup --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app.rb | 12 ++++++++++++ lib/database_connection.rb | 26 ++++++++++++++++++++++++++ lib/peeps.rb | 3 +++ lib/peeps_repository.rb | 0 lib/users.rb | 3 +++ lib/users_repository.rb | 0 spec/seeds.sql | 9 +++++++++ spec/spec_helper.rb | 5 +++++ 10 files changed, 62 insertions(+) create mode 100644 app.rb create mode 100644 lib/database_connection.rb create mode 100644 lib/peeps.rb create mode 100644 lib/peeps_repository.rb create mode 100644 lib/users.rb create mode 100644 lib/users_repository.rb create mode 100644 spec/seeds.sql diff --git a/Gemfile b/Gemfile index b1a320395a..ff34d824e1 100644 --- a/Gemfile +++ b/Gemfile @@ -11,3 +11,5 @@ end group :development, :test do gem 'rubocop', '1.20' end + +gem "pg", "~> 1.4" diff --git a/Gemfile.lock b/Gemfile.lock index 66064703c7..ea8986d935 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,7 @@ GEM parallel (1.20.1) parser (3.0.2.0) ast (~> 2.4.1) + pg (1.4.6) rainbow (3.0.0) regexp_parser (2.1.1) rexml (3.2.5) @@ -54,6 +55,7 @@ PLATFORMS ruby DEPENDENCIES + pg (~> 1.4) rspec rubocop (= 1.20) simplecov diff --git a/app.rb b/app.rb new file mode 100644 index 0000000000..524ddbb9ac --- /dev/null +++ b/app.rb @@ -0,0 +1,12 @@ +require_relative 'lib/database_connection' + +DatabaseConnection.connect('chitter') + +CREATE TABLE peeps ( +id SERIAL PRIMARY KEY, +peep text, +date timestamp, +username_id int, +constraint fk_username foreign key(username_id) +references users(id) +); \ No newline at end of file diff --git a/lib/database_connection.rb b/lib/database_connection.rb new file mode 100644 index 0000000000..ac9ec32a31 --- /dev/null +++ b/lib/database_connection.rb @@ -0,0 +1,26 @@ +require 'pg' + +# This class is a thin "wrapper" around the +# PG library. We'll use it in our project to interact +# with the database using SQL. + +class DatabaseConnection + # This method connects to PostgreSQL using the + # PG gem. We connect to 127.0.0.1, and select + # the database name given in argument. + def self.connect(database_name) + @connection = PG.connect({ host: '127.0.0.1', dbname: database_name }) + end + + # This method executes an SQL query + # on the database, providing some optional parameters + # (you will learn a bit later about when to provide these parameters). + def self.exec_params(query, params) + if @connection.nil? + raise 'DatabaseConnection.exec_params: Cannot run a SQL query as the connection to'\ + 'the database was never opened. Did you make sure to call first the method '\ + '`DatabaseConnection.connect` in your app.rb file (or in your tests spec_helper.rb)?' + end + @connection.exec_params(query, params) + end +end \ No newline at end of file diff --git a/lib/peeps.rb b/lib/peeps.rb new file mode 100644 index 0000000000..9c6e8f805e --- /dev/null +++ b/lib/peeps.rb @@ -0,0 +1,3 @@ +class Peep + +end \ No newline at end of file diff --git a/lib/peeps_repository.rb b/lib/peeps_repository.rb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/users.rb b/lib/users.rb new file mode 100644 index 0000000000..b38d91d6d3 --- /dev/null +++ b/lib/users.rb @@ -0,0 +1,3 @@ +class User + +end \ No newline at end of file diff --git a/lib/users_repository.rb b/lib/users_repository.rb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spec/seeds.sql b/spec/seeds.sql new file mode 100644 index 0000000000..cefe993045 --- /dev/null +++ b/spec/seeds.sql @@ -0,0 +1,9 @@ +TRUNCATE TABLE peeps RESTART IDENTITY; + +INSERT INTO users (username, email, password_hash) VALUES + ('user1', 'user1@example.com', ''), + ('user2', 'user2@example.com', ''); + +INSERT INTO peeps (username_id, peep, date) VALUES + (1, 'Test peep 1', '2023-04-10 12:34:56'), + (2, 'Test peep 2', '2023-04-11 15:30:00'); diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 252747d899..4e57aafeee 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,5 +1,10 @@ require 'simplecov' require 'simplecov-console' +require 'database_connection' + +# Make sure this connects to your test database +# (its name should end with '_test') +DatabaseConnection.connect('chitter_test') SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, From 03e9560cc6a96b8b904109ae20e09f36192492bc Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Sun, 9 Apr 2023 11:40:17 +0100 Subject: [PATCH 02/28] setting up my test seeds --- backup.tar | Bin 0 -> 13824 bytes lib/peeps.rb | 2 +- lib/users.rb | 2 +- spec/seeds.sql | 8 +++++--- 4 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 backup.tar diff --git a/backup.tar b/backup.tar new file mode 100644 index 0000000000000000000000000000000000000000..0e98bf845a4d29a6e6470653aeb2fe3832900efa GIT binary patch literal 13824 zcmeHNZExeo5!SV7o2YHtpg@}-ZMtm?$OqUe^;E<-sy!8>h}N=ezbo^QGtO`ABLf8%mbzMs7=DhEsO zK;!UWv+e&+pD$n5FD{?FpLyrq?+XooikBTc{TNU0;fad=f_G2w^dtE7hL-PhH}f;N zFO>6#Z1>C=^J|yiJxzS~3vBrD`*_9!lyr=G$7q8poy^Q5 zX|sz}KpXHHyKlPvZqKm?jynd8x}NXC#Y4>;dp*5=ldhLuZP(5&tHznoD|{rqj!3VD zUNhL_rrR@pbL8CQJ?@i&*3E9t9Dg#t5$7br5v}rFJI)r8TQc|GQYwu+pWu(_+(2nKY{ulzwX}Mk4AiK+KCSHU#KJw zVer8-X>dv0O*o%;^bU+|1h)PSngq7eTQ<499$CFSRc5?KG0qoQOUd0qg^KF;%GbWmB(%Z6ctaLs7M1 z=xsKwpt@NA9FvVAsOGJ{8WoBUtYS|=vFG2QjW}X{piy|BhsFZMg(gXXLX{TBC7`dTY0)Ry02RH$mi zF1i)iWRLar;l=xlA)LptUgVi zb^Ww@)i6YO_o0CZZ;`rN&Bm)p3^P$$Yc;MenmC={`Xms+Y?wd3Hb=X;gp`mwK6d#{ zZv?S{Q^es+M{0{$A7X+PdOmE}3o^?SpNv6wbtc~qwQM#J%*mT z$8a^W$I!@Nwegglwp$mlE{4T9I8(ToP^6}>w4!5=i^ft#JEAYsU#1wN&L;DXsm&N8 zhK7uh2JA7KF~B-?qIeXWf`JKas*p{;OHFT)Ngwf=&gl@^@_k7!6FLH}bm(^RGjx{~=hATAQL7@o-+eotRvwN?{8 z7OUq?gYtT&2^Ht&YB5pQ>nvUOX{_ zu;5d9wN>ihQ{gEc5e7KCzQD=JVK{gctUv(gGWuc|4fCad`OEF;XQ*l zUF;n*W(HfFex~?=@F3iYTiJsYx$Lyn*3Vxyg$v2vAP%3hwtlL&@q?=)mk$I*;Afas z=5`$OGo{x!k}G9>GQCuWO$6L&Di%~|;l=jY90Jp6c5_|8=IXrtUo1U{?f(`Jw)?*u z9FnQ%*ZFJ~S5MpH?L+|LUo1&CX#QI9U#@KVpAC+Q6upgqiNxD^1;*2NT%Py78f^@K z`SD-SN|n;={C`k7ENv3lJL{2e(DCD37yzg5s?}4-3GvG{p)3Sjq z#dcT*yOgfkgC1~W$z%i2!OIJUu*#(%=R2P_sQlXWK-}$kf;F(b4)@S@;5y^5!q>nv zSdI;@6``k<`q%eJmZBh*RyiAxy}(2w`;7b>vTo0rc0xrXr}rlQ*6NRdfwQ|{lnCYl}CYwPFlH))qQm1GUbPz$R)~SoqC&G_xZrQ9YK7W*N{0Tn1K|HXOsMKmUMs!)~>;`^neCTNl7sxkY6 z&07hB+=yNo3E5EYI*C10@kfA-6PM{g1m+O~m=wCp;xVNVmj^~9k$JdGg+@RlUNY*b zw1p>!=n*`XvDv9;%JC2mvKv~xhc0)g?~40scNv4TdEDD4@|vE{@>%F zZmD(>b4w~ra7(e@4Sz0?GkCxhPbct2S#l|h#VL(r{NN8 zJ^RFXtW5vTn7OKyh_$rk7Fw$eVoG+y|8y*GZQ>Wfa_q1i0HKoVh!Xy3f_Yq()UKjD zZ&W3bS;QIwiAJebNPsSn)7&JJ#6^bPCUB8aJ%xV=t2U)4Nl(%<>~^7^0#B983sQE3 roZKL$O4sX>mlFB3Z36gih5r$ApVJ3oxcRZY+aj<'), ('user2', 'user2@example.com', ''); -INSERT INTO peeps (username_id, peep, date) VALUES - (1, 'Test peep 1', '2023-04-10 12:34:56'), - (2, 'Test peep 2', '2023-04-11 15:30:00'); +INSERT INTO peeps (peep, date, username_id) VALUES + ('Test peep 1', '2023-04-10 12:34:56', 1), + ('Test peep 2', '2023-04-11 15:30:00', 2), + ('Test peep 3', '2023-04-11 20:30:00', 2); + From 52a309e6f35e3a9d42570a351088768838443983 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Sun, 9 Apr 2023 12:23:53 +0100 Subject: [PATCH 03/28] sinatra app setup --- Gemfile | 5 +++++ Gemfile.lock | 26 ++++++++++++++++++++++++++ app.rb | 19 ++++++++++--------- config.ru | 2 ++ lib/peeps_repository.rb | 1 + spec/integration/app_spec.rb | 19 +++++++++++++++++++ 6 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 config.ru create mode 100644 spec/integration/app_spec.rb diff --git a/Gemfile b/Gemfile index ff34d824e1..494eca7c08 100644 --- a/Gemfile +++ b/Gemfile @@ -13,3 +13,8 @@ group :development, :test do end gem "pg", "~> 1.4" + +gem "sinatra", "~> 3.0" +gem "sinatra-contrib", "~> 3.0" +gem "webrick", "~> 1.8" +gem "rack-test", "~> 2.1" diff --git a/Gemfile.lock b/Gemfile.lock index ea8986d935..0b085d0b88 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -5,10 +5,18 @@ GEM ast (2.4.2) diff-lcs (1.4.4) docile (1.4.0) + multi_json (1.15.0) + mustermann (3.0.0) + ruby2_keywords (~> 0.0.1) parallel (1.20.1) parser (3.0.2.0) ast (~> 2.4.1) pg (1.4.6) + rack (2.2.6.4) + rack-protection (3.0.5) + rack + rack-test (2.1.0) + rack (>= 1.3) rainbow (3.0.0) regexp_parser (2.1.1) rexml (3.2.5) @@ -37,6 +45,7 @@ GEM rubocop-ast (1.11.0) parser (>= 3.0.1.1) ruby-progressbar (1.11.0) + ruby2_keywords (0.0.5) simplecov (0.21.2) docile (~> 1.1) simplecov-html (~> 0.11) @@ -47,19 +56,36 @@ GEM terminal-table simplecov-html (0.12.3) simplecov_json_formatter (0.1.3) + sinatra (3.0.5) + mustermann (~> 3.0) + rack (~> 2.2, >= 2.2.4) + rack-protection (= 3.0.5) + tilt (~> 2.0) + sinatra-contrib (3.0.5) + multi_json + mustermann (~> 3.0) + rack-protection (= 3.0.5) + sinatra (= 3.0.5) + tilt (~> 2.0) terminal-table (3.0.1) unicode-display_width (>= 1.1.1, < 3) + tilt (2.1.0) unicode-display_width (2.0.0) + webrick (1.8.1) PLATFORMS ruby DEPENDENCIES pg (~> 1.4) + rack-test (~> 2.1) rspec rubocop (= 1.20) simplecov simplecov-console + sinatra (~> 3.0) + sinatra-contrib (~> 3.0) + webrick (~> 1.8) RUBY VERSION ruby 3.0.2p107 diff --git a/app.rb b/app.rb index 524ddbb9ac..d6a8bd12fd 100644 --- a/app.rb +++ b/app.rb @@ -1,12 +1,13 @@ require_relative 'lib/database_connection' +require 'sinatra/base' +require 'sinatra/reloader' -DatabaseConnection.connect('chitter') +class Application < Sinatra::Base + # This allows the app code to refresh + # without having to restart the server. + configure :development do + register Sinatra::Reloader + end +end -CREATE TABLE peeps ( -id SERIAL PRIMARY KEY, -peep text, -date timestamp, -username_id int, -constraint fk_username foreign key(username_id) -references users(id) -); \ No newline at end of file +DatabaseConnection.connect('chitter') diff --git a/config.ru b/config.ru new file mode 100644 index 0000000000..af14ef717e --- /dev/null +++ b/config.ru @@ -0,0 +1,2 @@ +require './app' +run Application diff --git a/lib/peeps_repository.rb b/lib/peeps_repository.rb index e69de29bb2..e5721f080f 100644 --- a/lib/peeps_repository.rb +++ b/lib/peeps_repository.rb @@ -0,0 +1 @@ +require \ No newline at end of file diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb new file mode 100644 index 0000000000..6618cb96c4 --- /dev/null +++ b/spec/integration/app_spec.rb @@ -0,0 +1,19 @@ +require relative '../../app' +require "spec_helper" +require "rack/test" + +describe Application do + # This is so we can use rack-test helper methods. + include Rack::Test::Methods + + # We need to declare the `app` value by instantiating the Application + # class so our tests work. + let(:app) { Application.new } + + context 'GET to /' do + it 'displays all peeps in reverse chronological order' do + response = get('/') + expect(response.status).to eq 200 + expect(response.body).to include('') + end + end \ No newline at end of file From 51fa4430c17f12ddee5acdf73d7d5071a5ef39ea Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Sun, 9 Apr 2023 13:30:55 +0100 Subject: [PATCH 04/28] unit test peep_repository expectations set --- app.rb | 3 +++ lib/{peeps.rb => peep.rb} | 0 lib/peep_repository.rb | 5 +++++ lib/peeps_repository.rb | 1 - lib/{users.rb => user.rb} | 0 lib/user_repository.rb | 1 + lib/users_repository.rb | 0 spec/integration/app_spec.rb | 2 +- spec/peep_repository_test.rb | 43 ++++++++++++++++++++++++++++++++++++ spec/spec_helper.rb | 2 +- spec/user_repository_test.rb | 25 +++++++++++++++++++++ 11 files changed, 79 insertions(+), 3 deletions(-) rename lib/{peeps.rb => peep.rb} (100%) create mode 100644 lib/peep_repository.rb delete mode 100644 lib/peeps_repository.rb rename lib/{users.rb => user.rb} (100%) create mode 100644 lib/user_repository.rb delete mode 100644 lib/users_repository.rb create mode 100644 spec/peep_repository_test.rb create mode 100644 spec/user_repository_test.rb diff --git a/app.rb b/app.rb index d6a8bd12fd..c849e822f6 100644 --- a/app.rb +++ b/app.rb @@ -1,4 +1,6 @@ require_relative 'lib/database_connection' +require_relative 'lib/user_repository.rb' +require_relative 'lib/peep_repository.rb' require 'sinatra/base' require 'sinatra/reloader' @@ -11,3 +13,4 @@ class Application < Sinatra::Base end DatabaseConnection.connect('chitter') + diff --git a/lib/peeps.rb b/lib/peep.rb similarity index 100% rename from lib/peeps.rb rename to lib/peep.rb diff --git a/lib/peep_repository.rb b/lib/peep_repository.rb new file mode 100644 index 0000000000..8c365eefc4 --- /dev/null +++ b/lib/peep_repository.rb @@ -0,0 +1,5 @@ +require_relative 'peep' + +class PeepRepository + +end \ No newline at end of file diff --git a/lib/peeps_repository.rb b/lib/peeps_repository.rb deleted file mode 100644 index e5721f080f..0000000000 --- a/lib/peeps_repository.rb +++ /dev/null @@ -1 +0,0 @@ -require \ No newline at end of file diff --git a/lib/users.rb b/lib/user.rb similarity index 100% rename from lib/users.rb rename to lib/user.rb diff --git a/lib/user_repository.rb b/lib/user_repository.rb new file mode 100644 index 0000000000..e0465a6edf --- /dev/null +++ b/lib/user_repository.rb @@ -0,0 +1 @@ +require 'user.rb' \ No newline at end of file diff --git a/lib/users_repository.rb b/lib/users_repository.rb deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 6618cb96c4..85f56c8caa 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -1,4 +1,4 @@ -require relative '../../app' +require_relative '../../app' require "spec_helper" require "rack/test" diff --git a/spec/peep_repository_test.rb b/spec/peep_repository_test.rb new file mode 100644 index 0000000000..65db877f08 --- /dev/null +++ b/spec/peep_repository_test.rb @@ -0,0 +1,43 @@ +require 'peep_repository.rb' +require 'peep.rb' + + +def reset_peeps_table + seed_sql = File.read('spec/seeds.sql') + connection = PG.connect({ host: '127.0.0.1', dbname: 'peeps_test' }) + connection.exec(seed_sql) +end + +describe PeepRepository do + before(:each) do + reset_peeps_table + end + + let(:repo) {PeepRepository.new} + let(:new_peep) {Peep.new} + # write your let(:peep_repositoty) {PeepRepository.new} here + # as well as for user_repository + # see which one is needed as you write each test + + describe '#all' do + it 'returns all peeps' do + peeps = repo.all + expect(peeps[0].peep).to eq 'Test peep 1' + expect(peeps[1].peep).to eq 'Test peep 2' + end + end + + describe '#create' do + xit 'creates and stores a new peep' do + new_peep.peep = 'Test peep 1' + new_peep.date = '2023-04-10 12:34:56' + new_peep.username_id = 1 # maybe put in quotes? + repo.create(new_peep) + + peeps = repo.all + expect(peeps.last.peep).to eq 'Test peep 1' + expect(peeps.last.date).to eq '2023-04-10 12:34:56' + expect(peeps.last.username_id).to eq 1 + end + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4e57aafeee..7e0c982965 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -4,7 +4,7 @@ # Make sure this connects to your test database # (its name should end with '_test') -DatabaseConnection.connect('chitter_test') +DatabaseConnection.connect('peeps_test') SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, diff --git a/spec/user_repository_test.rb b/spec/user_repository_test.rb new file mode 100644 index 0000000000..2576d9094d --- /dev/null +++ b/spec/user_repository_test.rb @@ -0,0 +1,25 @@ +require 'user.rb' +require 'user_repository.rb' + +def reset_users_table + seed_sql = File.read('spec/seeds.sql') + connection = PG.connect({ host: '127.0.0.1', dbname: 'peeps_test' }) + connection.exec(seed_sql) +end + +describe UserRepository do + before(:each) do + reset_users_table + end + + let(:repo) {UserRepository.new} + # write your let(:peep_repositoty) {PeepRepository.new} here + # as well as for user_repository + # see which one is needed as you write each test + + describe '#create' do + it 'creates and stores a new peep' do + + end + end +end \ No newline at end of file From a8f4338c8c95a6f55aa1529e757d9bdbf726f691 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Sun, 9 Apr 2023 15:22:46 +0100 Subject: [PATCH 05/28] correctly returns all peeps in reverse chronological order --- lib/peep.rb | 2 +- lib/peep_repository.rb | 19 ++++++++++++++++++- spec/peep_repository_test.rb | 6 +----- spec/seeds.sql | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/lib/peep.rb b/lib/peep.rb index 854eb83d2e..48d233eb50 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,3 +1,3 @@ class Peep - attr_accessor :peep, :date, :username_id + attr_accessor :id, :peep, :date, :username_id end \ No newline at end of file diff --git a/lib/peep_repository.rb b/lib/peep_repository.rb index 8c365eefc4..dc37310da1 100644 --- a/lib/peep_repository.rb +++ b/lib/peep_repository.rb @@ -1,5 +1,22 @@ require_relative 'peep' +require_relative 'database_connection' class PeepRepository - + + def all + # returns all rows in reverse chronological order + sql = 'SELECT peep, date, username_id FROM peeps;' + result_set = DatabaseConnection.exec_params(sql, []) + + peeps = [] + result_set.each do |row| + new_peep = Peep.new + new_peep.id = row['id'] + new_peep.peep = row['peep'] + new_peep.date = row['date'] + new_peep.username_id = row['username_id'] + peeps << new_peep + end + return peeps + end end \ No newline at end of file diff --git a/spec/peep_repository_test.rb b/spec/peep_repository_test.rb index 65db877f08..c49b1a2bab 100644 --- a/spec/peep_repository_test.rb +++ b/spec/peep_repository_test.rb @@ -1,7 +1,6 @@ require 'peep_repository.rb' require 'peep.rb' - def reset_peeps_table seed_sql = File.read('spec/seeds.sql') connection = PG.connect({ host: '127.0.0.1', dbname: 'peeps_test' }) @@ -15,12 +14,9 @@ def reset_peeps_table let(:repo) {PeepRepository.new} let(:new_peep) {Peep.new} - # write your let(:peep_repositoty) {PeepRepository.new} here - # as well as for user_repository - # see which one is needed as you write each test describe '#all' do - it 'returns all peeps' do + it 'returns all peeps in reverse chronological order' do peeps = repo.all expect(peeps[0].peep).to eq 'Test peep 1' expect(peeps[1].peep).to eq 'Test peep 2' diff --git a/spec/seeds.sql b/spec/seeds.sql index 37720315ee..f70d54004c 100644 --- a/spec/seeds.sql +++ b/spec/seeds.sql @@ -7,5 +7,5 @@ INSERT INTO users (username, email, password_hash) VALUES INSERT INTO peeps (peep, date, username_id) VALUES ('Test peep 1', '2023-04-10 12:34:56', 1), ('Test peep 2', '2023-04-11 15:30:00', 2), - ('Test peep 3', '2023-04-11 20:30:00', 2); + ('Test peep 3', '2023-04-12 20:30:00', 2); From 3fdcc0bf1851c53493e7917078b01aa2ee523954 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Sun, 9 Apr 2023 15:37:18 +0100 Subject: [PATCH 06/28] create method to add a new peep --- lib/peep_repository.rb | 7 +++++++ spec/peep_repository_test.rb | 6 +++--- spec/user_repository_test.rb | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/peep_repository.rb b/lib/peep_repository.rb index dc37310da1..9159559941 100644 --- a/lib/peep_repository.rb +++ b/lib/peep_repository.rb @@ -19,4 +19,11 @@ def all end return peeps end + + def create(message) + sql = 'INSERT INTO peeps (peep, date, username_id) VALUES ($1, $2, $3);' + result_set = DatabaseConnection.exec_params(sql, [message.peep, message.date, message.username_id]) + + return message + end end \ No newline at end of file diff --git a/spec/peep_repository_test.rb b/spec/peep_repository_test.rb index c49b1a2bab..f2034af385 100644 --- a/spec/peep_repository_test.rb +++ b/spec/peep_repository_test.rb @@ -24,16 +24,16 @@ def reset_peeps_table end describe '#create' do - xit 'creates and stores a new peep' do + it 'creates and stores a new peep' do new_peep.peep = 'Test peep 1' new_peep.date = '2023-04-10 12:34:56' - new_peep.username_id = 1 # maybe put in quotes? + new_peep.username_id = '1' repo.create(new_peep) peeps = repo.all expect(peeps.last.peep).to eq 'Test peep 1' expect(peeps.last.date).to eq '2023-04-10 12:34:56' - expect(peeps.last.username_id).to eq 1 + expect(peeps.last.username_id).to eq '1' end end end \ No newline at end of file diff --git a/spec/user_repository_test.rb b/spec/user_repository_test.rb index 2576d9094d..67739fe4ee 100644 --- a/spec/user_repository_test.rb +++ b/spec/user_repository_test.rb @@ -19,7 +19,7 @@ def reset_users_table describe '#create' do it 'creates and stores a new peep' do - + end end end \ No newline at end of file From 1556933dc6149d2ee107291225c5b6fa70d9d9d3 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Mon, 10 Apr 2023 09:41:05 +0100 Subject: [PATCH 07/28] all method created and tested on for UserRepository clas --- lib/user.rb | 2 +- lib/user_repository.rb | 21 ++++++++++++++++++++- spec/integration/app_spec.rb | 3 ++- spec/user_repository_test.rb | 15 ++++++++------- 4 files changed, 31 insertions(+), 10 deletions(-) diff --git a/lib/user.rb b/lib/user.rb index 728361db00..a87ac4d5ea 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,3 +1,3 @@ class User - attr_accessor :id, :email, :password_hash + attr_accessor :id, :username, :email, :password_hash end \ No newline at end of file diff --git a/lib/user_repository.rb b/lib/user_repository.rb index e0465a6edf..bbaebc7db9 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -1 +1,20 @@ -require 'user.rb' \ No newline at end of file +require_relative 'database_connection' +require_relative 'user' + +class UserRepository + def all + sql = 'SELECT * FROM users;' + result_set = DatabaseConnection.exec_params(sql, []) + + users = [] + result_set.each do |row| + user = User.new + user.id = row['id'] + user.username = row['username'] + user.email = row['email'] + user.password_hash = row['password_hash'] + users << user + end + return users + end +end \ No newline at end of file diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 85f56c8caa..14bf22890c 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -16,4 +16,5 @@ expect(response.status).to eq 200 expect(response.body).to include('') end - end \ No newline at end of file + end +end \ No newline at end of file diff --git a/spec/user_repository_test.rb b/spec/user_repository_test.rb index 67739fe4ee..5a2d7ec961 100644 --- a/spec/user_repository_test.rb +++ b/spec/user_repository_test.rb @@ -12,14 +12,15 @@ def reset_users_table reset_users_table end - let(:repo) {UserRepository.new} - # write your let(:peep_repositoty) {PeepRepository.new} here - # as well as for user_repository - # see which one is needed as you write each test + let(:repo) { UserRepository.new } - describe '#create' do - it 'creates and stores a new peep' do - + describe '#all' do + it 'lists all users' do + users = repo.all + expect(users[0].username).to eq 'user1' + expect(users[0].email).to eq 'user1@example.com' + expect(users[1].username).to eq 'user2' + expect(users[1].email).to eq 'user2@example.com' end end end \ No newline at end of file From 7c6a68348a1f451fef65241ec5b7388d53494d5d Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Mon, 10 Apr 2023 14:48:14 +0100 Subject: [PATCH 08/28] create method tested --- Gemfile | 2 ++ Gemfile.lock | 2 ++ app.rb | 64 +++++++++++++++++++++++++++++++----- lib/database_connection.rb | 10 ++++++ lib/user.rb | 4 +-- lib/user_repository.rb | 40 ++++++++++++++++++++-- spec/seeds.sql | 6 ++-- spec/user_repository_test.rb | 20 +++++++++-- 8 files changed, 131 insertions(+), 17 deletions(-) diff --git a/Gemfile b/Gemfile index 494eca7c08..9a0ec893e7 100644 --- a/Gemfile +++ b/Gemfile @@ -18,3 +18,5 @@ gem "sinatra", "~> 3.0" gem "sinatra-contrib", "~> 3.0" gem "webrick", "~> 1.8" gem "rack-test", "~> 2.1" + +gem 'bcrypt', '~> 3.1.18' diff --git a/Gemfile.lock b/Gemfile.lock index 0b085d0b88..49a7a97b57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,6 +3,7 @@ GEM specs: ansi (1.5.0) ast (2.4.2) + bcrypt (3.1.18) diff-lcs (1.4.4) docile (1.4.0) multi_json (1.15.0) @@ -77,6 +78,7 @@ PLATFORMS ruby DEPENDENCIES + bcrypt (~> 3.1.18) pg (~> 1.4) rack-test (~> 2.1) rspec diff --git a/app.rb b/app.rb index c849e822f6..f2e68fc7ec 100644 --- a/app.rb +++ b/app.rb @@ -4,13 +4,59 @@ require 'sinatra/base' require 'sinatra/reloader' -class Application < Sinatra::Base - # This allows the app code to refresh - # without having to restart the server. - configure :development do - register Sinatra::Reloader - end -end - -DatabaseConnection.connect('chitter') +https://github.com/makersacademy/web-applications/blob/main/pills/user_authentication.md + +# class Application < Sinatra::Base +# # Sessions are disabled by default, so this line is needed. +# enable :sessions + +# configure :development do +# register Sinatra::Reloader +# end + +# # This route simply returns the login page +# get '/login' do +# return erb(:login) +# end + +# # This route receives login information (email and password) +# # as body parameters, and find the user in the database +# # using the email. If the password matches, it returns +# # a success page. +# post '/login' do +# email = params[:email] +# password = params[:password] + +# user = UserRepository.find_by_email(email) + +# # This is a simplified way of +# # checking the password. In a real +# # project, you should encrypt the password +# # stored in the database. +# if user.password == password +# # Set the user ID in session +# session[:user_id] = user.id + +# return erb(:login_success) +# else +# return erb(:login_error) +# end +# end + +# # This route is an example +# # of a "authenticated-only" route. +# # It can be accessed only if a user is +# # signed-in (if we have user information in session). +# get '/account_page' do +# if session[:user_id] == nil +# # No user id in the session +# # so the user is not logged in. +# return redirect('/login') +# else +# # The user is logged in, display +# # their account page. +# return erb(:account) +# end +# end +# end diff --git a/lib/database_connection.rb b/lib/database_connection.rb index ac9ec32a31..c8983ed6db 100644 --- a/lib/database_connection.rb +++ b/lib/database_connection.rb @@ -23,4 +23,14 @@ def self.exec_params(query, params) end @connection.exec_params(query, params) end + + def self.setup(dbname) + @connection = PG.connect(dbname: dbname) + @connection.exec("CREATE TABLE IF NOT EXISTS users(id SERIAL PRIMARY KEY, username VARCHAR(60), email VARCHAR(60), password VARCHAR(255));") + end + + def self.clear(dbname) + @connection = PG.connect(dbname: dbname) + @connection.exec("TRUNCATE TABLE users;") + end end \ No newline at end of file diff --git a/lib/user.rb b/lib/user.rb index a87ac4d5ea..cbd574bacf 100644 --- a/lib/user.rb +++ b/lib/user.rb @@ -1,3 +1,3 @@ class User - attr_accessor :id, :username, :email, :password_hash -end \ No newline at end of file + attr_accessor :id, :username, :email, :password +end diff --git a/lib/user_repository.rb b/lib/user_repository.rb index bbaebc7db9..0819adb492 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -1,5 +1,6 @@ require_relative 'database_connection' require_relative 'user' +require 'bcrypt' class UserRepository def all @@ -12,9 +13,44 @@ def all user.id = row['id'] user.username = row['username'] user.email = row['email'] - user.password_hash = row['password_hash'] + user.password = row['password'] users << user end return users end -end \ No newline at end of file + + def create(user) + encrypted_password = BCrypt::Password.create(user.password) + + sql = ' + INSERT INTO users (username, email, password) + VALUES($1, $2, $3); + ' + sql_params = [ + user.username, + user.email, + encrypted_password + ] + DatabaseConnection.exec_params(sql, sql_params) + + return nil + end + + # def sign_in(email, submitted_password) + # user = find_by_email(email) + + # return nil if user.nil? + + # # Compare the submitted password with the encrypted one saved in the database + # stored_password = BCrypt::Password.new(user.password) + # if stored_password == submitted_password + # # login success + # else + # # wrong password + # end + # end + + # def find_by_email(email) + # # ... + # end +end diff --git a/spec/seeds.sql b/spec/seeds.sql index f70d54004c..77c202cec7 100644 --- a/spec/seeds.sql +++ b/spec/seeds.sql @@ -1,6 +1,8 @@ -TRUNCATE TABLE peeps RESTART IDENTITY; +TRUNCATE TABLE peeps RESTART IDENTITY CASCADE; +TRUNCATE TABLE users RESTART IDENTITY CASCADE; -INSERT INTO users (username, email, password_hash) VALUES + +INSERT INTO users (username, email, password) VALUES ('user1', 'user1@example.com', ''), ('user2', 'user2@example.com', ''); diff --git a/spec/user_repository_test.rb b/spec/user_repository_test.rb index 5a2d7ec961..95f34f3da4 100644 --- a/spec/user_repository_test.rb +++ b/spec/user_repository_test.rb @@ -1,5 +1,5 @@ -require 'user.rb' -require 'user_repository.rb' +require_relative '../lib/user.rb' +require_relative '../lib/user_repository.rb' def reset_users_table seed_sql = File.read('spec/seeds.sql') @@ -13,6 +13,7 @@ def reset_users_table end let(:repo) { UserRepository.new } + let(:user) { User.new} describe '#all' do it 'lists all users' do @@ -23,4 +24,19 @@ def reset_users_table expect(users[1].email).to eq 'user2@example.com' end end + + describe '#create' do + it 'adds a new user to the table' do + user.username = 'user3' + user.email = 'user3@example.com' + user.password = '12345678' + + new_user = repo.create(user) + all_users = repo.all + + expect(all_users.last.username).to eq 'user3' + expect(all_users.last.email).to eq 'user3@example.com' + expect(BCrypt::Password.new(all_users.last.password)).to eq '12345678' + end + end end \ No newline at end of file From 8a93d7651047d148099eb62b803442bf05d322b4 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Mon, 10 Apr 2023 14:53:36 +0100 Subject: [PATCH 09/28] reformatted --- lib/peep_repository.rb | 1 + lib/user_repository.rb | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/peep_repository.rb b/lib/peep_repository.rb index 9159559941..e884281dee 100644 --- a/lib/peep_repository.rb +++ b/lib/peep_repository.rb @@ -17,6 +17,7 @@ def all new_peep.username_id = row['username_id'] peeps << new_peep end + return peeps end diff --git a/lib/user_repository.rb b/lib/user_repository.rb index 0819adb492..daa8ee18c5 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -16,6 +16,7 @@ def all user.password = row['password'] users << user end + return users end From 007e292fa6f694d90c107eb11d085424ebfc813f Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Tue, 11 Apr 2023 10:59:53 +0100 Subject: [PATCH 10/28] test written for find_by_email method --- lib/user_repository.rb | 3 +++ spec/spec_helper.rb | 2 +- spec/user_repository_test.rb | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/lib/user_repository.rb b/lib/user_repository.rb index daa8ee18c5..1245214d4d 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -37,6 +37,9 @@ def create(user) return nil end + def find_by_email(email) + + end # def sign_in(email, submitted_password) # user = find_by_email(email) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 7e0c982965..0cbe469655 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,7 +9,7 @@ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::Console, # Want a nice code coverage website? Uncomment this next line! - # SimpleCov::Formatter::HTMLFormatter + SimpleCov::Formatter::HTMLFormatter ]) SimpleCov.start diff --git a/spec/user_repository_test.rb b/spec/user_repository_test.rb index 95f34f3da4..2ad010d947 100644 --- a/spec/user_repository_test.rb +++ b/spec/user_repository_test.rb @@ -39,4 +39,18 @@ def reset_users_table expect(BCrypt::Password.new(all_users.last.password)).to eq '12345678' end end + describe '#find_by_email' do + context 'when user exists' do + it 'returns the user associated with that email' do + found_user = repo.find('user1@example.com') + expect(found_user.username).to eq 'user1' + end + end + context 'when user does not exist' do + it 'returns nil' do + found_user = repo.find_by_email('nonexistent@example.com') + expect(found_user).to eq nil + end + end + end end \ No newline at end of file From 729e5f1a351ccf6cd89fb4a4cdbec60522fcc2c1 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Tue, 11 Apr 2023 11:49:37 +0100 Subject: [PATCH 11/28] succesfully finds a valid email --- lib/user_repository.rb | 14 ++++++++++++++ spec/user_repository_test.rb | 4 ++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/user_repository.rb b/lib/user_repository.rb index 1245214d4d..ddb85e6e58 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -38,7 +38,19 @@ def create(user) end def find_by_email(email) + sql = 'SELECT username FROM users WHERE email = $1;' + params = [email] + result = DatabaseConnection.exec_params(sql, params) + record = result[0] + p result + p result[0] + user = User.new + user.username = record['username'] + user.email = record['email'] + user.password = record['password'] + + return user end # def sign_in(email, submitted_password) # user = find_by_email(email) @@ -57,4 +69,6 @@ def find_by_email(email) # def find_by_email(email) # # ... # end + + # do a sign out if you have time end diff --git a/spec/user_repository_test.rb b/spec/user_repository_test.rb index 2ad010d947..e481309fb5 100644 --- a/spec/user_repository_test.rb +++ b/spec/user_repository_test.rb @@ -42,12 +42,12 @@ def reset_users_table describe '#find_by_email' do context 'when user exists' do it 'returns the user associated with that email' do - found_user = repo.find('user1@example.com') + found_user = repo.find_by_email('user1@example.com') expect(found_user.username).to eq 'user1' end end context 'when user does not exist' do - it 'returns nil' do + xit 'returns nil' do found_user = repo.find_by_email('nonexistent@example.com') expect(found_user).to eq nil end From 16d4b850a820bd393ad1ac1572be43cc10295ebb Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Tue, 11 Apr 2023 12:05:42 +0100 Subject: [PATCH 12/28] raise error when given invalid email to find --- lib/user_repository.rb | 20 +++++++++++--------- spec/user_repository_test.rb | 4 ++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/user_repository.rb b/lib/user_repository.rb index ddb85e6e58..51f67f9a6f 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -42,16 +42,18 @@ def find_by_email(email) params = [email] result = DatabaseConnection.exec_params(sql, params) - record = result[0] - p result - p result[0] - user = User.new - user.username = record['username'] - user.email = record['email'] - user.password = record['password'] - - return user + if result.ntuples > 0 + record = result[0] + user = User.new + user.username = record['username'] + user.email = record['email'] + user.password = record['password'] + return user + else + fail 'No such user with given email.' + end end + # def sign_in(email, submitted_password) # user = find_by_email(email) diff --git a/spec/user_repository_test.rb b/spec/user_repository_test.rb index e481309fb5..4f4bd24205 100644 --- a/spec/user_repository_test.rb +++ b/spec/user_repository_test.rb @@ -47,9 +47,9 @@ def reset_users_table end end context 'when user does not exist' do - xit 'returns nil' do + it 'fails' do found_user = repo.find_by_email('nonexistent@example.com') - expect(found_user).to eq nil + expect {found_user.username}.to raise_error 'No such user with given email.' end end end From 1822058755feecad2d4d7315aa824b0eeddb3a5f Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Tue, 11 Apr 2023 13:46:12 +0100 Subject: [PATCH 13/28] homepage added --- app.rb | 19 ++++++++++++------- lib/user_repository.rb | 28 ++++++++++++---------------- spec/integration/app_spec.rb | 4 ++-- views/index.erb | 21 +++++++++++++++++++++ 4 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 views/index.erb diff --git a/app.rb b/app.rb index f2e68fc7ec..3eac27bd8a 100644 --- a/app.rb +++ b/app.rb @@ -4,16 +4,21 @@ require 'sinatra/base' require 'sinatra/reloader' -https://github.com/makersacademy/web-applications/blob/main/pills/user_authentication.md +DatabaseConnection.connect('chitter') -# class Application < Sinatra::Base -# # Sessions are disabled by default, so this line is needed. -# enable :sessions +class Application < Sinatra::Base + # Sessions are disabled by default, so this line is needed. + enable :sessions -# configure :development do -# register Sinatra::Reloader -# end + configure :development do + register Sinatra::Reloader + end + get '/' do + @peeps = PeepRepository.new.all + return erb(:index) + end +end # # This route simply returns the login page # get '/login' do # return erb(:login) diff --git a/lib/user_repository.rb b/lib/user_repository.rb index 51f67f9a6f..0cbbb5de3f 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -54,23 +54,19 @@ def find_by_email(email) end end - # def sign_in(email, submitted_password) - # user = find_by_email(email) + def sign_in(email, submitted_password) + user = find_by_email(email) - # return nil if user.nil? + return nil if user.nil? - # # Compare the submitted password with the encrypted one saved in the database - # stored_password = BCrypt::Password.new(user.password) - # if stored_password == submitted_password - # # login success - # else - # # wrong password - # end - # end - - # def find_by_email(email) - # # ... - # end + # Compare the submitted password with the encrypted one saved in the database + stored_password = BCrypt::Password.new(user.password) + if stored_password == submitted_password + return 'login success' + else + return 'wrong password' + end + end - # do a sign out if you have time + # do a sign out too end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 14bf22890c..8d8f0e3305 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -10,8 +10,8 @@ # class so our tests work. let(:app) { Application.new } - context 'GET to /' do - it 'displays all peeps in reverse chronological order' do + describe 'GET to /' do + it 'responds with status 200 and renders the homepage template' do response = get('/') expect(response.status).to eq 200 expect(response.body).to include('') diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000000..502ad2ce6b --- /dev/null +++ b/views/index.erb @@ -0,0 +1,21 @@ +

Chitter

+ +
+ + + +
+ +

Recent Peeps

+ +<% @peeps.each do |peep| %> +
+ <%= peep.timestamp.strftime("%H:%M:%S on %B %d, %Y") %> +

<%= peep.text %>

+
+<% end %> + + + + + From 2e1827885eea13b8a0dc10086ebd8aa97c76f768 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Tue, 11 Apr 2023 14:34:00 +0100 Subject: [PATCH 14/28] homepage renders page correctly --- spec/integration/app_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 8d8f0e3305..a68ec7efad 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -14,7 +14,7 @@ it 'responds with status 200 and renders the homepage template' do response = get('/') expect(response.status).to eq 200 - expect(response.body).to include('') + expect(response.body).to include('') end end end \ No newline at end of file From 50c518f4f7147617910ea60c4e9908a1ca8f99c8 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Tue, 11 Apr 2023 15:04:44 +0100 Subject: [PATCH 15/28] signup form created --- Gemfile | 2 +- Gemfile.lock | 22 ++++++++++++++++ app.rb | 50 ++---------------------------------- spec/integration/app_spec.rb | 10 +++----- spec/spec_helper.rb | 5 ++++ views/signup.erb | 13 ++++++++++ 6 files changed, 46 insertions(+), 56 deletions(-) create mode 100644 views/signup.erb diff --git a/Gemfile b/Gemfile index 9a0ec893e7..bc919d582d 100644 --- a/Gemfile +++ b/Gemfile @@ -19,4 +19,4 @@ gem "sinatra-contrib", "~> 3.0" gem "webrick", "~> 1.8" gem "rack-test", "~> 2.1" -gem 'bcrypt', '~> 3.1.18' +gem 'bcrypt', '~> 3.1.18' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 49a7a97b57..043ba10309 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,18 +1,37 @@ GEM remote: https://rubygems.org/ specs: + addressable (2.8.4) + public_suffix (>= 2.0.2, < 6.0) ansi (1.5.0) ast (2.4.2) bcrypt (3.1.18) + capybara (3.39.0) + addressable + matrix + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (>= 1.5, < 3.0) + xpath (~> 3.2) diff-lcs (1.4.4) docile (1.4.0) + matrix (0.4.2) + mini_mime (1.1.2) + mini_portile2 (2.8.1) multi_json (1.15.0) mustermann (3.0.0) ruby2_keywords (~> 0.0.1) + nokogiri (1.14.2) + mini_portile2 (~> 2.8.0) + racc (~> 1.4) parallel (1.20.1) parser (3.0.2.0) ast (~> 2.4.1) pg (1.4.6) + public_suffix (5.0.1) + racc (1.6.2) rack (2.2.6.4) rack-protection (3.0.5) rack @@ -73,12 +92,15 @@ GEM tilt (2.1.0) unicode-display_width (2.0.0) webrick (1.8.1) + xpath (3.2.0) + nokogiri (~> 1.8) PLATFORMS ruby DEPENDENCIES bcrypt (~> 3.1.18) + capybara pg (~> 1.4) rack-test (~> 2.1) rspec diff --git a/app.rb b/app.rb index 3eac27bd8a..8a45f97e3c 100644 --- a/app.rb +++ b/app.rb @@ -14,54 +14,8 @@ class Application < Sinatra::Base register Sinatra::Reloader end - get '/' do - @peeps = PeepRepository.new.all - return erb(:index) + get '/signup' do + return erb(:signup) end end -# # This route simply returns the login page -# get '/login' do -# return erb(:login) -# end - -# # This route receives login information (email and password) -# # as body parameters, and find the user in the database -# # using the email. If the password matches, it returns -# # a success page. -# post '/login' do -# email = params[:email] -# password = params[:password] - -# user = UserRepository.find_by_email(email) - -# # This is a simplified way of -# # checking the password. In a real -# # project, you should encrypt the password -# # stored in the database. -# if user.password == password -# # Set the user ID in session -# session[:user_id] = user.id - -# return erb(:login_success) -# else -# return erb(:login_error) -# end -# end - -# # This route is an example -# # of a "authenticated-only" route. -# # It can be accessed only if a user is -# # signed-in (if we have user information in session). -# get '/account_page' do -# if session[:user_id] == nil -# # No user id in the session -# # so the user is not logged in. -# return redirect('/login') -# else -# # The user is logged in, display -# # their account page. -# return erb(:account) -# end -# end -# end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index a68ec7efad..0eeb71c556 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -10,11 +10,7 @@ # class so our tests work. let(:app) { Application.new } - describe 'GET to /' do - it 'responds with status 200 and renders the homepage template' do - response = get('/') - expect(response.status).to eq 200 - expect(response.body).to include('') - end - end + + + end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 0cbe469655..4a9dba69e2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,8 @@ require 'simplecov' require 'simplecov-console' require 'database_connection' +require 'rack/test' +require 'capybara/rspec' # Make sure this connects to your test database # (its name should end with '_test') @@ -20,3 +22,6 @@ puts "\e[33mTry it now! Just run: rubocop\e[0m" end end + +Capybara.app = Application.new +Capybara.default_driver = :rack_test \ No newline at end of file diff --git a/views/signup.erb b/views/signup.erb new file mode 100644 index 0000000000..93b19e2029 --- /dev/null +++ b/views/signup.erb @@ -0,0 +1,13 @@ +

Sign Up

+ +
+ + + + + + + + + +
\ No newline at end of file From ce2856ae1e55cbb261d67ebb1c124c2206b5b7d7 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Wed, 12 Apr 2023 08:12:15 +0100 Subject: [PATCH 16/28] get request for sign up page tested --- spec/integration/app_spec.rb | 8 +++++++- spec/spec_helper.rb | 6 +----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 0eeb71c556..70b152aa6c 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -10,7 +10,13 @@ # class so our tests work. let(:app) { Application.new } - + context 'GET /signup' do + it 'returns the signup page' do + response = get('/signup') + expect(response.status).to eq 200 + expect(response.body).to include '' + end + end end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4a9dba69e2..2e4beb6fb2 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -2,7 +2,6 @@ require 'simplecov-console' require 'database_connection' require 'rack/test' -require 'capybara/rspec' # Make sure this connects to your test database # (its name should end with '_test') @@ -21,7 +20,4 @@ puts "\e[33mHave you considered running rubocop? It will help you improve your code!\e[0m" puts "\e[33mTry it now! Just run: rubocop\e[0m" end -end - -Capybara.app = Application.new -Capybara.default_driver = :rack_test \ No newline at end of file +end \ No newline at end of file From bed960dae2c8cae32ad2c3baebb805f984602c72 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Wed, 12 Apr 2023 08:24:08 +0100 Subject: [PATCH 17/28] first test failing for creating a new user --- spec/integration/app_spec.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 70b152aa6c..4f1a392628 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -10,13 +10,27 @@ # class so our tests work. let(:app) { Application.new } - context 'GET /signup' do + describe 'GET /signup' do it 'returns the signup page' do response = get('/signup') expect(response.status).to eq 200 expect(response.body).to include '' end end + describe 'POST /signup' do + context 'when given valid user data' do + it 'creates a new user' do + response = post( + 'post/signup', + username: 'user1', + email: 'user1@gmail.com', + password: '12345678' + ) + expect(response.status).to eq(200) + expect(response.body).to change { UserRepository.new.count }.by(1) + end + end + end end \ No newline at end of file From 68226bc6e86d0d0d96bcb96fdb36f99199897e88 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Wed, 12 Apr 2023 08:58:24 +0100 Subject: [PATCH 18/28] succesfully tested post /signup --- app.rb | 10 ++++++++++ lib/user_repository.rb | 24 ++++++++++++------------ spec/integration/app_spec.rb | 8 +++----- 3 files changed, 25 insertions(+), 17 deletions(-) diff --git a/app.rb b/app.rb index 8a45f97e3c..7f9dd78556 100644 --- a/app.rb +++ b/app.rb @@ -17,5 +17,15 @@ class Application < Sinatra::Base get '/signup' do return erb(:signup) end + + post '/signup' do + new_user = User.new + new_user.username = params['username'] + new_user.email = params['email'] + new_user.password = params['password'] + UserRepository.new.create(new_user) + redirect '/peeps' + end + end diff --git a/lib/user_repository.rb b/lib/user_repository.rb index 0cbbb5de3f..a1fe376b5a 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -54,19 +54,19 @@ def find_by_email(email) end end - def sign_in(email, submitted_password) - user = find_by_email(email) + # def sign_in(email, submitted_password) + # user = find_by_email(email) - return nil if user.nil? + # return nil if user.nil? - # Compare the submitted password with the encrypted one saved in the database - stored_password = BCrypt::Password.new(user.password) - if stored_password == submitted_password - return 'login success' - else - return 'wrong password' - end - end + # # Compare the submitted password with the encrypted one saved in the database + # stored_password = BCrypt::Password.new(user.password) + # if stored_password == submitted_password + # return 'login success' + # else + # return 'wrong password' + # end + # end - # do a sign out too + # # do a sign out too end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 4f1a392628..d4510b75ba 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -18,17 +18,15 @@ end end describe 'POST /signup' do - context 'when given valid user data' do it 'creates a new user' do response = post( - 'post/signup', + '/signup', username: 'user1', email: 'user1@gmail.com', password: '12345678' ) - expect(response.status).to eq(200) - expect(response.body).to change { UserRepository.new.count }.by(1) - end + expect(response.status).to eq(302) # 302 automatically redirects to another URL + expect(UserRepository.new.find_by_email('user1@gmail.com').username).to eq 'user1' end end From 78150e869911a0c454f47f72c59bf8e58ae0e384 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Wed, 12 Apr 2023 09:23:56 +0100 Subject: [PATCH 19/28] displays most recent peeps and redirects to post page --- app.rb | 8 +++++++- spec/integration/app_spec.rb | 7 +++++++ views/index.erb | 21 --------------------- views/peeps.erb | 12 ++++++++++++ views/post_peep.erb | 14 ++++++++++++++ 5 files changed, 40 insertions(+), 22 deletions(-) delete mode 100644 views/index.erb create mode 100644 views/peeps.erb create mode 100644 views/post_peep.erb diff --git a/app.rb b/app.rb index 7f9dd78556..e632969678 100644 --- a/app.rb +++ b/app.rb @@ -24,7 +24,13 @@ class Application < Sinatra::Base new_user.email = params['email'] new_user.password = params['password'] UserRepository.new.create(new_user) - redirect '/peeps' + redirect '/' + end + + get '/' do + repo = PeepRepository.new + @peeps = repo.all + return erb(:peeps) end end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index d4510b75ba..2d40d59d5e 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -29,6 +29,13 @@ expect(UserRepository.new.find_by_email('user1@gmail.com').username).to eq 'user1' end end + describe 'GET /peeps' do + it 'returns all peeps from most recent date' do + response = get('/') + expect(response.status).to eq 200 + expect(response.body).to include '

Recent Peeps

' + end + end end \ No newline at end of file diff --git a/views/index.erb b/views/index.erb deleted file mode 100644 index 502ad2ce6b..0000000000 --- a/views/index.erb +++ /dev/null @@ -1,21 +0,0 @@ -

Chitter

- -
- - - -
- -

Recent Peeps

- -<% @peeps.each do |peep| %> -
- <%= peep.timestamp.strftime("%H:%M:%S on %B %d, %Y") %> -

<%= peep.text %>

-
-<% end %> - - - - - diff --git a/views/peeps.erb b/views/peeps.erb new file mode 100644 index 0000000000..e0ee9b0daf --- /dev/null +++ b/views/peeps.erb @@ -0,0 +1,12 @@ +

Recent Peeps

+ +<% @peeps.each do |peep| %> +
+ <%= peep.timestamp.strftime("%H:%M:%S on %B %d, %Y") %> +

<%= peep.text %>

+
+<% end %> + + \ No newline at end of file diff --git a/views/post_peep.erb b/views/post_peep.erb new file mode 100644 index 0000000000..ed4b45accb --- /dev/null +++ b/views/post_peep.erb @@ -0,0 +1,14 @@ +

Chitter

+ +
+ + + +
+ + + + + From b2e2413abfa8a835572595562ebddc525805edd1 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Wed, 12 Apr 2023 09:38:29 +0100 Subject: [PATCH 20/28] created and tested new page to input peeps --- app.rb | 3 +++ spec/integration/app_spec.rb | 10 ++++++++-- views/peeps.erb | 4 +++- views/post_peep.erb | 2 -- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/app.rb b/app.rb index e632969678..bf5bcce5bc 100644 --- a/app.rb +++ b/app.rb @@ -33,5 +33,8 @@ class Application < Sinatra::Base return erb(:peeps) end + get '/post_peep' do + return erb(:post_peep) + end end diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 2d40d59d5e..1cfdfde402 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -33,9 +33,15 @@ it 'returns all peeps from most recent date' do response = get('/') expect(response.status).to eq 200 - expect(response.body).to include '

Recent Peeps

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

Most Recent Peeps

' + end + end + describe 'GET /post_peep' do + it 'returns the post peep page' do + response = get('/post_peep') + expect(response.status).to eq 200 + expect(response.body).to include '' end end - end \ No newline at end of file diff --git a/views/peeps.erb b/views/peeps.erb index e0ee9b0daf..27c6527449 100644 --- a/views/peeps.erb +++ b/views/peeps.erb @@ -1,4 +1,6 @@ -

Recent Peeps

+

Welcome to Chitter

+ +

Most Recent Peeps

<% @peeps.each do |peep| %>
diff --git a/views/post_peep.erb b/views/post_peep.erb index ed4b45accb..d2d2e24196 100644 --- a/views/post_peep.erb +++ b/views/post_peep.erb @@ -1,5 +1,3 @@ -

Chitter

-
From 01165b779ac7206fa14b22eb20fce9ad3655f68c Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Wed, 12 Apr 2023 12:40:02 +0100 Subject: [PATCH 21/28] trying to fix 500 post request error --- app.rb | 9 +++++++++ lib/peep.rb | 2 +- lib/peep_repository.rb | 9 ++++----- spec/integration/app_spec.rb | 8 +++++++- spec/peep_repository_test.rb | 20 ++++++++++---------- spec/seeds.sql | 2 +- 6 files changed, 32 insertions(+), 18 deletions(-) diff --git a/app.rb b/app.rb index bf5bcce5bc..942a6fb1ed 100644 --- a/app.rb +++ b/app.rb @@ -36,5 +36,14 @@ class Application < Sinatra::Base get '/post_peep' do return erb(:post_peep) end + + post '/' do + new_peep = Peep.new + new_peep.peep = params['peep'] + new_peep.timestamp = Time.now + new_peep.username_id = session[:user_id] + PeepRepository.new.create(new_peep) + redirect '/' + end end diff --git a/lib/peep.rb b/lib/peep.rb index 48d233eb50..f8b862947f 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,3 +1,3 @@ class Peep - attr_accessor :id, :peep, :date, :username_id + attr_accessor :id, :peep, :timestamp, :username_id end \ No newline at end of file diff --git a/lib/peep_repository.rb b/lib/peep_repository.rb index e884281dee..9a97bf5d3e 100644 --- a/lib/peep_repository.rb +++ b/lib/peep_repository.rb @@ -5,7 +5,7 @@ class PeepRepository def all # returns all rows in reverse chronological order - sql = 'SELECT peep, date, username_id FROM peeps;' + sql = 'SELECT peep, timestamp, username_id FROM peeps;' result_set = DatabaseConnection.exec_params(sql, []) peeps = [] @@ -13,7 +13,7 @@ def all new_peep = Peep.new new_peep.id = row['id'] new_peep.peep = row['peep'] - new_peep.date = row['date'] + new_peep.timestamp = row['timestamp'] new_peep.username_id = row['username_id'] peeps << new_peep end @@ -22,9 +22,8 @@ def all end def create(message) - sql = 'INSERT INTO peeps (peep, date, username_id) VALUES ($1, $2, $3);' - result_set = DatabaseConnection.exec_params(sql, [message.peep, message.date, message.username_id]) - + sql = 'INSERT INTO peeps (peep, timestamp, username_id) VALUES ($1, $2, $3);' + result_set = DatabaseConnection.exec_params(sql, [message.peep, message.timestamp, message.username_id]) return message end end \ No newline at end of file diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index 1cfdfde402..caa2b3831e 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -43,5 +43,11 @@ expect(response.body).to include '' end end - + describe 'POST /' do + it 'creates a new peep' do + post '/', { peep: 'This is a new peep' }, { 'rack.session' => { user_id: 1 } } + expect(last_response.status).to eq(302) + expect(PeepRepository.new.all.first.text).to eq('This is a new peep') + end + end end \ No newline at end of file diff --git a/spec/peep_repository_test.rb b/spec/peep_repository_test.rb index f2034af385..7a0f47e692 100644 --- a/spec/peep_repository_test.rb +++ b/spec/peep_repository_test.rb @@ -24,16 +24,16 @@ def reset_peeps_table end describe '#create' do - it 'creates and stores a new peep' do - new_peep.peep = 'Test peep 1' - new_peep.date = '2023-04-10 12:34:56' - new_peep.username_id = '1' - repo.create(new_peep) + it 'creates and stores a new peep' do + new_peep.peep = 'Test peep 1' + new_peep.timestamp = '2023-04-10 12:34:56' + new_peep.username_id = '1' + repo.create(new_peep) - peeps = repo.all - expect(peeps.last.peep).to eq 'Test peep 1' - expect(peeps.last.date).to eq '2023-04-10 12:34:56' - expect(peeps.last.username_id).to eq '1' - end + peeps = repo.all + expect(peeps.last.peep).to eq 'Test peep 1' + expect(peeps.last.timestamp).to eq '2023-04-10 12:34:56' + expect(peeps.last.username_id).to eq '1' end +end end \ No newline at end of file diff --git a/spec/seeds.sql b/spec/seeds.sql index 77c202cec7..67941c5fc1 100644 --- a/spec/seeds.sql +++ b/spec/seeds.sql @@ -6,7 +6,7 @@ INSERT INTO users (username, email, password) VALUES ('user1', 'user1@example.com', ''), ('user2', 'user2@example.com', ''); -INSERT INTO peeps (peep, date, username_id) VALUES +INSERT INTO peeps (peep, timestamp, username_id) VALUES ('Test peep 1', '2023-04-10 12:34:56', 1), ('Test peep 2', '2023-04-11 15:30:00', 2), ('Test peep 3', '2023-04-12 20:30:00', 2); From 05069202aea753b38a0200a66c97df1d538444a4 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Thu, 13 Apr 2023 11:38:04 +0100 Subject: [PATCH 22/28] http requests completed. App is now completed --- app.rb | 19 ++++++++++--------- lib/peep.rb | 2 +- lib/peep_repository.rb | 4 ++-- lib/user_repository.rb | 15 +++++++++++---- spec/seeds.sql | 3 +-- views/homepage.erb | 15 +++++++++++++++ views/{post_peep.erb => peep.erb} | 4 ++-- views/peeps.erb | 14 -------------- 8 files changed, 42 insertions(+), 34 deletions(-) create mode 100644 views/homepage.erb rename views/{post_peep.erb => peep.erb} (64%) delete mode 100644 views/peeps.erb diff --git a/app.rb b/app.rb index 942a6fb1ed..760235ba86 100644 --- a/app.rb +++ b/app.rb @@ -23,27 +23,28 @@ class Application < Sinatra::Base new_user.username = params['username'] new_user.email = params['email'] new_user.password = params['password'] - UserRepository.new.create(new_user) - redirect '/' + created_user = UserRepository.new.create(new_user) + session[:user_id] = created_user.id + redirect '/homepage' end - get '/' do + get '/homepage' do repo = PeepRepository.new @peeps = repo.all - return erb(:peeps) + return erb(:homepage) end - get '/post_peep' do - return erb(:post_peep) + get '/peep' do + return erb(:peep) end - post '/' do + post '/peep' do new_peep = Peep.new - new_peep.peep = params['peep'] + new_peep.peep = params[:peep] new_peep.timestamp = Time.now new_peep.username_id = session[:user_id] PeepRepository.new.create(new_peep) - redirect '/' + redirect '/homepage' end end diff --git a/lib/peep.rb b/lib/peep.rb index f8b862947f..cb2f2de3b4 100644 --- a/lib/peep.rb +++ b/lib/peep.rb @@ -1,3 +1,3 @@ class Peep - attr_accessor :id, :peep, :timestamp, :username_id + attr_accessor :id, :peep, :date, :username_id, :timestamp end \ No newline at end of file diff --git a/lib/peep_repository.rb b/lib/peep_repository.rb index 9a97bf5d3e..e18ea68d76 100644 --- a/lib/peep_repository.rb +++ b/lib/peep_repository.rb @@ -22,8 +22,8 @@ def all end def create(message) - sql = 'INSERT INTO peeps (peep, timestamp, username_id) VALUES ($1, $2, $3);' - result_set = DatabaseConnection.exec_params(sql, [message.peep, message.timestamp, message.username_id]) + sql = 'INSERT INTO peeps (peep, username_id, timestamp) VALUES ($1, $2, $3);' + result_set = DatabaseConnection.exec_params(sql, [message.peep, message.username_id, message.timestamp]) return message end end \ No newline at end of file diff --git a/lib/user_repository.rb b/lib/user_repository.rb index a1fe376b5a..7d43baf67d 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -25,17 +25,21 @@ def create(user) sql = ' INSERT INTO users (username, email, password) - VALUES($1, $2, $3); + VALUES($1, $2, $3) + RETURNING id; ' sql_params = [ user.username, user.email, encrypted_password ] - DatabaseConnection.exec_params(sql, sql_params) + result_set = DatabaseConnection.exec_params(sql, sql_params) + + user.id = result_set[0]['id'].to_i - return nil + return user end + def find_by_email(email) sql = 'SELECT username FROM users WHERE email = $1;' @@ -54,6 +58,9 @@ def find_by_email(email) end end + ###################################################################################### + # implement this feature if you have time: + # def sign_in(email, submitted_password) # user = find_by_email(email) @@ -67,6 +74,6 @@ def find_by_email(email) # return 'wrong password' # end # end - +######################################################################################### # # do a sign out too end diff --git a/spec/seeds.sql b/spec/seeds.sql index 67941c5fc1..e9188e7055 100644 --- a/spec/seeds.sql +++ b/spec/seeds.sql @@ -1,12 +1,11 @@ TRUNCATE TABLE peeps RESTART IDENTITY CASCADE; TRUNCATE TABLE users RESTART IDENTITY CASCADE; - INSERT INTO users (username, email, password) VALUES ('user1', 'user1@example.com', ''), ('user2', 'user2@example.com', ''); -INSERT INTO peeps (peep, timestamp, username_id) VALUES +INSERT INTO peeps (peep, username_id, timestamp) VALUES ('Test peep 1', '2023-04-10 12:34:56', 1), ('Test peep 2', '2023-04-11 15:30:00', 2), ('Test peep 3', '2023-04-12 20:30:00', 2); diff --git a/views/homepage.erb b/views/homepage.erb new file mode 100644 index 0000000000..f14d258e98 --- /dev/null +++ b/views/homepage.erb @@ -0,0 +1,15 @@ +

Welcome to Chitter

+ +

Most Recent Peeps

+ +<% @peeps.each do |peep| %> +
+ <%= Time.parse(peep.timestamp).strftime("%H:%M:%S on %B %d, %Y") %> +

<%= peep.peep %>

+
+
+<% end %> + + diff --git a/views/post_peep.erb b/views/peep.erb similarity index 64% rename from views/post_peep.erb rename to views/peep.erb index d2d2e24196..9970e0703c 100644 --- a/views/post_peep.erb +++ b/views/peep.erb @@ -1,11 +1,11 @@ - + diff --git a/views/peeps.erb b/views/peeps.erb deleted file mode 100644 index 27c6527449..0000000000 --- a/views/peeps.erb +++ /dev/null @@ -1,14 +0,0 @@ -

Welcome to Chitter

- -

Most Recent Peeps

- -<% @peeps.each do |peep| %> -
- <%= peep.timestamp.strftime("%H:%M:%S on %B %d, %Y") %> -

<%= peep.text %>

-
-<% end %> - - \ No newline at end of file From 2b459556bc8de5b00ba5bbe8a468438553fe094b Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Thu, 13 Apr 2023 13:13:16 +0100 Subject: [PATCH 23/28] updated seed data to match changes in my real database tables --- lib/user_repository.rb | 19 ------------------- spec/seeds.sql | 6 +++--- 2 files changed, 3 insertions(+), 22 deletions(-) diff --git a/lib/user_repository.rb b/lib/user_repository.rb index 7d43baf67d..cbabd5b112 100644 --- a/lib/user_repository.rb +++ b/lib/user_repository.rb @@ -57,23 +57,4 @@ def find_by_email(email) fail 'No such user with given email.' end end - - ###################################################################################### - # implement this feature if you have time: - - # def sign_in(email, submitted_password) - # user = find_by_email(email) - - # return nil if user.nil? - - # # Compare the submitted password with the encrypted one saved in the database - # stored_password = BCrypt::Password.new(user.password) - # if stored_password == submitted_password - # return 'login success' - # else - # return 'wrong password' - # end - # end -######################################################################################### - # # do a sign out too end diff --git a/spec/seeds.sql b/spec/seeds.sql index e9188e7055..fec90b607d 100644 --- a/spec/seeds.sql +++ b/spec/seeds.sql @@ -6,7 +6,7 @@ INSERT INTO users (username, email, password) VALUES ('user2', 'user2@example.com', ''); INSERT INTO peeps (peep, username_id, timestamp) VALUES - ('Test peep 1', '2023-04-10 12:34:56', 1), - ('Test peep 2', '2023-04-11 15:30:00', 2), - ('Test peep 3', '2023-04-12 20:30:00', 2); + ('Test peep 1', 1, '2023-04-10 12:34:56'), + ('Test peep 2', 2, '2023-04-11 15:30:00'), + ('Test peep 3', 2, '2023-04-12 20:30:00'); From 5eee7386cc67e1b1bb4a41c454bed7a2e27b320e Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Thu, 13 Apr 2023 13:19:21 +0100 Subject: [PATCH 24/28] changed route names --- spec/integration/app_spec.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index caa2b3831e..ac68932b65 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -31,23 +31,23 @@ end describe 'GET /peeps' do it 'returns all peeps from most recent date' do - response = get('/') + response = get('/homepage') expect(response.status).to eq 200 expect(response.body).to include '

Most Recent Peeps

' end end describe 'GET /post_peep' do it 'returns the post peep page' do - response = get('/post_peep') + response = get('/peep') expect(response.status).to eq 200 expect(response.body).to include '' end end describe 'POST /' do it 'creates a new peep' do - post '/', { peep: 'This is a new peep' }, { 'rack.session' => { user_id: 1 } } + post '/peep', { peep: 'This is a new peep' }, { 'rack.session' => { user_id: 1 } } expect(last_response.status).to eq(302) - expect(PeepRepository.new.all.first.text).to eq('This is a new peep') + expect(PeepRepository.new.all.last.peep).to eq('This is a new peep') end end end \ No newline at end of file From 2569c8f226dc7d5f4428dddfa645341e7b155a54 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Thu, 13 Apr 2023 13:20:33 +0100 Subject: [PATCH 25/28] renaming --- spec/integration/app_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/integration/app_spec.rb b/spec/integration/app_spec.rb index ac68932b65..db082ae7b8 100644 --- a/spec/integration/app_spec.rb +++ b/spec/integration/app_spec.rb @@ -29,21 +29,21 @@ expect(UserRepository.new.find_by_email('user1@gmail.com').username).to eq 'user1' end end - describe 'GET /peeps' do + describe 'GET /homepage' do it 'returns all peeps from most recent date' do response = get('/homepage') expect(response.status).to eq 200 expect(response.body).to include '

Most Recent Peeps

' end end - describe 'GET /post_peep' do + describe 'GET /peep' do it 'returns the post peep page' do response = get('/peep') expect(response.status).to eq 200 expect(response.body).to include '' end end - describe 'POST /' do + describe 'POST /peep' do it 'creates a new peep' do post '/peep', { peep: 'This is a new peep' }, { 'rack.session' => { user_id: 1 } } expect(last_response.status).to eq(302) From 484f7013f4b8cb4f604043b97fa5fe5029d90fcc Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Thu, 13 Apr 2023 13:31:05 +0100 Subject: [PATCH 26/28] gemfile gems added/tweaked --- Gemfile | 4 +--- Gemfile.lock | 22 ---------------------- 2 files changed, 1 insertion(+), 25 deletions(-) diff --git a/Gemfile b/Gemfile index bc919d582d..ad9fe69ef3 100644 --- a/Gemfile +++ b/Gemfile @@ -13,10 +13,8 @@ group :development, :test do end gem "pg", "~> 1.4" - gem "sinatra", "~> 3.0" gem "sinatra-contrib", "~> 3.0" gem "webrick", "~> 1.8" gem "rack-test", "~> 2.1" - -gem 'bcrypt', '~> 3.1.18' \ No newline at end of file +gem 'bcrypt', '~> 3.1.18' diff --git a/Gemfile.lock b/Gemfile.lock index 043ba10309..49a7a97b57 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,37 +1,18 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.8.4) - public_suffix (>= 2.0.2, < 6.0) ansi (1.5.0) ast (2.4.2) bcrypt (3.1.18) - capybara (3.39.0) - addressable - matrix - mini_mime (>= 0.1.3) - nokogiri (~> 1.8) - rack (>= 1.6.0) - rack-test (>= 0.6.3) - regexp_parser (>= 1.5, < 3.0) - xpath (~> 3.2) diff-lcs (1.4.4) docile (1.4.0) - matrix (0.4.2) - mini_mime (1.1.2) - mini_portile2 (2.8.1) multi_json (1.15.0) mustermann (3.0.0) ruby2_keywords (~> 0.0.1) - nokogiri (1.14.2) - mini_portile2 (~> 2.8.0) - racc (~> 1.4) parallel (1.20.1) parser (3.0.2.0) ast (~> 2.4.1) pg (1.4.6) - public_suffix (5.0.1) - racc (1.6.2) rack (2.2.6.4) rack-protection (3.0.5) rack @@ -92,15 +73,12 @@ GEM tilt (2.1.0) unicode-display_width (2.0.0) webrick (1.8.1) - xpath (3.2.0) - nokogiri (~> 1.8) PLATFORMS ruby DEPENDENCIES bcrypt (~> 3.1.18) - capybara pg (~> 1.4) rack-test (~> 2.1) rspec From 15e7639f01b955e5715546f6e879c498cff2ff9d Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Thu, 13 Apr 2023 13:31:38 +0100 Subject: [PATCH 27/28] Setup for deploy --- Gemfile.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Gemfile.lock b/Gemfile.lock index 49a7a97b57..6a34d0d0ec 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -76,6 +76,7 @@ GEM PLATFORMS ruby + x86_64-linux DEPENDENCIES bcrypt (~> 3.1.18) From 43c5f7cf189ded6f1ac06fb6f4d1d639c8f31873 Mon Sep 17 00:00:00 2001 From: Sebastien Wylleman <31222029+sebwylleman@users.noreply.github.com> Date: Mon, 24 Apr 2023 17:56:58 +0100 Subject: [PATCH 28/28] testing git --- lib/database_connection.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/database_connection.rb b/lib/database_connection.rb index c8983ed6db..68b8f27943 100644 --- a/lib/database_connection.rb +++ b/lib/database_connection.rb @@ -30,7 +30,6 @@ def self.setup(dbname) end def self.clear(dbname) - @connection = PG.connect(dbname: dbname) @connection.exec("TRUNCATE TABLE users;") end end \ No newline at end of file