-
Notifications
You must be signed in to change notification settings - Fork 0
/
myPollApp.txt
158 lines (141 loc) · 4.69 KB
/
myPollApp.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
-rails new myPollApp
Add those gems inside development group:
gem 'shoulda-matchers'
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'capybara'
Run bundle install
>rails g model User email name provider uid
>rake db:migrate if any mistake run rake db:rollback
>rails g rspec:install , rails db:migrate RAILS_ENV=test
================================rspec================
https://github.com/rspec/rspec-rails
https://github.com/thoughtbot/shoulda-matchers
------------------
You can supply this information by using a configuration block. Place the following in rails_helper.rb:
Shoulda::Matchers.configure do |config|
require 'shoulda/matchers'
Shoulda::Matchers.configure do |config|
config.integrate do |with|
# Choose a test framework:
with.test_framework :rspec
# with.test_framework :minitest
# with.test_framework :minitest_4
# with.test_framework :test_unit
# # Choose one or more libraries:
# with.library :active_record
# with.library :active_model
# with.library :action_controller
# Or, choose the following (which implies all of the above):
with.library :rails
end
end
==================================================
Firts test type this in factories/models/user_spec.rb
require 'rails_helper'
RSpec.describe User, type: :model do
it { should validate_presence_of(:email) }
it { should_not allow_value("eda@codigo").for(:email) }
it { should allow_value("eda@codigo.com").for(:email) }
end
This should fail, to be able to past the test validate in model for user in model/user.rb type this:
validates :email, presence: true
#####You did pass you firt test hurray!!!!!!!
----------------
To be able to test email add this gem
https://github.com/hallelujah/valid_email
- gem 'valid_email'
IN models/user.rb change the line to this>
validates :email, presence: true, email: true
Run >rspec , and all this 3 test should past succesfully '"hurray"''
-----------------
Add two more test to user.spec.rb;
it { should validate_presence_of(:email) }
it { should_not allow_value("eda@codigo").for(:email) }
it { should allow_value("eda@codigo.com").for(:email) }
it { should validate_presence_of(:uid) }
it { should validate_presence_of(:provider) }
Then validate in the model models/user.rb
validates :email, presence: true, email: true
validates :uid, presence: true
validates :provider, presence: true
All 5 test should past!
> 5 examples, 0 failures
=============================================
2-How to authenticate users with tokens
=============================================
*Tokens_tbl
-id user->Token with user
-Random string ex-> 123bcdssldc41681asfjsa
-Expiration date
-user_id <-Foreign Key<- makes faster search for queries
*User_tbl
-User will has many tokens
*Applications_tbl
-Secrept key
-App ID
-----------------------------
Create table Token
>rails g model Token expires_at:datetime user:references token
Add this to models/user.rb
has_many :tokens
Add this in models/token.rb
before_create :generate_token
private
# This is rails method to genrate random tokens
def generate_token
# Ciclo do while in rails
begin
self.token = SecureRandom.hex
end while Token.where(token: self.token).any?
#expires for 1 montn 'from_now is a helper of rails'
self.expires_at = 1.month.from_now
end
----------
How to use factory_girl
https://github.com/thoughtbot/factory_girl_rails
----------
Add this in factories/tokens.tb
factory :token do
expires_at "2016-12-12 19:34:35"
# user nil
# token "MyString"
association :user, factory: :user
end
In factories/users.rb fill the fields like
email "eda@codigo.com"
name "Uriel"
provider "github"
uid "duah18wd21qwq2w1"
Add this method in models/token.rb
def is_valid?
end
----------------
if has pending migrations run: rails db:migrate RAILS_ENV=test
Then run 'rspec' , this test should fail
In models/token.rb make the method
def is_valid?
DateTime.now < self.expires_at
end
private
# This is rails method to genrate random tokens
def generate_token
# Ciclo do while in rails
begin
self.token = SecureRandom.hex
end while Token.where(token: self.token).any?
#expires for 1 montn 'from_now is a helper of rails'
self.expires_at ||= 1.month.from_now
end
---------
In factories/models/token_spec.rb add this two tests:
it "should return valid when is not expired" do
token = FactoryGirl.create(:token,expires_at: DateTime.now + 1.minute)
expect(token.is_valid?).to eq(true)
end
it "should return invalid when is expired" do
token = FactoryGirl.create(:token,expires_at: DateTime.now - 1.day)
expect(token.is_valid?).to eq(false)
end
---
Then run> rspec and all test should past "Hurrayyyy"