require 'rails_helper'

describe V1::UserSessionsController do
  Dictum.resource(name: 'UserSessions', description: 'Manage user sessions')

  describe '#login' do
    let(:password) { SecureRandom.hex(6) }
    let!(:user) { create :user, password: password }

    context 'when uses valid data' do
      it 'succeeds', dictum: true, dictum_description: 'Create a user session' do
        post :login, params: { email: user.email, password: password }, format: :json
        expect(response).to have_http_status :created
      end
    end

    it 'fails when misses the email' do
      post :login, params: { password: password }, format: :json
      expect(response).to have_http_status :bad_request
    end

    it 'fails when misses the password' do
      post :login, params: { email: user.email }, format: :json
      expect(response).to have_http_status :bad_request
    end

    it 'fails when the email in invalid' do
      post :login, params: { email: "x#{user.email}", password: password }, format: :json
      expect(response).to have_http_status :unauthorized
    end

    it 'fails when the password is invalid' do
      post :login, params: { email: user.email, password: "1#{password}" }, format: :json
      expect(response).to have_http_status :unauthorized
    end
  end
end
