Commit 6b54ec2f authored by Pieterjan Vanhoof's avatar Pieterjan Vanhoof Committed by GitHub

Navigate with HTTP status, edit wallet load, unit tests (#26)

- Handling HTTP status codes (temporary catching all statuses)
- Added default unit tests for all components
- Edited wallet load function
- Moved API error handling to components
parents d727e6f6 685da94a
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { remote } from 'electron';
import { ApiService } from './shared/api/api.service'; import { ApiService } from './shared/api/api.service';
@Component({ @Component({
...@@ -13,9 +11,8 @@ import { ApiService } from './shared/api/api.service'; ...@@ -13,9 +11,8 @@ import { ApiService } from './shared/api/api.service';
export class AppComponent implements OnInit { export class AppComponent implements OnInit {
constructor(private router: Router, private apiService: ApiService) {} constructor(private router: Router, private apiService: ApiService) {}
private errorMessage: string; private errorMessage: any;
private response: any; private responseMessage: any;
private isConfigured: boolean = true;
ngOnInit() { ngOnInit() {
this.checkWalletStatus(); this.checkWalletStatus();
...@@ -24,22 +21,19 @@ export class AppComponent implements OnInit { ...@@ -24,22 +21,19 @@ export class AppComponent implements OnInit {
private checkWalletStatus(){ private checkWalletStatus(){
this.apiService.getWalletStatus() this.apiService.getWalletStatus()
.subscribe( .subscribe(
response => this.response = response, response => {
error => this.errorMessage = <any>error, if (response.status >= 200 && response.status < 400) {
() => this.navigate() this.responseMessage = response;
this.router.navigate(['/login']);
}
},
error => {
this.errorMessage = <any>error;
if (error.status === 400 || error.status === 404) {
this.router.navigate(['/setup']);
console.log(this.errorMessage);
}
}
); );
} }
private navigate() {
if (this.response.success === "true") {
// remote.dialog.showMessageBox({message: remote.app.getPath('userData')})
this.router.navigate(['/login'])
} else {
this.router.navigate(['/setup'])
}
}
private hasWallet() {
return true;
}
} }
\ No newline at end of file
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'; import { Router } from '@angular/router';
import { ApiService } from '../shared/api/api.service'; import { ApiService } from '../shared/api/api.service';
import { WalletLoad } from '../shared/wallet-load';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
...@@ -10,27 +11,34 @@ import { ApiService } from '../shared/api/api.service'; ...@@ -10,27 +11,34 @@ import { ApiService } from '../shared/api/api.service';
export class LoginComponent implements OnInit { export class LoginComponent implements OnInit {
constructor(private apiService: ApiService, private router: Router) { } constructor(private apiService: ApiService, private router: Router) { }
private response: any; private responseMessage: any;
private errorMessage: string; private errorMessage: any;
private walletLoad: WalletLoad;
ngOnInit() { ngOnInit() {
} }
private onSubmit() { private onSubmit() {
this.apiService.loadWallet("123") this.walletLoad = new WalletLoad();
this.walletLoad.password = "123";
this.walletLoad.name = "test"
this.walletLoad.folderPath = "folderPath"
this.apiService.loadWallet(this.walletLoad)
.subscribe( .subscribe(
response => this.response = response, response => {
error => this.errorMessage = error, if (response.status >= 200 && response.status < 400) {
() => this.loadWallet() this.responseMessage = response;
this.router.navigate['/wallet']
}
},
error => {
this.errorMessage = <any>error;
if (error.status >= 400) {
alert(this.errorMessage);
console.log(this.errorMessage);
}
}
); );
} }
}
private loadWallet() { \ No newline at end of file
if (this.response.success === "true") {
this.router.navigate(['/wallet/send']);
} else {
alert("Something went wrong.")
}
}
}
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { CreateComponent } from './create.component';
describe('CreateComponent', () => {
let component: CreateComponent;
let fixture: ComponentFixture<CreateComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(CreateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
...@@ -15,7 +15,9 @@ export class CreateComponent { ...@@ -15,7 +15,9 @@ export class CreateComponent {
constructor(private apiService: ApiService) {} constructor(private apiService: ApiService) {}
private newWallet: WalletCreation; private newWallet: WalletCreation;
private responseMessage: string; private responseMessage: string;
private errorMessage: string;
private createWallet(password: string, network: string, folderPath: string, name: string, ) { private createWallet(password: string, network: string, folderPath: string, name: string, ) {
this.newWallet = new WalletCreation(); this.newWallet = new WalletCreation();
...@@ -26,6 +28,18 @@ export class CreateComponent { ...@@ -26,6 +28,18 @@ export class CreateComponent {
this.apiService this.apiService
.createWallet(this.newWallet) .createWallet(this.newWallet)
.subscribe((response: string) => this.responseMessage = response); .subscribe(
response => {
if (response.status >= 200 && response.status < 400){
this.responseMessage = response;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = error;
console.log(this.errorMessage);
}
}
);
} }
} }
...@@ -10,8 +10,11 @@ import { WalletRecovery } from '../../shared/wallet-recovery' ...@@ -10,8 +10,11 @@ import { WalletRecovery } from '../../shared/wallet-recovery'
export class RecoverComponent implements OnInit { export class RecoverComponent implements OnInit {
constructor(private apiService: ApiService) { } constructor(private apiService: ApiService) { }
private walletRecovery: WalletRecovery; private walletRecovery: WalletRecovery;
private responseBody: string;
private responseMessage: string;
private errorMessage: string;
ngOnInit() { ngOnInit() {
} }
...@@ -26,7 +29,18 @@ export class RecoverComponent implements OnInit { ...@@ -26,7 +29,18 @@ export class RecoverComponent implements OnInit {
this.apiService this.apiService
.recoverWallet(this.walletRecovery) .recoverWallet(this.walletRecovery)
.subscribe((response: string) => this.responseBody = response, .subscribe(
() => console.log("recoverWallet() completed")); response => {
if (response.status >= 200 && response.status < 400) {
this.responseMessage = response;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = error;
console.log(this.errorMessage);
}
}
);
} }
} }
\ No newline at end of file
...@@ -6,6 +6,7 @@ import 'rxjs/add/operator/catch'; ...@@ -6,6 +6,7 @@ import 'rxjs/add/operator/catch';
import { WalletCreation } from '../wallet-creation'; import { WalletCreation } from '../wallet-creation';
import { WalletRecovery } from '../wallet-recovery'; import { WalletRecovery } from '../wallet-recovery';
import { WalletLoad } from '../wallet-load';
import { Mnemonic } from '../mnemonic'; import { Mnemonic } from '../mnemonic';
/** /**
...@@ -26,7 +27,7 @@ export class ApiService { ...@@ -26,7 +27,7 @@ export class ApiService {
createWallet(data: WalletCreation): Observable<any> { createWallet(data: WalletCreation): Observable<any> {
return this.http return this.http
.post(this.webApiUrl + '/wallet/create/', JSON.stringify(data), {headers: this.headers}) .post(this.webApiUrl + '/wallet/create/', JSON.stringify(data), {headers: this.headers})
.map(response => response.json()); .map((response: Response) => response);
} }
/** /**
...@@ -35,17 +36,16 @@ export class ApiService { ...@@ -35,17 +36,16 @@ export class ApiService {
recoverWallet(data: WalletRecovery): Observable<any> { recoverWallet(data: WalletRecovery): Observable<any> {
return this.http return this.http
.post(this.webApiUrl + '/wallet/recover/', JSON.stringify(data), {headers: this.headers}) .post(this.webApiUrl + '/wallet/recover/', JSON.stringify(data), {headers: this.headers})
.map(response => response.json()); .map((response: Response) => response);
} }
/** /**
* Load a wallet * Load a wallet
*/ */
loadWallet(password: string): Observable<any> { loadWallet(data: WalletLoad): Observable<any> {
return this.http return this.http
.get(this.webApiUrl + '/wallet/load/', {headers: this.headers, body: JSON.stringify(password)}) .get(this.webApiUrl + '/wallet/load/', {headers: this.headers, body: JSON.stringify(data)})
.map(response => response.json()) .map((response: Response) => response);
.catch(this.handleError);
} }
/** /**
...@@ -54,8 +54,7 @@ export class ApiService { ...@@ -54,8 +54,7 @@ export class ApiService {
getWalletStatus(): Observable<any> { getWalletStatus(): Observable<any> {
return this.http return this.http
.get(this.mockApiUrl + '/wallet/status') .get(this.mockApiUrl + '/wallet/status')
.map((response:Response) => response.json()) .map((response: Response) => response);
.catch(this.handleError);
} }
/** /**
...@@ -64,8 +63,7 @@ export class ApiService { ...@@ -64,8 +63,7 @@ export class ApiService {
getWalletBalance(): Observable<any> { getWalletBalance(): Observable<any> {
return this.http return this.http
.get(this.webApiUrl + '/wallet/balance') .get(this.webApiUrl + '/wallet/balance')
.map((response:Response) => response.json()) .map((response: Response) => response);
.catch(this.handleError);
} }
/** /**
...@@ -74,8 +72,7 @@ export class ApiService { ...@@ -74,8 +72,7 @@ export class ApiService {
getWalletHistory(): Observable<any> { getWalletHistory(): Observable<any> {
return this.http return this.http
.get(this.webApiUrl + '/wallet/history') .get(this.webApiUrl + '/wallet/history')
.map((response:Response) => response.json()) .map((response: Response) => response);
.catch(this.handleError);
} }
/** /**
...@@ -84,24 +81,8 @@ export class ApiService { ...@@ -84,24 +81,8 @@ export class ApiService {
getUnusedReceiveAddresses(): Observable<any> { getUnusedReceiveAddresses(): Observable<any> {
return this.http return this.http
.get(this.webApiUrl + '/wallet/receive') .get(this.webApiUrl + '/wallet/receive')
.map((response:Response) => response.json()) .map((response: Response) => response);
.catch(this.handleError);
} }
/**
* Handle errors from the API.
* @param error
*/
private handleError (error: Response | any) {
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : error.toString();
}
console.error(errMsg);
return Observable.throw(errMsg);
} }
} }
export class WalletLoad {
password: string;
folderPath: string;
name: string;
}
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HistoryComponent } from './history.component';
describe('HistoryComponent', () => {
let component: HistoryComponent;
let fixture: ComponentFixture<HistoryComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ HistoryComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(HistoryComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
...@@ -21,8 +21,17 @@ export class HistoryComponent { ...@@ -21,8 +21,17 @@ export class HistoryComponent {
private getWalletHistory() { private getWalletHistory() {
this.apiService.getWalletHistory() this.apiService.getWalletHistory()
.subscribe( .subscribe(
response => this.transactions = response.history, response => {
error => this.errorMessage = <any>error if (response.status >= 200 && response.status < 400) {
this.transactions = response.history;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = <any>error;
console.log(this.errorMessage);
}
}
); );
} }
} }
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DashboardComponent } from './dashboard.component';
describe('DashboardComponent', () => {
let component: DashboardComponent;
let fixture: ComponentFixture<DashboardComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ DashboardComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(DashboardComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
...@@ -21,14 +21,19 @@ export class DashboardComponent { ...@@ -21,14 +21,19 @@ export class DashboardComponent {
private getWalletBalance() { private getWalletBalance() {
this.apiService.getWalletBalance() this.apiService.getWalletBalance()
.subscribe( .subscribe(
response => this.balanceResponse = response, response => {
error => this.errorMessage = <any>error, if (response.status >= 200 && response.status < 400) {
() => this.setBalance() this.balanceResponse = response
this.confirmedBalance = this.balanceResponse.confirmed;
this.unconfirmedBalance = this.balanceResponse.unconfirmed;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = <any>error;
console.log(this.errorMessage);
}
}
); );
} }
private setBalance() {
this.confirmedBalance = this.balanceResponse.confirmed;
this.unconfirmedBalance = this.balanceResponse.unconfirmed;
}
} }
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MenuComponent } from './menu.component';
describe('MenuComponent', () => {
let component: MenuComponent;
let fixture: ComponentFixture<MenuComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ MenuComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(MenuComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReceiveComponent } from './receive.component';
describe('ReceiveComponent', () => {
let component: ReceiveComponent;
let fixture: ComponentFixture<ReceiveComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ReceiveComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ReceiveComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
...@@ -21,8 +21,17 @@ export class ReceiveComponent { ...@@ -21,8 +21,17 @@ export class ReceiveComponent {
private getUnusedReceiveAddresses() { private getUnusedReceiveAddresses() {
this.apiService.getUnusedReceiveAddresses() this.apiService.getUnusedReceiveAddresses()
.subscribe( .subscribe(
response => this.addresses = response.addresses, response => {
error => this.errorMessage = <any>error if (response.status >= 200 && response.status < 400) {
this.addresses = response.addresses;
}
},
error => {
if (error.status >= 400) {
this.errorMessage = <any>error;
console.log(this.errorMessage);
}
}
); );
} }
} }
\ No newline at end of file
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { SendComponent } from './send.component';
describe('SendComponent', () => {
let component: SendComponent;
let fixture: ComponentFixture<SendComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SendComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(SendComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { WalletComponent } from './wallet.component';
describe('WalletComponent', () => {
let component: WalletComponent;
let fixture: ComponentFixture<WalletComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ WalletComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(WalletComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment