mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-12-11 08:11:21 -05:00
210 lines
8.9 KiB
CoffeeScript
210 lines
8.9 KiB
CoffeeScript
|
CreateAccountModal = require 'views/core/CreateAccountModal'
|
||
|
forms = require 'core/forms'
|
||
|
|
||
|
describe 'CreateAccountModal', ->
|
||
|
|
||
|
modal = null
|
||
|
|
||
|
beforeEach ->
|
||
|
modal = new CreateAccountModal()
|
||
|
modal.render()
|
||
|
modal.render = _.noop
|
||
|
jasmine.demoModal(modal)
|
||
|
window.gapi =
|
||
|
client:
|
||
|
load: _.noop
|
||
|
window.FB =
|
||
|
login: _.noop
|
||
|
api: _.noop
|
||
|
|
||
|
afterEach ->
|
||
|
modal.stopListening()
|
||
|
|
||
|
describe 'clicking the save button', ->
|
||
|
|
||
|
it 'fails if nothing is in the form, showing errors for email and password', ->
|
||
|
modal.$('form').each (i, el) -> el.reset()
|
||
|
modal.$('form:first').submit()
|
||
|
expect(jasmine.Ajax.requests.all().length).toBe(0)
|
||
|
expect(modal.$('.has-error').length).toBe(2)
|
||
|
|
||
|
it 'fails if email is missing', ->
|
||
|
modal.$('form').each (i, el) -> el.reset()
|
||
|
forms.objectToForm(modal.$el, { name: 'Name', password: 'xyzzy' })
|
||
|
modal.$('form:first').submit()
|
||
|
expect(jasmine.Ajax.requests.all().length).toBe(0)
|
||
|
expect(modal.$('.has-error').length).toBeTruthy()
|
||
|
|
||
|
it 'signs up if only email and password is provided', ->
|
||
|
modal.$('form').each (i, el) -> el.reset()
|
||
|
forms.objectToForm(modal.$el, { email: 'some@email.com', password: 'xyzzy' })
|
||
|
modal.$('form:first').submit()
|
||
|
requests = jasmine.Ajax.requests.all()
|
||
|
expect(requests.length).toBe(1)
|
||
|
expect(modal.$el.has('.has-warning').length).toBeFalsy()
|
||
|
expect(modal.$('#signup-button').is(':disabled')).toBe(true)
|
||
|
|
||
|
describe 'and a class code is entered', ->
|
||
|
|
||
|
beforeEach ->
|
||
|
modal.$('form').each (i, el) -> el.reset()
|
||
|
forms.objectToForm(modal.$el, { email: 'some@email.com', password: 'xyzzy', classCode: 'qwerty' })
|
||
|
modal.$('form:first').submit()
|
||
|
expect(jasmine.Ajax.requests.all().length).toBe(1)
|
||
|
|
||
|
it 'checks for Classroom existence if a class code was entered', ->
|
||
|
jasmine.demoModal(modal)
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.url).toBe('/db/classroom?code=qwerty')
|
||
|
|
||
|
it 'has not hidden the close-modal button', ->
|
||
|
expect(modal.$('#close-modal').css('display')).toBe('block')
|
||
|
|
||
|
it 'continues with signup if the Classroom exists', ->
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
request.respondWith({status: 200, responseText: JSON.stringify({})})
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.url).toBe('/db/user')
|
||
|
expect(request.method).toBe('POST')
|
||
|
|
||
|
it 'fails and shows an error if the Classroom does not exist', ->
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
request.respondWith({status: 404, responseText: JSON.stringify({})})
|
||
|
expect(jasmine.Ajax.requests.all().length).toBe(1)
|
||
|
expect(modal.$el.has('.has-error').length).toBeTruthy()
|
||
|
|
||
|
|
||
|
describe 'clicking the gplus button', ->
|
||
|
|
||
|
signupButton = null
|
||
|
|
||
|
beforeEach ->
|
||
|
application.gplusHandler.trigger 'render-login-buttons'
|
||
|
signupButton = modal.$('#gplus-signup-btn')
|
||
|
expect(signupButton.attr('disabled')).toBeFalsy()
|
||
|
signupButton.click()
|
||
|
application.gplusHandler.fakeGPlusLogin()
|
||
|
application.gplusHandler.trigger 'person-loaded', { firstName: 'Mr', lastName: 'Bean', gplusID: 'abcd', email: 'some@email.com' }
|
||
|
|
||
|
it 'checks to see if the user already exists in our system', ->
|
||
|
requests = jasmine.Ajax.requests.all()
|
||
|
expect(requests.length).toBe(1)
|
||
|
expect(signupButton.attr('disabled')).toBeTruthy()
|
||
|
|
||
|
|
||
|
describe 'and finding the given person is already a user', ->
|
||
|
beforeEach ->
|
||
|
expect(modal.$('#gplus-account-exists-row').hasClass('hide')).toBe(true)
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
request.respondWith({status: 200, responseText: JSON.stringify({_id: 'existinguser'})})
|
||
|
|
||
|
it 'shows a message saying you are connected with Google+, with a button for logging in', ->
|
||
|
expect(modal.$('#gplus-account-exists-row').hasClass('hide')).toBe(false)
|
||
|
loginBtn = modal.$('#gplus-login-btn')
|
||
|
expect(loginBtn.attr('disabled')).toBeFalsy()
|
||
|
loginBtn.click()
|
||
|
expect(loginBtn.attr('disabled')).toBeTruthy()
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.method).toBe('POST')
|
||
|
expect(request.params).toBe('gplusID=abcd&gplusAccessToken=1234')
|
||
|
expect(request.url).toBe('/auth/login-gplus')
|
||
|
|
||
|
describe 'and the user finishes signup anyway with new info', ->
|
||
|
beforeEach ->
|
||
|
forms.objectToForm(modal.$el, { email: 'some@email.com', schoolName: 'Hogwarts' })
|
||
|
modal.$('form:first').submit()
|
||
|
|
||
|
it 'upserts the values to the new user', ->
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.method).toBe('PUT')
|
||
|
expect(request.url).toBe('/db/user?gplusID=abcd&gplusAccessToken=1234')
|
||
|
expect(JSON.parse(request.params).schoolName).toBe('Hogwarts')
|
||
|
|
||
|
|
||
|
describe 'and finding the given person is not yet a user', ->
|
||
|
beforeEach ->
|
||
|
expect(modal.$('#gplus-logged-in-row').hasClass('hide')).toBe(true)
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
request.respondWith({status: 404})
|
||
|
|
||
|
it 'shows a message saying you are connected with Google+', ->
|
||
|
expect(modal.$('#gplus-logged-in-row').hasClass('hide')).toBe(false)
|
||
|
|
||
|
describe 'and the user finishes signup', ->
|
||
|
beforeEach ->
|
||
|
modal.$('form:first').submit()
|
||
|
|
||
|
it 'creates the user with the gplus attributes', ->
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.method).toBe('PUT')
|
||
|
expect(request.url).toBe('/db/user?gplusID=abcd&gplusAccessToken=1234')
|
||
|
expect(_.string.startsWith(request.url, '/db/user')).toBe(true)
|
||
|
expect(modal.$('#signup-button').is(':disabled')).toBe(true)
|
||
|
|
||
|
|
||
|
describe 'clicking the facebook button', ->
|
||
|
|
||
|
signupButton = null
|
||
|
|
||
|
beforeEach ->
|
||
|
Backbone.Mediator.publish 'auth:facebook-api-loaded', {}
|
||
|
signupButton = modal.$('#facebook-signup-btn')
|
||
|
expect(signupButton.attr('disabled')).toBeFalsy()
|
||
|
signupButton.click()
|
||
|
application.facebookHandler.fakeFacebookLogin()
|
||
|
application.facebookHandler.trigger 'person-loaded', { firstName: 'Mr', lastName: 'Bean', facebookID: 'abcd', email: 'some@email.com' }
|
||
|
|
||
|
it 'checks to see if the user already exists in our system', ->
|
||
|
requests = jasmine.Ajax.requests.all()
|
||
|
expect(requests.length).toBe(1)
|
||
|
expect(signupButton.attr('disabled')).toBeTruthy()
|
||
|
|
||
|
|
||
|
describe 'and finding the given person is already a user', ->
|
||
|
beforeEach ->
|
||
|
expect(modal.$('#facebook-account-exists-row').hasClass('hide')).toBe(true)
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
request.respondWith({status: 200, responseText: JSON.stringify({_id: 'existinguser'})})
|
||
|
|
||
|
it 'shows a message saying you are connected with Facebook, with a button for logging in', ->
|
||
|
expect(modal.$('#facebook-account-exists-row').hasClass('hide')).toBe(false)
|
||
|
loginBtn = modal.$('#facebook-login-btn')
|
||
|
expect(loginBtn.attr('disabled')).toBeFalsy()
|
||
|
loginBtn.click()
|
||
|
expect(loginBtn.attr('disabled')).toBeTruthy()
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.method).toBe('POST')
|
||
|
expect(request.params).toBe('facebookID=abcd&facebookAccessToken=1234')
|
||
|
expect(request.url).toBe('/auth/login-facebook')
|
||
|
|
||
|
describe 'and the user finishes signup anyway with new info', ->
|
||
|
beforeEach ->
|
||
|
forms.objectToForm(modal.$el, { email: 'some@email.com', schoolName: 'Hogwarts' })
|
||
|
modal.$('form:first').submit()
|
||
|
|
||
|
it 'upserts the values to the new user', ->
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.method).toBe('PUT')
|
||
|
expect(request.url).toBe('/db/user?facebookID=abcd&facebookAccessToken=1234')
|
||
|
expect(JSON.parse(request.params).schoolName).toBe('Hogwarts')
|
||
|
|
||
|
|
||
|
describe 'and finding the given person is not yet a user', ->
|
||
|
beforeEach ->
|
||
|
expect(modal.$('#facebook-logged-in-row').hasClass('hide')).toBe(true)
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
request.respondWith({status: 404})
|
||
|
|
||
|
it 'shows a message saying you are connected with Facebook', ->
|
||
|
expect(modal.$('#facebook-logged-in-row').hasClass('hide')).toBe(false)
|
||
|
|
||
|
describe 'and the user finishes signup', ->
|
||
|
beforeEach ->
|
||
|
modal.$('form:first').submit()
|
||
|
|
||
|
it 'creates the user with the facebook attributes', ->
|
||
|
request = jasmine.Ajax.requests.mostRecent()
|
||
|
expect(request.method).toBe('PUT')
|
||
|
expect(_.string.startsWith(request.url, '/db/user')).toBe(true)
|
||
|
expect(modal.$('#signup-button').is(':disabled')).toBe(true)
|
||
|
expect(request.url).toBe('/db/user?facebookID=abcd&facebookAccessToken=1234')
|