Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gem "nokogiri"
gem "feedbag", github: "dwillis/feedbag"
gem "coveralls", require: false
gem "uglifier"
gem "highline", require: false

group :production do
gem "unicorn"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ GEM
foreman (0.63.0)
dotenv (>= 0.7)
thor (>= 0.13.6)
highline (1.6.19)
i18n (0.6.4)
jsmin (1.0.1)
kgio (2.8.0)
Expand Down Expand Up @@ -154,6 +155,7 @@ DEPENDENCIES
feedbag!
feedzirra!
foreman
highline
i18n
loofah
nokogiri
Expand Down
6 changes: 6 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Bundler.require

require "./app"
require_relative "./app/tasks/fetch_feeds"
require_relative "./app/tasks/change_password"

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

desc "Change your password"
task :change_password do
ChangePassword.new.change_password
end

require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:speedy_tests) do |t|
Expand Down
12 changes: 12 additions & 0 deletions app/commands/users/change_user_password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ChangeUserPassword
def initialize(repository = User)
@repo = repository
end

def change_user_password(new_password)
user = @repo.first
user.password = new_password
user.save
user
end
end
36 changes: 36 additions & 0 deletions app/tasks/change_password.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require "highline"
require_relative "../commands/users/change_user_password"

class ChangePassword
def initialize(ui = HighLine.new, command = ChangeUserPassword.new)
@ui = ui
@command = command
end

def change_password
while (password = ask_password) != (confirmation = ask_confirmation)
@ui.say "The confirmation doesn't match the password. Please try again."
end
@command.change_user_password(password)
end

private

def ask_password
ask_hidden("New password: ") do |q|
q.validate = /\A.+\Z/
q.responses[:not_valid] = "The password can't be blank."
end
end

def ask_confirmation
ask_hidden("Confirmation: ")
end

def ask_hidden(question)
@ui.ask(question) do |q|
q.echo = "*"
yield(q) if block_given?
end
end
end
28 changes: 28 additions & 0 deletions spec/commands/users/change_user_password_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require "spec_helper"

app_require "commands/users/change_user_password"

describe ChangeUserPassword do
let(:repo) do
double("repo")
end

let(:user) do
double("user", id: 1, password_digest: BCrypt::Password.create(old_password))
end

let(:old_password){ "old-pw" }
let(:new_password){ "new-pw" }

describe "#change_user_password" do
it "changes the password of the user" do
command = ChangeUserPassword.new(repo)

repo.should_receive(:first){ user }
user.should_receive(:password=).with(new_password)
user.should_receive(:save)

command.change_user_password(new_password)
end
end
end
35 changes: 35 additions & 0 deletions spec/tasks/change_password_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require "spec_helper"
app_require "tasks/change_password"

describe ChangePassword do
let(:ui) { double("ui") }
let(:command) { double("command") }
let(:new_password){ "new-pw" }

let(:task) { ChangePassword.new(ui, command) }

describe "#change_password" do
it "invokes command with confirmed password" do
ui.should_receive(:ask).twice
.and_return(new_password, new_password)

command
.should_receive(:change_user_password)
.with(new_password)

task.change_password
end

it "repeats until a matching confirmation" do
ui.should_receive(:ask).exactly(2).times
.and_return(new_password, "", new_password, new_password)

ui.should_receive(:say).with(/match/)
command
.should_receive(:change_user_password)
.with(new_password)

task.change_password
end
end
end