53 lines
1.7 KiB
Ruby
53 lines
1.7 KiB
Ruby
require "rails_helper"
|
|
|
|
RSpec.describe "Registrations API", type: :request do
|
|
describe "POST /api/register" do
|
|
let(:valid_attributes) do
|
|
{
|
|
user: {
|
|
nickname: "Sinbad",
|
|
password: "password123"
|
|
}
|
|
}
|
|
end
|
|
|
|
context "with valid parameters" do
|
|
it "registers a new user and returns 201 Created with token" do
|
|
expect {
|
|
post "/api/register", params: valid_attributes, as: :json
|
|
}.to change(User, :count).by(1)
|
|
|
|
expect(response).to have_http_status(:created)
|
|
json = JSON.parse(response.body)
|
|
|
|
expect(json["token"]).to be_present
|
|
expect(json["user"]["nickname"]).to eq("Sinbad")
|
|
expect(json["user"]["id"]).to eq(User.last.id)
|
|
expect(json["user"]["avatar_url"]).to be_nil
|
|
expect(json["user"]["created_at"]).to be_present
|
|
end
|
|
end
|
|
|
|
context "with invalid parameters" do
|
|
it "returns 422 Unprocessable Content when nickname is taken" do
|
|
User.create!(nickname: "Sinbad", password: "password123")
|
|
|
|
post "/api/register", params: valid_attributes, as: :json
|
|
|
|
expect(response).to have_http_status(422)
|
|
json = JSON.parse(response.body)
|
|
expect(json["errors"]).to include("Nickname has already been taken")
|
|
end
|
|
|
|
it "returns 422 Unprocessable Content when password is too short" do
|
|
invalid_attrs = { user: { nickname: "Sinbad", password: "123" } }
|
|
post "/api/register", params: invalid_attrs, as: :json
|
|
|
|
expect(response).to have_http_status(422)
|
|
json = JSON.parse(response.body)
|
|
expect(json["errors"]).to include("Password is too short (minimum is 6 characters)")
|
|
end
|
|
end
|
|
end
|
|
end
|