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
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