sum7/warehost
sum7
/
warehost
Archived
1
0
Fork 0
This repository has been archived on 2020-09-27. You can view files and clone it, but cannot push or open issues or pull requests.
warehost/system/api_test.go

129 lines
3.8 KiB
Go
Raw Permalink Normal View History

2016-10-07 15:13:31 +02:00
package system
import (
"net/http"
"testing"
2016-10-16 19:00:53 +02:00
"github.com/stretchr/testify/assert"
2016-10-11 20:16:24 +02:00
"dev.sum7.eu/sum7/warehost/test"
2016-10-07 15:13:31 +02:00
)
2016-10-16 19:00:53 +02:00
func loginTest(session *test.Request, assertion *assert.Assertions) {
result, w := session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-16 19:00:53 +02:00
}
2016-10-07 15:13:31 +02:00
func TestAPI(t *testing.T) {
2016-10-16 19:00:53 +02:00
assertion, db, router := test.Init(t)
2016-10-07 15:13:31 +02:00
defer db.Close()
//load system Models to database
SyncModels(db)
// Bind API
2016-10-11 20:16:24 +02:00
BindAPI(db, router, "")
2016-10-07 15:13:31 +02:00
session := test.NewSession(router)
2016-10-12 22:28:10 +02:00
/*
* TEST status
*/
2016-10-07 15:13:31 +02:00
result, w := session.JSONRequest("GET", "/status", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
2016-10-16 19:00:53 +02:00
assertion.Nil(result.Error)
2016-12-19 12:24:18 +01:00
assertion.Equal(true, result.Data)
2016-10-07 15:13:31 +02:00
2016-10-12 22:28:10 +02:00
/*
* TEST login
*/
2016-10-07 15:13:31 +02:00
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root", Password: "root2"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result.Data)
assertion.Equal("password", result.Error.Fields[0])
2016-10-07 15:13:31 +02:00
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result.Data)
assertion.Equal("username", result.Error.Fields[0])
2016-10-07 15:13:31 +02:00
result, w = session.JSONRequest("POST", "/login", []byte{2, 3})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusBadRequest, w.StatusCode)
2016-10-07 15:13:31 +02:00
2016-10-16 19:00:53 +02:00
//login before
loginTest(session, assertion)
2016-10-07 15:13:31 +02:00
2016-10-12 22:28:10 +02:00
/*
* TEST logout
*/
2016-10-07 15:13:31 +02:00
result, w = session.JSONRequest("GET", "/logout", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-07 15:13:31 +02:00
// Test if crash on if not login in
result, w = session.JSONRequest("GET", "/logout", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-12 22:28:10 +02:00
/*
* TEST password
*/
2016-10-17 14:07:17 +02:00
session.Clean()
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-bug"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-12 22:28:10 +02:00
//login before
2016-10-16 19:00:53 +02:00
loginTest(session, assertion)
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", []byte{2, 3})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusBadRequest, w.StatusCode)
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-wrong", NewPassword: "root-bug"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result.Data)
assertion.Equal("currentpassword", result.Error.Fields[0])
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: ""})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(false, result.Data)
assertion.Equal("newpassword", result.Error.Fields[0])
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-tmp"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-tmp", NewPassword: "root"})
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
assertion.Equal(true, result.Data)
2016-10-12 22:28:10 +02:00
/*
* TEST inviteList
*/
session.Clean()
result, w = session.JSONRequest("GET", "/invite", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-12 22:28:10 +02:00
//login before
2016-10-16 19:00:53 +02:00
loginTest(session, assertion)
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("GET", "/invite", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
2016-10-12 22:28:10 +02:00
/*
* TEST loginList
*/
session.Clean()
result, w = session.JSONRequest("GET", "/user", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusUnauthorized, w.StatusCode)
assertion.Equal(false, result.Data)
2016-10-12 22:28:10 +02:00
//login before
2016-10-16 19:00:53 +02:00
loginTest(session, assertion)
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("GET", "/user", nil)
2016-12-19 12:24:18 +01:00
assertion.Equal(http.StatusOK, w.StatusCode)
2016-10-07 15:13:31 +02:00
}