WIP: Move verification specs

pull/688/head
Rick Carlino 2018-03-01 10:21:53 -06:00
parent b051aa9b8c
commit b4b90b0943
8 changed files with 35 additions and 126 deletions

View File

@ -28,12 +28,10 @@ class DashboardController < ApplicationController
end
def verify
user = User.find_by!(confirmation_token: params.fetch(:token)) or raise "X"
user.update_attributes!(confirmation_token: SecureRandom.uuid,
confirmed_at: Time.now)
@token = SessionToken.as_json(user,
AbstractJwtToken::HUMAN_AUD,
Gem::Version.new("99.99.99")).to_json
user = params[:token] && User.find_by!(confirmation_token: params[:token])
# Two use cases: re-confirmation Email change
klass = user.unconfirmed_email? ? Users::Reverify : Users::Verify
@token = klass.run!(user: user).to_json
render :confirmation_page, layout: false
end

View File

@ -122,17 +122,6 @@ describe Api::UsersController do
end
end
end
it 'can not re-verify' do
pending
user.update_attributes(confirmed_at: Time.now)
sign_in user
put :verify, params: { token: user.confirmation_token }, format: :json
expect(response.status).to eq(409)
expect(subject.default_serializer_options[:root]).to be false
expect(subject.default_serializer_options[:user]).to eq(user)
end
it 'handles password confirmation mismatch' do
email = Faker::Internet.email
original_count = User.count

View File

@ -1,27 +0,0 @@
require 'spec_helper'
describe Api::UsersController do
let(:user) { FactoryBot.create(:user, confirmed_at: nil) }
include Devise::Test::ControllerHelpers
it 'creates a new user' do
pending
params = { token: user.confirmation_token }
expect(user.confirmed_at).to eq(nil)
put :verify, params: params
user.reload
expect(user.confirmation_token).to be # TODO: Hmm..
expect(user.confirmed_at).to be
expect(user.confirmed_at - Time.now).to be < 3
end
it 'verifies email changes' do
pending
email = "foo@bar.com"
user.update_attributes!(unconfirmed_email: "foo@bar.com")
params = { token: user.confirmation_token }
put :verify, params: params
expect(user.reload.unconfirmed_email).to be nil
expect(user.email).to eq email
end
end

View File

@ -2,6 +2,7 @@ require 'spec_helper'
describe DashboardController do
include Devise::Test::ControllerHelpers
let(:user) { FactoryBot.create(:user, confirmed_at: nil) }
describe 'dashboard endpoint' do
it "renders the terms of service" do
@ -30,5 +31,35 @@ describe DashboardController do
.with("CSP VIOLATION!!!", {})
post :csp_reports, body: {}.to_json, params: {format: :json}
end
it 'creates a new user' do
params = { token: user.confirmation_token }
expect(user.confirmed_at).to eq(nil)
get :verify, params: params
user.reload
expect(user.confirmation_token).to be # TODO: Hmm..
expect(user.confirmed_at).to be
expect(user.confirmed_at - Time.now).to be < 3
end
it 'verifies email changes' do
email = "foo@bar.com"
user.update_attributes!(unconfirmed_email: "foo@bar.com")
params = { token: user.confirmation_token }
get :verify, params: params
expect(user.reload.unconfirmed_email).to be nil
expect(user.email).to eq email
end
it 'can not re-verify' do
user.update_attributes(confirmed_at: Time.now)
sign_in user
expect do
get :verify, params: { token: user.confirmation_token }, format: :json
end.to raise_error(User::AlreadyVerified)
expect(response.status).to eq(409)
expect(subject.default_serializer_options[:root]).to be false
expect(subject.default_serializer_options[:user]).to eq(user)
end
end
end

View File

@ -1,5 +0,0 @@
require 'spec_helper'
RSpec.describe PinBinding, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -1,5 +0,0 @@
require 'spec_helper'
RSpec.describe Sensor, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

View File

@ -131,6 +131,4 @@ export class API {
get farmwareInstallationPath() {
return `${this.baseUrl}/api/farmware_installations`;
}
/** /api/users/verify/:token */
verificationPath = (token: string) => ("/api/users/verify/" + token);
}

View File

@ -1,70 +0,0 @@
// import { getParam, HttpData } from "./util";
// import axios, { AxiosResponse } from "axios";
// import { API } from "./api/api";
// import { Session } from "./session";
// import { AuthState } from "./auth/interfaces";
// /** Keep track of this in rollbar to prevent global registration failures. */
// export const ALREADY_VERIFIED =
// `<p>
// You are already verified. We will now forward you to the main application.
// </p>
// <p>
// If you are still unable to access the app, try logging in again or
// <a href="http://forum.farmbot.org/"> asking for help on the FarmBot Forum.</a>
// </p>`;
// const ALREADY_VERIFIED_MSG = "TRIED TO RE-VERIFY";
// export const FAILURE_PAGE =
// `<p>
// This verification link is not valid. Most likely you either copy/pasted
// the link incorrectly or are trying to use an old link. Please use the most
// recent email verification link that was sent to you.
// </p>
// <p>
// Please try again or <a href="http://forum.farmbot.org/"> ask for help on
// the FarmBot Forum.</a>
// </p>`;
// export const FAILURE_MSG = "USER VERIFICATION FAILED!";
// /** Function called when the Frontend verifies its registration token.
// * IF YOU BREAK THIS FUNCTION, YOU BREAK *ALL* NEW USER REGISTRATIONS. */
// // export const verify = async () => {
// // try {
// // await attempt();
// // } catch (e) {
// // fail(e);
// // }
// // };
// export async function attempt() {
// API.setBaseUrl(API.fetchBrowserLocation());
// type Resp = HttpData<AuthState>;
// const r: Resp =
// await axios.put(API.current.verificationPath(getParam("token")));
// Session.replaceToken(r.data);
// window.location.href = API.current.baseUrl + "/app/controls";
// }
// interface AxiosError extends Error {
// response?: AxiosResponse | undefined; // Need to be extra cautious here.
// }
// export function fail(err: AxiosError | undefined) {
// switch (err && err.response && err.response.status) {
// case 409:
// return alreadyVerified();
// default:
// document.write(FAILURE_PAGE);
// throw new Error(FAILURE_MSG);
// }
// }
// const alreadyVerified = (): never => {
// // Wait 2 seconds to let the user know what's going on.
// setTimeout(() => window.location.href = "/app/controls", 2000);
// document.write(ALREADY_VERIFIED);
// // Throw an error to keep track of stats (may be a sign of a system outage)
// throw new Error(ALREADY_VERIFIED_MSG);
// };