This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enabling users to create api sessions
- Loading branch information
1 parent
d9b2bc3
commit d699699
Showing
12 changed files
with
125 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'securerandom' | ||
|
||
module Todo | ||
module Models | ||
class Session | ||
include DataMapper::Resource | ||
|
||
property :access_token, String | ||
|
||
belongs_to :user, 'Todo::Models::User', key: true | ||
|
||
before :create do |session| | ||
session.access_token = SecureRandom.uuid | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module Todo | ||
module Services | ||
autoload :SessionCreator, 'todo/services/session_creator' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Todo | ||
module Services | ||
class SessionCreator | ||
|
||
def initialize(params) | ||
@params = params | ||
@result = nil | ||
end | ||
|
||
def valid? | ||
!!user | ||
end | ||
|
||
def access_token | ||
user && !@session && create_session | ||
@access_token | ||
end | ||
|
||
def errors | ||
{ password: 'email or password invalid' } | ||
end | ||
|
||
private | ||
|
||
def user | ||
@user ||= Todo::Models::User.first(email: @params[:email], | ||
password: @params[:password]) | ||
end | ||
|
||
def create_session | ||
@session = Models::Session.create user: @user | ||
if @session.saved? | ||
@access_token = @session.access_token | ||
else | ||
fail 'Session could not be created' | ||
end | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
describe 'Sessions API' do | ||
it 'a user can create a session' | ||
require 'spec_helper' | ||
|
||
describe 'Sessions API', api: true do | ||
|
||
it 'a user can create a session' do | ||
params = { email: 'test@test.com', password: '123test123' } | ||
user = Todo::Models::User.create! params | ||
|
||
post '/api/sessions', { user: params } | ||
|
||
expect(last_response.headers['Content-Type']).to eq 'application/json;charset=utf-8' | ||
expect(last_response.headers['Set-Cookie']).to match /access_token/ | ||
expect(last_response.body).to eq '' | ||
expect(last_response.status).to eq 200 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
require 'spec_helper' | ||
|
||
describe Todo::Services::SessionCreator do | ||
|
||
it 'successfully creates a session for an existing user' do | ||
params = { email: 'test@test.com', password: '123test123' } | ||
user = Todo::Models::User.create! params | ||
session_creator = Todo::Services::SessionCreator.new params | ||
expect(session_creator.valid?).to be_true | ||
expect(session_creator.access_token).not_to be_nil | ||
expect(session_creator.errors).not_to be_nil | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters