summaryrefslogtreecommitdiffstats
path: root/store/sqlstore/audit_store_test.go
blob: 4378c13e585be19ee30c0605a02e7a7d55610953 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

package sqlstore

import (
	"testing"
	"time"

	"github.com/mattermost/mattermost-server/model"
	"github.com/mattermost/mattermost-server/store"
)

func TestSqlAuditStore(t *testing.T) {
	ss := Setup()

	audit := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"}
	store.Must(ss.Audit().Save(audit))
	time.Sleep(100 * time.Millisecond)
	store.Must(ss.Audit().Save(audit))
	time.Sleep(100 * time.Millisecond)
	store.Must(ss.Audit().Save(audit))
	time.Sleep(100 * time.Millisecond)
	audit.ExtraInfo = "extra"
	time.Sleep(100 * time.Millisecond)
	store.Must(ss.Audit().Save(audit))

	time.Sleep(100 * time.Millisecond)

	c := ss.Audit().Get(audit.UserId, 0, 100)
	result := <-c
	audits := result.Data.(model.Audits)

	if len(audits) != 4 {
		t.Fatal("Failed to save and retrieve 4 audit logs")
	}

	if audits[0].ExtraInfo != "extra" {
		t.Fatal("Failed to save property for extra info")
	}

	c = ss.Audit().Get("missing", 0, 100)
	result = <-c
	audits = result.Data.(model.Audits)

	if len(audits) != 0 {
		t.Fatal("Should have returned empty because user_id is missing")
	}

	c = ss.Audit().Get("", 0, 100)
	result = <-c
	audits = result.Data.(model.Audits)

	if len(audits) < 4 {
		t.Fatal("Failed to save and retrieve 4 audit logs")
	}

	if r2 := <-ss.Audit().PermanentDeleteByUser(audit.UserId); r2.Err != nil {
		t.Fatal(r2.Err)
	}
}

func TestAuditStorePermanentDeleteBatch(t *testing.T) {
	ss := Setup()

	a1 := &model.Audit{UserId: model.NewId(), IpAddress: "ipaddress", Action: "Action"}
	store.Must(ss.Audit().Save(a1))
	time.Sleep(10 * time.Millisecond)
	a2 := &model.Audit{UserId: a1.UserId, IpAddress: "ipaddress", Action: "Action"}
	store.Must(ss.Audit().Save(a2))
	time.Sleep(10 * time.Millisecond)
	cutoff := model.GetMillis()
	time.Sleep(10 * time.Millisecond)
	a3 := &model.Audit{UserId: a1.UserId, IpAddress: "ipaddress", Action: "Action"}
	store.Must(ss.Audit().Save(a3))

	if r := <-ss.Audit().Get(a1.UserId, 0, 100); len(r.Data.(model.Audits)) != 3 {
		t.Fatal("Expected 3 audits. Got ", len(r.Data.(model.Audits)))
	}

	store.Must(ss.Audit().PermanentDeleteBatch(cutoff, 1000000))

	if r := <-ss.Audit().Get(a1.UserId, 0, 100); len(r.Data.(model.Audits)) != 1 {
		t.Fatal("Expected 1 audit. Got ", len(r.Data.(model.Audits)))
	}

	if r2 := <-ss.Audit().PermanentDeleteByUser(a1.UserId); r2.Err != nil {
		t.Fatal(r2.Err)
	}
}