Code Monkey home page Code Monkey logo

devise_roles_lab's Introduction

Devise with Roles

Learning Objectives

  1. Explain the use of role-based authorization with Devise
  2. Design a set of roles to model a forum with different permission levels
  3. Set up Devise roles to implement such a model

Data Model

In this lab, we're going to make a simple discussion board.

First we'll have a User model, which pretty much always shows up when we talk about Devise. There will be three possible roles: user, vip, and admin.

We'll also have a Post model.

  • Users can read anyone's Posts, and create, read, update, and delete their own Posts.
  • VIPs can do everything a User can do, and update other users' Posts.
  • Admins can do anything that Users and VIPs can do, and they can delete other users' Posts.

Instructions

Provided is a Rails skeleton with the Devise gem installed.

  1. Run the migrations. A basic User model and its migrations have been set up for you as part of the devise install. We'll want to get the roles working.
  2. Take a look at the User model and the User spec
  3. Run rspec spec/models/user_spec.rb
  4. Add roles to the User model to get the User model specs working
  5. Note: Running rspec spec/models/post_spec.rb will fail because we haven't created those model or tables yet.
  6. Now that our User model is valid and has roles, let's get its controller working. Run its spec (and it will fail!) with rspec ./spec/controllers/users_controller_spec.rb
  7. Use the failing specs to help you update the UsersController. You'll want to use authentication and authorization filters to help accomplish this goal. Ensure that only administrators can update or destroy users
  8. Run rails generate devise:views to generate the views
  9. Be sure to add the ERB code to display the flash in app/views/layouts/application.html.erb

Once you've reached this point, all the User specs should be passing. For the next part, we'll create a Post model, which has different permissions for different User roles.

We've written one basic Post spec for you, but you will need to write additional specs for the Post model and controller. You can reference the feature and model specs for the Users controller to see how to write these.

  1. Create a Post model and migration. Posts have an owner and content
  2. Create the Posts controller. Ensure that it enforces the following requirements:
  • Posts can be created by any user
  • Anyone can read any Post
  • Users can edit or delete Posts they own
  • VIPs can edit anyone's Posts
  • Admins can do anything to any Post
  1. Write views for your Posts
  2. Try it out!

Resources

devise_roles_lab's People

Contributors

alexgriff avatar annjohn avatar blake41 avatar danielseehausen avatar dependabot[bot] avatar gj avatar lizbur10 avatar maxwellbenton avatar peterbell avatar pletcher avatar queerviolet avatar rrcobb avatar thelucasmoore avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

devise_roles_lab's Issues

Add more tests to solution as per README

The README suggests that the solution should include creating additional specs for the the posts model and posts controller. I struggled with this, only to find that the solution tests are exactly as forked. I have some additional tests in my branch, I'm happy to have those included in the solution (though it doesn't appear I can make a pull request to the solution branch with only that file).

Alot of issues with Tests

There seem to be alot of issues with tests on this lab.

As noted on another issue, need to include the sessions_helpers.rb in rails_helpers.rb file

session_helpers.rb file line 15 needs to be changed to 'Log in' rather than 'Sign in' as devise uses the term 'Log in' for the buttons.

Also sign_out_spec.rb line 16 is causing issues. I have had to comment out this line to get the test to pass.

Unresolved Merge?

lines 175-179
config/initializers/devise.rb

<<<<<<< HEAD
  config.reconfirmable = true
=======
  config.reconfirmable = false
>>>>>>> origin/solution

some dependency causing hang

this lab, and not others around it, is causing an endless hang when bundle install is called
Resolving Dependencies...... running without end.
haven't tried to recreate on any other machine

lab needs reworking

This lab needs some major examination.

In spec/features/users/user_delete_spec, if you comment out skip 'skip a slow test', the test will run, but opens a new Firefox window and times out. Not touching that.

I truly have no idea how spec/features/visitors/about_page_spec is passing. It visits pages/about and looks for the text "About the Website", but it's nowhere in my code. It hits a No such page error when I try to visit pages/about manually.

I noticed about 90% of the submissions didn't have tests/code for Posts. I think there should be some dummy tests to help students remember to write them. Something like:

it 'writes some tests, or else it gets the hose again'
  expect(true).to eq(false)
end

would help remind students. I personally think it's just muscle memory, not laziness. "All green, next lesson."

lab not the same as instructions

In the instructions for this lab it says :
Once you've reached this point, all the specs should be passing. For the next part, we'll create a Post model, which has different permissions for different user roles.

I the repo that I cloned, the specs for the post controller are already created. It seems like the flow of the instructions do not match well with the flow of the tests.

Flash messages need to be added

We tell the student we've already installed devise, so they do not see the install instructions telling them to add flash messages to the application.html.erb or how to generate the devise views. We tell them how to gen the views, but don't mention anything about the flash messages.

sign_up_spec not linking

sign_up_spec.rb:12 uses #sign_up_with method that is defined in suppord/helpers/session_helpers.rb. But, the spec can't access the method for some reason. giving this error:

