Skip to content

Commit 0d35ec2

Browse files
author
Matt Swanson
committed
Merge branch 'pr/120'
2 parents 939c3c1 + c933ba8 commit 0d35ec2

11 files changed

Lines changed: 136 additions & 3 deletions

File tree

‎Gemfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ gem "nokogiri"
1818
gem "feedbag", github: "dwillis/feedbag"
1919
gem "coveralls", require: false
2020
gem "uglifier"
21+
gem "highline", require: false
2122

2223
group :production do
2324
gem "unicorn"

‎Gemfile.lock‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ GEM
6969
foreman (0.63.0)
7070
dotenv (>= 0.7)
7171
thor (>= 0.13.6)
72+
highline (1.6.19)
7273
i18n (0.6.4)
7374
jsmin (1.0.1)
7475
kgio (2.8.0)
@@ -154,6 +155,7 @@ DEPENDENCIES
154155
feedbag!
155156
feedzirra!
156157
foreman
158+
highline
157159
i18n
158160
loofah
159161
nokogiri

‎Rakefile‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Bundler.require
55

66
require "./app"
77
require_relative "./app/tasks/fetch_feeds"
8+
require_relative "./app/tasks/change_password"
89

910
desc "Fetch all feeds."
1011
task :fetch_feeds do
@@ -26,6 +27,11 @@ task :work_jobs do
2627
Delayed::Worker.new(:min_priority => ENV['MIN_PRIORITY'], :max_priority => ENV['MAX_PRIORITY']).start
2728
end
2829

30+
desc "Change your password"
31+
task :change_password do
32+
ChangePassword.new.change_password
33+
end
34+
2935
require 'rspec/core/rake_task'
3036

3137
RSpec::Core::RakeTask.new(:speedy_tests) do |t|
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
require_relative "../../repositories/user_repository"
2+
3+
class ChangeUserPassword
4+
def initialize(repository = UserRepository)
5+
@repo = repository
6+
end
7+
8+
def change_user_password(new_password)
9+
user = @repo.first
10+
user.password = new_password
11+
12+
@repo.save(user)
13+
user
14+
end
15+
end

‎app/repositories/user_repository.rb‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,13 @@ def self.fetch(id)
1010
def self.setup_complete?
1111
User.any? && User.first.setup_complete?
1212
end
13+
14+
def self.save(user)
15+
user.save
16+
user
17+
end
18+
19+
def self.first
20+
User.first
21+
end
1322
end

‎app/tasks/change_password.rb‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
require "highline"
2+
require_relative "../commands/users/change_user_password"
3+
4+
class ChangePassword
5+
def initialize(ui = HighLine.new, command = ChangeUserPassword.new)
6+
@ui = ui
7+
@command = command
8+
end
9+
10+
def change_password
11+
while (password = ask_password) != (confirmation = ask_confirmation)
12+
@ui.say "The confirmation doesn't match the password. Please try again."
13+
end
14+
@command.change_user_password(password)
15+
end
16+
17+
private
18+
def ask_password
19+
ask_hidden("New password: ") do |q|
20+
q.validate = /\A.+\Z/
21+
q.responses[:not_valid] = "The password can't be blank."
22+
end
23+
end
24+
25+
def ask_confirmation
26+
ask_hidden("Confirmation: ")
27+
end
28+
29+
def ask_hidden(question)
30+
@ui.ask(question) do |q|
31+
q.echo = "*"
32+
yield(q) if block_given?
33+
end
34+
end
35+
end

‎config/database.yml‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
development:
22
adapter: sqlite3
3-
database: db/stringer.sqlite
3+
database: db/stringer.sqlite
4+
5+
test:
6+
adapter: sqlite3
7+
database: db/stringer_test.sqlite
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require "spec_helper"
2+
require "support/active_record"
3+
4+
app_require "commands/users/change_user_password"
5+
6+
describe ChangeUserPassword do
7+
let(:repo) { stub }
8+
let(:user) { User.create(password: old_password) }
9+
10+
let(:old_password) { "old-pw" }
11+
let(:new_password) { "new-pw" }
12+
13+
describe "#change_user_password" do
14+
it "changes the password of the user" do
15+
repo.should_receive(:first).and_return(user)
16+
repo.should_receive(:save)
17+
18+
command = ChangeUserPassword.new(repo)
19+
result = command.change_user_password(new_password)
20+
21+
BCrypt::Password.new(result.password_digest).should eq new_password
22+
end
23+
end
24+
end

‎spec/models/story_spec.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
require "spec_helper"
2-
require 'support/active_record'
2+
require "support/active_record"
33

44
app_require "models/story"
55

‎spec/support/active_record.rb‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'active_record'
22

3-
ActiveRecord::Base.establish_connection adapter: "sqlite3", database: "db/stringer_test.sqlite"
3+
config = YAML.load(File.read('config/database.yml'))
4+
ActiveRecord::Base.establish_connection(config['test'])
45

56
def need_to_migrate?
67
current_migration = ActiveRecord::Migrator.current_version

0 commit comments

Comments
 (0)