tests - service update

pull/41/head^2
Hunter Long 2018-08-12 12:23:04 -07:00
parent 849113e2d4
commit 5efef37243
7 changed files with 108 additions and 59 deletions

View File

@ -21,7 +21,7 @@ func CheckServices() {
for _, ser := range CoreApp.Services {
s := ser.ToService()
//go obj.StartCheckins()
s.StopRoutine = make(chan struct{})
s.Start()
go CheckQueue(s)
}
}
@ -32,6 +32,7 @@ func CheckQueue(s *types.Service) {
case <-s.StopRoutine:
return
default:
s = SelectService(s.Id).ToService()
ServiceCheck(s)
}
time.Sleep(time.Duration(s.Interval) * time.Second)

View File

@ -220,16 +220,17 @@ func DeleteService(u *types.Service) error {
return err
}
func UpdateService(u *types.Service) *types.Service {
u.CreatedAt = time.Now()
res := serviceCol().Find("id", u.Id)
err := res.Update(u)
func UpdateService(service *types.Service) *types.Service {
service.CreatedAt = time.Now()
res := serviceCol().Find("id", service.Id)
err := res.Update(service)
if err != nil {
utils.Log(3, fmt.Sprintf("Failed to update service %v. %v", u.Name, err))
utils.Log(3, fmt.Sprintf("Failed to update service %v. %v", service.Name, err))
return service
}
updateService(u)
OnUpdateService(u)
return u
CoreApp.Services, _ = SelectAllServices()
OnUpdateService(service)
return service
}
func updateService(u *types.Service) {
@ -251,7 +252,7 @@ func CreateService(u *types.Service) (int64, error) {
return 0, err
}
u.Id = uuid.(int64)
u.StopRoutine = make(chan struct{})
u.StopRoutine = make(chan bool)
CoreApp.Services = append(CoreApp.Services, &Service{u})
return uuid.(int64), err
}

View File

@ -1,6 +1,7 @@
package handlers
import (
"fmt"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/source"
"github.com/hunterlong/statup/utils"
@ -8,6 +9,7 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"strings"
"testing"
)
@ -17,6 +19,25 @@ func init() {
source.Assets()
}
func IsRouteAuthenticated(req *http.Request) bool {
os.Setenv("GO_ENV", "production")
req, err := http.NewRequest(req.Method, req.URL.String(), req.Body)
if err != nil {
os.Setenv("GO_ENV", "test")
return false
}
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
fmt.Println(req.URL.String(), rr.Code)
code := rr.Code
if code != 303 {
os.Setenv("GO_ENV", "test")
return false
}
os.Setenv("GO_ENV", "test")
return true
}
func TestIndexHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
assert.Nil(t, err)
@ -152,6 +173,7 @@ func TestServicesHandler(t *testing.T) {
assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Services</title>")
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestCreateUserHandler(t *testing.T) {
@ -166,6 +188,7 @@ func TestCreateUserHandler(t *testing.T) {
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.True(t, IsRouteAuthenticated(req))
}
func TestEditUserHandler(t *testing.T) {
@ -183,6 +206,7 @@ func TestEditUserHandler(t *testing.T) {
assert.Contains(t, body, "<td>admin</td>")
assert.Contains(t, body, "<td>changedusername</td>")
assert.Equal(t, 200, rr.Code)
assert.True(t, IsRouteAuthenticated(req))
}
func TestDeleteUserHandler(t *testing.T) {
@ -191,6 +215,7 @@ func TestDeleteUserHandler(t *testing.T) {
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.True(t, IsRouteAuthenticated(req))
}
func TestUsersHandler(t *testing.T) {
@ -204,6 +229,7 @@ func TestUsersHandler(t *testing.T) {
assert.Contains(t, body, "<td>admin</td>")
assert.NotContains(t, body, "<td>changedusername</td>")
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestUsersEditHandler(t *testing.T) {
@ -218,6 +244,7 @@ func TestUsersEditHandler(t *testing.T) {
assert.Contains(t, body, "value=\"info@statup.io\"")
assert.Contains(t, body, "value=\"##########\"")
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestSettingsHandler(t *testing.T) {
@ -229,6 +256,7 @@ func TestSettingsHandler(t *testing.T) {
assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Settings</title>")
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestHelpHandler(t *testing.T) {
@ -240,6 +268,7 @@ func TestHelpHandler(t *testing.T) {
assert.Equal(t, 200, rr.Code)
assert.Contains(t, body, "<title>Statup | Help</title>")
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestCreateHTTPServiceHandler(t *testing.T) {
@ -259,7 +288,8 @@ func TestCreateHTTPServiceHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
assert.True(t, IsRouteAuthenticated(req))
}
func TestCreateTCPerviceHandler(t *testing.T) {
@ -279,7 +309,8 @@ func TestCreateTCPerviceHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
assert.True(t, IsRouteAuthenticated(req))
}
func TestServicesHandler2(t *testing.T) {
@ -293,6 +324,7 @@ func TestServicesHandler2(t *testing.T) {
assert.Contains(t, body, "Crystal Castles - Kept")
assert.Contains(t, body, "Local Postgres")
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestViewHTTPServicesHandler(t *testing.T) {
@ -322,7 +354,7 @@ func TestServicesDeleteFailuresHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
}
func TestServicesUpdateHandler(t *testing.T) {
@ -352,7 +384,7 @@ func TestDeleteServiceHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
}
func TestLogsHandler(t *testing.T) {
@ -388,7 +420,7 @@ func TestSaveSettingsHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
}
func TestViewSettingsHandler(t *testing.T) {
@ -401,6 +433,7 @@ func TestViewSettingsHandler(t *testing.T) {
assert.Contains(t, body, "<title>Statup | Settings</title>")
assert.Contains(t, body, "Awesome Status")
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestSaveAssetsHandler(t *testing.T) {
@ -408,7 +441,7 @@ func TestSaveAssetsHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
assert.FileExists(t, utils.Directory+"/assets/css/base.css")
assert.FileExists(t, utils.Directory+"/assets/js/main.js")
assert.DirExists(t, utils.Directory+"/assets")
@ -420,7 +453,7 @@ func TestDeleteAssetsHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
assert.False(t, source.UsingAssets)
}
@ -452,7 +485,7 @@ func TestSaveNotificationHandler(t *testing.T) {
assert.Nil(t, err)
rr := httptest.NewRecorder()
Router().ServeHTTP(rr, req)
assert.Equal(t, 303, rr.Code)
assert.Equal(t, 200, rr.Code)
}
func TestViewNotificationSettingsHandler(t *testing.T) {
@ -471,6 +504,7 @@ func TestViewNotificationSettingsHandler(t *testing.T) {
assert.Contains(t, body, `value="7" id="limits_per_hour_email"`)
assert.Contains(t, body, `id="switch-email" checked`)
assert.Contains(t, body, "Statup made with ❤️")
assert.True(t, IsRouteAuthenticated(req))
}
func TestError404Handler(t *testing.T) {

View File

@ -62,7 +62,7 @@ func CreateServiceHandler(w http.ResponseWriter, r *http.Request) {
go core.CheckQueue(service)
core.OnNewService(service)
http.Redirect(w, r, "/services", http.StatusSeeOther)
ExecuteResponse(w, r, "services.html", core.CoreApp.Services)
}
func ServicesDeleteHandler(w http.ResponseWriter, r *http.Request) {
@ -74,7 +74,7 @@ func ServicesDeleteHandler(w http.ResponseWriter, r *http.Request) {
serv := core.SelectService(utils.StringInt(vars["id"]))
service := serv.ToService()
core.DeleteService(service)
http.Redirect(w, r, "/services", http.StatusSeeOther)
ExecuteResponse(w, r, "services.html", core.CoreApp.Services)
}
func ServicesViewHandler(w http.ResponseWriter, r *http.Request) {
@ -120,6 +120,8 @@ func ServicesUpdateHandler(w http.ResponseWriter, r *http.Request) {
Timeout: timeout,
}
service = core.UpdateService(serviceUpdate)
core.CoreApp.Services, _ = core.SelectAllServices()
serv = core.SelectService(service.Id)
ExecuteResponse(w, r, "service.html", serv)
}
@ -134,7 +136,7 @@ func ServicesDeleteFailuresHandler(w http.ResponseWriter, r *http.Request) {
service := serv.ToService()
core.DeleteFailures(service)
core.CoreApp.Services, _ = core.SelectAllServices()
http.Redirect(w, r, "/services", http.StatusSeeOther)
ExecuteResponse(w, r, "services.html", core.CoreApp.Services)
}
func CheckinCreateUpdateHandler(w http.ResponseWriter, r *http.Request) {

View File

@ -47,7 +47,7 @@ func SaveSettingsHandler(w http.ResponseWriter, r *http.Request) {
core.CoreApp.UseCdn = (r.PostForm.Get("enable_cdn") == "on")
core.CoreApp, _ = core.UpdateCore(core.CoreApp)
core.OnSettingsSaved(core.CoreApp.ToCore())
http.Redirect(w, r, "/settings", http.StatusSeeOther)
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func SaveSASSHandler(w http.ResponseWriter, r *http.Request) {
@ -61,7 +61,7 @@ func SaveSASSHandler(w http.ResponseWriter, r *http.Request) {
source.SaveAsset(theme, ".", "scss/base.scss")
source.SaveAsset(variables, ".", "scss/variables.scss")
source.CompileSASS(".")
http.Redirect(w, r, "/settings", http.StatusSeeOther)
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func SaveAssetsHandler(w http.ResponseWriter, r *http.Request) {
@ -77,18 +77,18 @@ func SaveAssetsHandler(w http.ResponseWriter, r *http.Request) {
utils.Log(2, "Default 'base.css' was insert because SASS did not work.")
}
source.UsingAssets = true
http.Redirect(w, r, "/settings", http.StatusSeeOther)
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func DeleteAssetsHandler(w http.ResponseWriter, req *http.Request) {
if !IsAuthenticated(req) {
http.Redirect(w, req, "/", http.StatusSeeOther)
func DeleteAssetsHandler(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
source.DeleteAllAssets(".")
source.UsingAssets = false
LocalizedAssets(r)
http.Redirect(w, req, "/settings", http.StatusSeeOther)
LocalizedAssets(Router())
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}
func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
@ -158,5 +158,5 @@ func SaveNotificationHandler(w http.ResponseWriter, r *http.Request) {
utils.Log(1, fmt.Sprintf("Notifier saved: %v", notifer))
http.Redirect(w, r, "/settings", http.StatusSeeOther)
ExecuteResponse(w, r, "settings.html", core.CoreApp)
}

40
types/service.go Normal file
View File

@ -0,0 +1,40 @@
package types
import "time"
type Service struct {
Id int64 `db:"id,omitempty" json:"id"`
Name string `db:"name" json:"name"`
Domain string `db:"domain" json:"domain"`
Expected string `db:"expected" json:"expected"`
ExpectedStatus int `db:"expected_status" json:"expected_status"`
Interval int `db:"check_interval" json:"check_interval"`
Type string `db:"check_type" json:"type"`
Method string `db:"method" json:"method"`
PostData string `db:"post_data" json:"post_data"`
Port int `db:"port" json:"port"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
Timeout int `db:"timeout" json:"timeout"`
Order int `db:"order_id" json:"order_id"`
Online bool `json:"online"`
Latency float64 `json:"latency"`
Online24Hours float32 `json:"24_hours_online"`
AvgResponse string `json:"avg_response"`
TotalUptime string `json:"uptime"`
OrderId int64 `json:"order_id"`
Failures []*Failure `json:"failures"`
Checkins []*Checkin `json:"checkins"`
StopRoutine chan bool `json:"-"`
LastResponse string
LastStatusCode int
LastOnline time.Time
DnsLookup float64 `json:"dns_lookup_time"`
}
func (s *Service) Start() {
s.StopRoutine = make(chan bool)
}
func (s *Service) Close() {
s.StopRoutine <- true
}

View File

@ -67,35 +67,6 @@ type Core struct {
Started time.Time
}
type Service struct {
Id int64 `db:"id,omitempty" json:"id"`
Name string `db:"name" json:"name"`
Domain string `db:"domain" json:"domain"`
Expected string `db:"expected" json:"expected"`
ExpectedStatus int `db:"expected_status" json:"expected_status"`
Interval int `db:"check_interval" json:"check_interval"`
Type string `db:"check_type" json:"type"`
Method string `db:"method" json:"method"`
PostData string `db:"post_data" json:"post_data"`
Port int `db:"port" json:"port"`
CreatedAt time.Time `db:"created_at" json:"created_at"`
Timeout int `db:"timeout" json:"timeout"`
Order int `db:"order_id" json:"order_id"`
Online bool `json:"online"`
Latency float64 `json:"latency"`
Online24Hours float32 `json:"24_hours_online"`
AvgResponse string `json:"avg_response"`
TotalUptime string `json:"uptime"`
OrderId int64 `json:"order_id"`
Failures []*Failure `json:"failures"`
Checkins []*Checkin `json:"checkins"`
StopRoutine chan struct{} `json:"-"`
LastResponse string
LastStatusCode int
LastOnline time.Time
DnsLookup float64 `json:"dns_lookup_time"`
}
type User struct {
Id int64 `db:"id,omitempty" json:"id"`
Username string `db:"username" json:"username"`