Failures:

  1) Sign Up visitor cannot sign up with invalid email address
     Failure/Error: sign_up_with('bogus', 'please123', 'please123')

     NoMethodError:
       undefined method `sign_up_with' for #<RSpec::ExampleGroups::SignUp:0x007fe784ef2658>
     # ./spec/features/visitors/sign_up_spec.rb:26:in `block (2 levels) in <top (required)>'

Finished in 1.75 seconds (files took 2.23 seconds to load)
16 examples, 1 failure, 1 pending

Failed examples:

rspec ./spec/features/visitors/sign_up_spec.rb:12 # Sign Up visitor cannot sign up with invalid email address

A work around is to move the logic from that helper file to the spec. sign_up_spec.rb:12:

    visit new_user_registration_path
      fill_in 'Email', with: '[email protected]'
      fill_in 'Password', with: 'please123'
      fill_in 'Password confirmation', :with => 'please123'
      click_button 'Sign up'
    txts = [I18n.t( 'devise.registrations.signed_up'), I18n.t( 'devise.registrations.signed_up_but_unconfirmed')]
    expect(page).to have_content(/.*#{txts[0]}.*|.*#{txts[1]}.*/)

This problem occurs in the following specs (like line 25) too.
You can see my implementation here:
https://github.com/onyoo/devise_roles_lab-v-000/blob/master/spec/features/visitors/sign_up_spec.rb

Rspec issues

The specs can't access file: spec/support/helpers/session_helpers.rb where signin is defined.

Right out of the box, if you run rspec you get:

Sign in
  user cannot sign in if not registered (FAILED - 1)

Failures:

  1) Sign in user cannot sign in if not registered
     Failure/Error: signin('[email protected]', 'please123')

     NoMethodError:
       undefined method `signin' for #<RSpec::ExampleGroups::SignIn:0x007f90393e89f8>
     # ./spec/features/users/sign_in_spec.rb:12:in `block (2 levels) in <top (required)>'

Finished in 0.0641 seconds (files took 2.19 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/features/users/sign_in_spec.rb:11 # Sign in user cannot sign in if not registered

I temporarily fixed this issue by moving the logic out of the helpers file and replacing the signin method. Starting line sign_in_spec:12:

      visit new_user_session_path
      fill_in 'Email', with: '[email protected]'
      fill_in 'Password', with: 'please123'
      click_button 'Sign in'

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

With the above code in, sign_in_spec:26:

      visit new_user_session_path
      fill_in 'Email', with: user.email
      fill_in 'Password', with: user.password
      click_button 'Log in'

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ok ALL the tests have the same issue. Just modify them accordingly.
Can use mine as a guide:
https://github.com/onyoo/devise_roles_lab-v-000/blob/master/spec/features/users/sign_in_spec.rb

PS if you get uninitialized constant for FactoryGirl on the second test, delete your Gemfile.lock and bundle install.

undefined method `signin' for #<RSpec::ExampleGroups::SignOut:0x00000006581af0>

Hello ,
This lab has a test bug ,
spec/users/sign_out_spec.rb line number 13 ##> signin(user.email, user.password)
there is no such a method name for Devise , the correct name is sign_in
I know that because when change the method call to ' sign_in(user.email, user.password) ' , it works and the test pass otherwise , I get the error below.

  1. Sign out user signs out successfully
    Failure/Error: signin(user.email, user.password)

    NoMethodError:
    undefined method `signin' for #RSpec::ExampleGroups::SignOut:0x00000006581af0
    Did you mean? sign_in

    ./spec/features/users/sign_out_spec.rb:13:in `block (2 levels) in <top (required)>'

best ,
Ali Alrawi

Readme wants students to write new specs

The readme says, after passing all included specs, we should create a Post model with a corresponding controller and views, and write our own specs. The curriculum thus far has never expected students to write our own tests for a lab (nor taught how to do it).

Change spec file

in spec/features/users/sign_out_spec.rb line 13 change from

signin(user.email, user.password)
to
sign_in(user.email, user.password)

Needs more roles practice

I feel like this lab has very little to do with Devise Roles overall. Lots of work in views and partials. Things like creating a nav seem out of place and just busy work. This could be by design but I feel this lab could be revised and offer more practice on roles. The video review even has Avi starting it off saying he really wants to get to the Roles part.

user_spec.rb

This set of tests checks primarily that the role set returns a certain value. If we look at this example, found on user_spec.rb:18 :

  it "#role can be set to :vip" do
    @user.role = :vip
    expect(@user.role).to eq "vip"
  end

we will get the following error:

Failures:

  1) User #role can be set to :vip
     Failure/Error: expect(@user.role).to eq :vip

       expected: :vip
            got: "vip"

       (compared using ==)

       Diff:
       @@ -1,2 +1,2 @@
       -:vip
       +"vip"

     # ./spec/models/user_spec.rb:20:in `block (2 levels) in <top (required)>'

To I realize the test passes with the solution, but the only way I could get it to pass was to change line 20 to:

    expect(@user.role).to eq "vip"

I'd be interested to know how else to not have it return a string.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.