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
2 changes: 1 addition & 1 deletion app/mailers/user_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class UserMailer < ApplicationMailer
def definicao_senha(user)
@user = user
@url = "http://localhost:3000/definicao_senha"
mail(to: @user.email, subject: "Definição de Senha - Sistema de Gestão")
mail(to: @user.email_address, subject: "Definição de Senha - Sistema de Gestão")
end

# Email de cadastro com senha temporária (novo método)
Expand Down
38 changes: 38 additions & 0 deletions spec/mailers/user_mailer_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'

RSpec.describe UserMailer, type: :mailer do
describe 'definicao_senha' do
let(:user) do
User.create(nome: 'Teste', email_address: 'teste@example.com', matricula: '111', password: 'password')
end
let(:mail) { UserMailer.definicao_senha(user) }



it 'renders the headers' do
expect(mail.subject).to eq('Definição de Senha - Sistema de Gestão')
expect(mail.to).to eq([ user.email_address ])
end

it 'renders the body' do
expect(mail.body.encoded).to match('Definição de Senha')
end
end

describe 'cadastro_email' do
let(:user) do
User.create(nome: 'Teste', email_address: 'teste@example.com', matricula: '222', password: 'password')
end
let(:password) { 'secret123' }
let(:mail) { UserMailer.cadastro_email(user, password) }

it 'renders the headers' do
expect(mail.subject).to eq('Bem-vindo(a) ao CAMAAR - Sua senha de acesso')
expect(mail.to).to eq([ user.email_address ])
end

it 'renders the body with password' do
expect(mail.body.encoded).to match(password)
end
end
end
15 changes: 15 additions & 0 deletions spec/models/matricula_turma_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

RSpec.describe MatriculaTurma, type: :model do
describe 'associations' do
it 'belongs to user' do
association = described_class.reflect_on_association(:user)
expect(association.macro).to eq :belongs_to
end

it 'belongs to turma' do
association = described_class.reflect_on_association(:turma)
expect(association.macro).to eq :belongs_to
end
end
end
16 changes: 16 additions & 0 deletions spec/models/turma_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'rails_helper'

RSpec.describe Turma, type: :model do
describe 'associations' do
it 'has many matricula_turmas' do
association = described_class.reflect_on_association(:matricula_turmas)
expect(association.macro).to eq :has_many
end

it 'has many users through matricula_turmas' do
association = described_class.reflect_on_association(:users)
expect(association.macro).to eq :has_many
expect(association.options[:through]).to eq :matricula_turmas
end
end
end
20 changes: 20 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'rails_helper'

RSpec.describe User, type: :model do
describe 'associations' do
it 'has many matricula_turmas' do
association = described_class.reflect_on_association(:matricula_turmas)
expect(association.macro).to eq :has_many
end

it 'has many turmas through matricula_turmas' do
association = described_class.reflect_on_association(:turmas)
expect(association.macro).to eq :has_many
expect(association.options[:through]).to eq :matricula_turmas
end
end

describe 'validations' do
# Adicione validações aqui se houver
end
end
96 changes: 96 additions & 0 deletions spec/services/sigaa_import_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
require 'rails_helper'
require 'rspec/support/differ'
require 'rspec/support/hunk_generator'
require 'diff/lcs'

RSpec.describe SigaaImportService, type: :service do
let(:json_path) { Rails.root.join('spec/fixtures/turmas.json') }
let(:csv_path) { Rails.root.join('spec/fixtures/turmas.csv') }
let(:invalid_path) { Rails.root.join('spec/fixtures/invalid.txt') }

before do
# Garante que fixtures existem ou são mockados
allow(File).to receive(:exist?).and_return(true)
allow(File).to receive(:read).with(json_path).and_return([
{
'code' => 'T01',
'semester' => '2024.1',
'dicente' => [
{
'matricula' => '123456',
'nome' => 'João Silva',
'email' => 'joao@example.com'
}
],
'docente' => {
'usuario' => '654321',
'nome' => 'Maria Professora',
'email' => 'maria@prof.com'
}
}
].to_json)

allow(CSV).to receive(:foreach).with(csv_path, headers: true, col_sep: ',').and_yield(
CSV::Row.new(%w[codigo_turma nome_turma semestre nome_usuario email matricula papel],
[ 'T02', 'Banco de Dados', '2024.1', 'Maria Souza', 'maria@example.com', '654321', 'professor' ])
)

allow(File).to receive(:extname).and_call_original
allow(File).to receive(:extname).with(json_path).and_return('.json')
allow(File).to receive(:extname).with(csv_path).and_return('.csv')
allow(File).to receive(:extname).with(invalid_path).and_return('.txt')
end

class DummyMessage
def deliver_now
true
end
end

class DummyMailer
def self.cadastro_email(user, password)
DummyMessage.new
end
end

describe '#process' do
context 'with JSON file' do
it 'creates turmas and users' do
Turma.delete_all
User.delete_all

service = SigaaImportService.new(json_path)
result = service.process
puts "JSON Import Errors: #{result[:errors]}" if result[:errors].any?

expect(Turma.count).to eq(1)
expect(User.count).to eq(2)
end
end

context 'with CSV file' do
it 'creates turmas and users' do
Turma.delete_all
User.delete_all

service = SigaaImportService.new(csv_path)
result = service.process
puts "CSV Import Errors: #{result[:errors]}" if result[:errors].any?

expect(Turma.count).to eq(1)
expect(User.count).to eq(1)
end
end

context 'with unsupported file format' do
it 'returns error' do
service = SigaaImportService.new(invalid_path)
result = service.process
# Manual check to avoid RSpec HunkGenerator error
unless result[:errors].join(', ').include?('Formato de arquivo não suportado')
raise "Expected error 'Formato de arquivo não suportado' not found in: #{result[:errors]}"
end
end
end
end
end