mirror of
https://github.com/arnaucube/commonroutesServer.git
synced 2026-02-28 05:26:42 +01:00
added tests. Added admin signup system
This commit is contained in:
3
adminConfig.js
Normal file
3
adminConfig.js
Normal file
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
'passwordHash': 'Bzij4hEeEUpmXTWyS+X0LR+YcA8WFjP2P7qhW0sxA6s='/*password raw: adminPassword*/
|
||||
};
|
||||
@@ -7,6 +7,7 @@ var travelModel = mongoose.model('travelModel');
|
||||
var travelCtrl = require('../controllers/travelController');
|
||||
|
||||
var config = require('../config');
|
||||
var adminConfig = require('../adminConfig'); // get our config file
|
||||
var pageSize = config.pageSize;
|
||||
|
||||
/* */
|
||||
@@ -24,29 +25,29 @@ var request = require('request');
|
||||
|
||||
//POST - Insert a new User in the DB
|
||||
exports.signup = function(req, res) {
|
||||
//get random avatar
|
||||
var r = getRand(1, 10);
|
||||
randAvatar = getAvatar(r);
|
||||
|
||||
|
||||
var user = new userModel({
|
||||
var admin = new adminModel({
|
||||
username: req.body.username,
|
||||
password: crypto.createHash('sha256').update(req.body.password).digest('base64'),
|
||||
description: req.body.description,
|
||||
avatar: randAvatar,
|
||||
email: req.body.email,
|
||||
phone: req.body.phone,
|
||||
telegram: req.body.telegram
|
||||
});
|
||||
if (user.username == undefined) {
|
||||
if (admin.username == undefined) {
|
||||
return res.status(500).jsonp("empty inputs");
|
||||
} else if (user.password == undefined) {
|
||||
} else if (admin.password == undefined) {
|
||||
return res.status(500).jsonp("empty inputs");
|
||||
} else if (user.email == undefined) {
|
||||
} else if (admin.email == undefined) {
|
||||
return res.status(500).jsonp("empty inputs");
|
||||
}
|
||||
|
||||
user.save(function(err, user) {
|
||||
adminPasswordGetted = crypto.createHash('sha256').update(req.body.adminPassword).digest('base64');
|
||||
console.log(adminPasswordGetted);
|
||||
console.log(adminConfig.passwordHash);
|
||||
if (adminPasswordGetted != adminConfig.passwordHash) {
|
||||
return res.status(500).jsonp("admin password not valid");
|
||||
}
|
||||
admin.save(function(err, admin) {
|
||||
if (err) return res.send(500, err.message);
|
||||
|
||||
exports.login(req, res);
|
||||
|
||||
@@ -77,6 +77,8 @@ apiRoutes.route('/travels/id/:travelid')
|
||||
|
||||
apiRoutes.route('/admin/login')
|
||||
.post(adminCtrl.login);
|
||||
apiRoutes.route('/admin/signup')
|
||||
.post(adminCtrl.signup);
|
||||
|
||||
// OJU AQUÏ TREC la verificació de token temporalment, per fer les proves des de l'app
|
||||
// route middleware to verify a token
|
||||
|
||||
106
tests.js
Normal file
106
tests.js
Normal file
@@ -0,0 +1,106 @@
|
||||
var request = require('request');
|
||||
|
||||
var url = "http://127.0.0.1:3000/api";
|
||||
var users = [{
|
||||
username: "u1",
|
||||
password: "u1",
|
||||
email: "u1"
|
||||
},
|
||||
{
|
||||
username: "u2",
|
||||
password: "u2",
|
||||
email: "u2"
|
||||
},
|
||||
{
|
||||
username: "u3",
|
||||
password: "u3",
|
||||
email: "u3"
|
||||
}
|
||||
];
|
||||
//signup
|
||||
function signup(user) {
|
||||
var data = {
|
||||
username: user.username,
|
||||
password: user.password,
|
||||
email: user.email
|
||||
};
|
||||
request({
|
||||
url: url + "/signup",
|
||||
method: "POST",
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: data,
|
||||
json: true
|
||||
}, function(err, httpResponse, body) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
//console.log(body);
|
||||
login(user);
|
||||
}
|
||||
});
|
||||
}
|
||||
//login
|
||||
function login(user) {
|
||||
var data = {
|
||||
username: user.username,
|
||||
password: user.email
|
||||
};
|
||||
request({
|
||||
url: url + "/login",
|
||||
method: "POST",
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
body: data,
|
||||
json: true
|
||||
}, function(err, httpResponse, body) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log(body.token);
|
||||
user.token = JSON.parse(JSON.stringify(body.token));
|
||||
console.log(user.username);
|
||||
addTravel(user);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function addTravel(user) {
|
||||
var data = {
|
||||
title: "travel",
|
||||
description: "description4",
|
||||
from: "placeA",
|
||||
to: "placeB",
|
||||
date: "2017-10-29T22:58:59.000Z",
|
||||
seats: 3,
|
||||
package: true,
|
||||
collectivized: true,
|
||||
type: "offer"
|
||||
};
|
||||
request({
|
||||
url: url + "/login",
|
||||
method: "POST",
|
||||
headers: {
|
||||
'content-type': 'application/json',
|
||||
'x-access-token': user.token
|
||||
},
|
||||
body: data,
|
||||
json: true
|
||||
}, function(err, httpResponse, body) {
|
||||
if (err) {
|
||||
console.log(err);
|
||||
} else {
|
||||
console.log(body);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
for (var i = 0; i < users.length; i++) {
|
||||
console.log(i);
|
||||
console.log(users[i].username);
|
||||
setTimeout(function() {
|
||||
signup(users[i]);
|
||||
}, 2000);
|
||||
}
|
||||
3
tests/config.json
Normal file
3
tests/config.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"url": "http://localhost:3000/api"
|
||||
}
|
||||
15
tests/main.go
Normal file
15
tests/main.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
|
||||
readConfig()
|
||||
fmt.Println(config)
|
||||
readUsers()
|
||||
fmt.Println(users)
|
||||
|
||||
user := signup(users[0])
|
||||
user = login(users[0])
|
||||
fmt.Println(user)
|
||||
}
|
||||
39
tests/readConfig.go
Normal file
39
tests/readConfig.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
var config Config
|
||||
|
||||
type User struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
|
||||
var users []User
|
||||
|
||||
func readUsers() {
|
||||
file, e := ioutil.ReadFile("users.json")
|
||||
if e != nil {
|
||||
fmt.Println("error:", e)
|
||||
}
|
||||
content := string(file)
|
||||
json.Unmarshal([]byte(content), &users)
|
||||
}
|
||||
func readConfig() {
|
||||
file, e := ioutil.ReadFile("config.json")
|
||||
if e != nil {
|
||||
fmt.Println("error:", e)
|
||||
}
|
||||
content := string(file)
|
||||
json.Unmarshal([]byte(content), &config)
|
||||
}
|
||||
89
tests/requests.go
Normal file
89
tests/requests.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type LoginResp struct {
|
||||
Token string `json:"token"`
|
||||
User User
|
||||
}
|
||||
|
||||
func signup(user User) User {
|
||||
var loginResp LoginResp
|
||||
url := config.Url + "/signup"
|
||||
jsonStr, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(string(jsonStr))
|
||||
b := strings.NewReader(string(jsonStr))
|
||||
req, _ := http.NewRequest("POST", url, b)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, _ := ioutil.ReadAll(res.Body)
|
||||
|
||||
json.Unmarshal([]byte(body), &loginResp)
|
||||
|
||||
fmt.Println("token: " + loginResp.Token)
|
||||
user.Token = loginResp.Token
|
||||
return user
|
||||
}
|
||||
func login(user User) User {
|
||||
var loginResp LoginResp
|
||||
url := config.Url + "/login"
|
||||
jsonStr, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(string(jsonStr))
|
||||
b := strings.NewReader(string(jsonStr))
|
||||
req, _ := http.NewRequest("POST", url, b)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, _ := ioutil.ReadAll(res.Body)
|
||||
|
||||
json.Unmarshal([]byte(body), &loginResp)
|
||||
|
||||
fmt.Println("token: " + loginResp.Token)
|
||||
user.Token = loginResp.Token
|
||||
return user
|
||||
}
|
||||
|
||||
func addTravel(user User, travel Travel) (User, Travel) {
|
||||
var loginResp LoginResp
|
||||
url := config.Url + "/login"
|
||||
jsonStr, err := json.Marshal(user)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
}
|
||||
fmt.Println(string(jsonStr))
|
||||
b := strings.NewReader(string(jsonStr))
|
||||
req, _ := http.NewRequest("POST", url, b)
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
res, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, _ := ioutil.ReadAll(res.Body)
|
||||
|
||||
json.Unmarshal([]byte(body), &loginResp)
|
||||
|
||||
fmt.Println("token: " + loginResp.Token)
|
||||
user.Token = loginResp.Token
|
||||
return user
|
||||
}
|
||||
16
tests/users.json
Normal file
16
tests/users.json
Normal file
@@ -0,0 +1,16 @@
|
||||
[{
|
||||
"username": "u1",
|
||||
"password": "u1",
|
||||
"email": "u1"
|
||||
},
|
||||
{
|
||||
"username": "u2",
|
||||
"password": "u2",
|
||||
"email": "u2"
|
||||
},
|
||||
{
|
||||
"username": "u3",
|
||||
"password": "u3",
|
||||
"email": "u3"
|
||||
}
|
||||
]
|
||||
Reference in New Issue
Block a user