# -*- coding: utf-8 -*- require 'spec_helper' describe Magellan::Cli::Login do describe :login! do let(:email){ "magellan@groovenauts.jp" } let(:password){ "password" } let(:auth_token){ "047bcCC1dnyVE+7DWE6YKIJF97L/qHk1mPrf2oaqWtE=" } before do allow(File).to receive(:readable?).and_return(true) allow(YAML).to receive(:load_file).with(".magellan-cli").and_return({"login" => {"email" => email, "password" => password} }) end context :production do let(:cli){ Magellan::Cli::Login.new("https://example.com") } before do res = double(:res) allow(cli.httpclient).to receive(:get).with("https://example.com/users/sign_in.html").and_return(res) allow(res).to receive(:status).and_return(200) allow(res).to receive(:body).and_return(File.read(File.expand_path("../login_page.html", __FILE__))) allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8")) end let(:params) do { "user" => {"email" => email, "password" => password}, "authenticity_token" => auth_token, } end it :login_success do res = double(:res) allow(cli.httpclient).to receive(:post).with("https://example.com/api/sign_in.json", params.to_json, Magellan::Cli::JSON_HEADER).and_return(res) allow(res).to receive(:status).and_return(200) allow(res).to receive(:body).and_return({"success" => true}.to_json) allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8")) expect(cli.api_login!(email, password)).to eq true end it :login_failure do res = double(:res) allow(cli.httpclient).to receive(:post).with("https://example.com/api/sign_in.json", params.to_json, Magellan::Cli::JSON_HEADER).and_return(res) allow(res).to receive(:status).and_return(401) # Unauthorized allow(res).to receive(:body).and_return({"success" => false, "message" => "Error with your login or password"}.to_json) allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8")) expect(cli.api_login!(email, password)).to eq false end end context :development do let(:cli){ Magellan::Cli::Login.new("http://localhost:3001") } before do res = double(:res) allow(cli.httpclient).to receive(:get).with("http://localhost:3001/users/sign_in.html").and_return(res) allow(res).to receive(:status).and_return(200) allow(res).to receive(:body).and_return(File.read(File.expand_path("../login_page.html", __FILE__))) allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8")) end let(:params) do { "user" => {"email" => email, "password" => password}, "authenticity_token" => auth_token, } end it :login_success do res = double(:res) allow(cli.httpclient).to receive(:post).with("http://localhost:3001/api/sign_in.json", params.to_json, Magellan::Cli::JSON_HEADER).and_return(res) allow(res).to receive(:status).and_return(200) allow(res).to receive(:body).and_return({"success" => true}.to_json) allow(res).to receive(:body_encoding).and_return(Encoding.find("UTF-8")) expect(cli.api_login!(email, password)).to eq true end end end end