Skip to content
Open

HT2 #21

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
5 changes: 5 additions & 0 deletions admin/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class App extends Component {
people
</NavLink>
</div>
<div>
<NavLink to="/admin/events" activeStyle={{ color: 'red' }}>
events
</NavLink>
</div>
<div>
<NavLink to="/auth/sign-in" activeStyle={{ color: 'red' }}>
auth
Expand Down
8 changes: 4 additions & 4 deletions admin/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import firebase from 'firebase/app'
import 'firebase/auth'
import 'firebase/database'

export const appName = 'advreact-10-05'
export const appName = 'advancedreactcourse'

export const config = {
apiKey: 'AIzaSyCbMQM0eQUSQ0SuLVAu9ZNPUcm4rdbiB8U',
apiKey: 'AIzaSyCCtMGA9FTNVds_QTkB1oRTlqF2u07MHuk',
authDomain: `${appName}.firebaseapp.com`,
databaseURL: `https://${appName}.firebaseio.com`,
projectId: appName,
storageBucket: '',
messagingSenderId: '1094825197832'
storageBucket: `${appName}.appspot.com`,
messagingSenderId: '649150663252'
}

firebase.initializeApp(config)
134 changes: 134 additions & 0 deletions admin/src/ducks/auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import { call, put, take, apply, takeEvery, all } from 'redux-saga/effects'
import firebase from 'firebase/app'

import {
signInSaga,
signUpSaga,
SIGN_IN_SUCCESS,
SIGN_IN_REQUEST,
SIGN_IN_ERROR,
SIGN_IN_REQUESTS_LIMIT,
SIGN_UP_SUCCESS,
SIGN_UP_REQUEST,
SIGN_UP_ERROR
} from './auth'

describe('Auth duck', () => {
const auth = firebase.auth()
const goodCredentials = {
email: 'exampl1@home.ru',
password: '12345678'
}

const badCredentials = {
email: 'newuser@home.ru',
password: '12345678'
}

describe('Positive cases', () => {
const user = apply(auth, auth.signInWithEmailAndPassword, [
goodCredentials.email,
goodCredentials.password
])

it('should sign in user', () => {
const sagaProcess = signInSaga()

expect(sagaProcess.next().value).toEqual(take(SIGN_IN_REQUEST))
expect(
sagaProcess.next({ type: SIGN_IN_REQUEST, payload: goodCredentials })
.value
).toEqual(user)
expect(sagaProcess.next(user).value).toEqual(
put({ type: SIGN_IN_SUCCESS, payload: { user } })
)
expect(sagaProcess.next().done).toEqual(false)
})

it('should sign up user', () => {
const sagaProcess = signUpSaga({
type: SIGN_UP_REQUEST,
payload: badCredentials
})

const user = call(
[auth, auth.createUserWithEmailAndPassword],
badCredentials.email,
badCredentials.password
)

expect(sagaProcess.next().value).toEqual(user)
expect(sagaProcess.next(user).value).toEqual(
put({ type: SIGN_UP_SUCCESS, payload: { user } })
)
expect(sagaProcess.next().done).toEqual(true)
})
})

describe('Negative cases', () => {
const user = apply(auth, auth.signInWithEmailAndPassword, [
badCredentials.email,
badCredentials.password
])

const error = new Error()

it('should reject signing in with invalid credentials', () => {
const sagaProcess = signInSaga()

expect(sagaProcess.next().value).toEqual(take(SIGN_IN_REQUEST))

expect(
sagaProcess.next({ type: SIGN_IN_REQUEST, payload: badCredentials })
.value
).toEqual(user)
// expect(sagaProcess.next(user).value).toEqual( put({ type: SIGN_IN_ERROR, error }))
// приходит success, не понимаю как решать
// throw делает искусственную ошибку, а не сага сама
expect(sagaProcess.throw(error).value).toEqual(
put({ type: SIGN_IN_ERROR, error })
)
expect(sagaProcess.next().done).toEqual(false)
})

it('should limit attempts of invalid signing in', () => {
const sagaProcess = signInSaga()

for (let i = 0; i < 3; i++) {
expect(sagaProcess.next().value).toEqual(take(SIGN_IN_REQUEST))

expect(
sagaProcess.next({ type: SIGN_IN_REQUEST, payload: badCredentials })
.value
).toEqual(user)
expect(sagaProcess.throw(error).value).toEqual(
put({ type: SIGN_IN_ERROR, error })
)
}

expect(sagaProcess.next().value).toEqual(
put({ type: SIGN_IN_REQUESTS_LIMIT })
)
expect(sagaProcess.next().done).toEqual(true)
})

it('should reject attempts to sign up with existing credentials', () => {
const sagaProcess = signUpSaga({
type: SIGN_UP_REQUEST,
payload: goodCredentials
})

const user = call(
[auth, auth.createUserWithEmailAndPassword],
goodCredentials.email,
goodCredentials.password
)

expect(sagaProcess.next().value).toEqual(user)
expect(sagaProcess.throw(error).value).toEqual(
put({ type: SIGN_UP_ERROR, error })
)
expect(sagaProcess.next().done).toEqual(true)
})
})
})
4 changes: 3 additions & 1 deletion admin/src/redux/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import { routerReducer as router } from 'react-router-redux'
import { reducer as form } from 'redux-form'
import authReducer, { moduleName as authModule } from '../ducks/auth'
import peopleReducer, { moduleName as peopleModule } from '../ducks/people'
import eventsReducer, { moduleName as eventsModule } from '../ducks/events'

export default combineReducers({
router,
form,
[authModule]: authReducer,
[peopleModule]: peopleReducer
[peopleModule]: peopleReducer,
[eventsModule]: eventsReducer
})
3 changes: 2 additions & 1 deletion admin/src/redux/saga.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { all } from 'redux-saga/effects'
import { saga as authSaga } from '../ducks/auth'
import { saga as peopleSaga } from '../ducks/people'
import { saga as eventsSaga } from '../ducks/events'

export default function*() {
yield all([authSaga(), peopleSaga()])
yield all([authSaga(), peopleSaga(), eventsSaga()])
}
2 changes: 2 additions & 0 deletions admin/src/routes/admin.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import PersonPage from './person-page'
import EventsPage from './events'

class AdminPage extends Component {
static propTypes = {}
Expand All @@ -10,6 +11,7 @@ class AdminPage extends Component {
<div>
<h1>Admin Page</h1>
<Route path="/admin/people" component={PersonPage} />
<Route path="/admin/events" component={EventsPage} />
</div>
)
}
Expand Down