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 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"})
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
}
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-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Nil(result.Error)
assertion.Equal(result.Data, true)
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-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
assertion.Equal(result.Error.Fields[0], "password")
2016-10-07 15:13:31 +02:00
result, w = session.JSONRequest("POST", "/login", RequestLogin{Username: "root2", Password: "root"})
2016-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
assertion.Equal(result.Error.Fields[0], "username")
2016-10-07 15:13:31 +02:00
result, w = session.JSONRequest("POST", "/login", []byte{2, 3})
2016-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusBadRequest)
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-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
2016-10-07 15:13:31 +02:00
// Test if crash on if not login in
result, w = session.JSONRequest("GET", "/logout", nil)
2016-10-17 14:07:17 +02:00
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
2016-10-16 19:00:53 +02:00
assertion.Equal(result.Data, false)
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-10-17 14:07:17 +02:00
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
2016-10-16 19:00:53 +02:00
assertion.Equal(result.Data, false)
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-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusBadRequest)
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-wrong", NewPassword: "root-bug"})
2016-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
assertion.Equal(result.Error.Fields[0], "currentpassword")
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: ""})
2016-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, false)
assertion.Equal(result.Error.Fields[0], "newpassword")
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root", NewPassword: "root-tmp"})
2016-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
2016-10-12 22:28:10 +02:00
result, w = session.JSONRequest("POST", "/password", ChangePasswordRequest{CurrentPassword: "root-tmp", NewPassword: "root"})
2016-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
assertion.Equal(result.Data, true)
2016-10-12 22:28:10 +02:00
/*
* TEST inviteList
*/
session.Clean()
result, w = session.JSONRequest("GET", "/invite", nil)
2016-10-17 14:07:17 +02:00
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
2016-10-16 19:00:53 +02:00
assertion.Equal(result.Data, false)
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-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
2016-10-12 22:28:10 +02:00
/*
* TEST loginList
*/
session.Clean()
result, w = session.JSONRequest("GET", "/user", nil)
2016-10-17 14:07:17 +02:00
assertion.Equal(w.StatusCode, http.StatusUnauthorized)
2016-10-16 19:00:53 +02:00
assertion.Equal(result.Data, false)
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-10-16 19:00:53 +02:00
assertion.Equal(w.StatusCode, http.StatusOK)
2016-10-07 15:13:31 +02:00
}