diff options
| author | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2022-07-03 13:51:37 -0700 |
|---|---|---|
| committer | Jeff Halter <868228+jhalter@users.noreply.github.com> | 2022-07-03 13:51:37 -0700 |
| commit | 8a1512f9d2c16c8d811099fbb3a168cc2d7ec074 (patch) | |
| tree | 46702fd67b7c5c72ff3332889879fdd7c864a8c6 /hotline | |
| parent | 969e6481f12ed859e42ed81699547d2d33b0637f (diff) | |
Fix filepath handling and backfill test
Diffstat (limited to 'hotline')
| -rw-r--r-- | hotline/transaction_handlers.go | 9 | ||||
| -rw-r--r-- | hotline/transaction_handlers_test.go | 91 |
2 files changed, 95 insertions, 5 deletions
diff --git a/hotline/transaction_handlers.go b/hotline/transaction_handlers.go index 4866bee..3ecb6bd 100644 --- a/hotline/transaction_handlers.go +++ b/hotline/transaction_handlers.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "gopkg.in/yaml.v3" - "io/ioutil" "math/big" "os" "path" @@ -1001,14 +1000,14 @@ func HandleTranOldPostNews(cc *ClientConn, t *Transaction) (res []Transaction, e newsPost := fmt.Sprintf(newsTemplate+"\r", cc.UserName, time.Now().Format(newsDateTemplate), t.GetField(fieldData).Data) newsPost = strings.Replace(newsPost, "\n", "\r", -1) - // update news in memory - cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...) - // update news on disk - if err := ioutil.WriteFile(cc.Server.ConfigDir+"MessageBoard.txt", cc.Server.FlatNews, 0644); err != nil { + if err := cc.Server.FS.WriteFile(filepath.Join(cc.Server.ConfigDir, "MessageBoard.txt"), cc.Server.FlatNews, 0644); err != nil { return res, err } + // update news in memory + cc.Server.FlatNews = append([]byte(newsPost), cc.Server.FlatNews...) + // Notify all clients of updated news cc.sendAll( tranNewMsg, diff --git a/hotline/transaction_handlers_test.go b/hotline/transaction_handlers_test.go index ca466d5..0a95f62 100644 --- a/hotline/transaction_handlers_test.go +++ b/hotline/transaction_handlers_test.go @@ -3195,3 +3195,94 @@ func TestHandleDownloadBanner(t *testing.T) { }) } } + +func TestHandleTranOldPostNews(t *testing.T) { + type args struct { + cc *ClientConn + t *Transaction + } + tests := []struct { + name string + args args + wantRes []Transaction + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when user does not have required permission", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + return bits + }(), + }, + }, + t: NewTransaction( + tranOldPostNews, &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x00}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 1}, + Fields: []Field{ + NewField(fieldError, []byte("You are not allowed to post news.")), + }, + }, + }, + wantErr: assert.NoError, + }, + { + name: "when user posts news update", + args: args{ + cc: &ClientConn{ + Account: &Account{ + Access: func() accessBitmap { + var bits accessBitmap + bits.Set(accessNewsPostArt) + return bits + }(), + }, + Server: &Server{ + FS: func() *MockFileStore { + mfs := &MockFileStore{} + mfs.On("WriteFile", "/fakeConfigRoot/MessageBoard.txt", mock.Anything, mock.Anything).Return(nil, os.ErrNotExist) + return mfs + }(), + ConfigDir: "/fakeConfigRoot", + Config: &Config{}, + }, + }, + t: NewTransaction( + tranOldPostNews, &[]byte{0, 1}, + NewField(fieldData, []byte("hai")), + ), + }, + wantRes: []Transaction{ + { + Flags: 0x00, + IsReply: 0x01, + Type: []byte{0, 0x67}, + ID: []byte{0, 0, 0, 0}, + ErrorCode: []byte{0, 0, 0, 0}, + }, + }, + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotRes, err := HandleTranOldPostNews(tt.args.cc, tt.args.t) + if !tt.wantErr(t, err, fmt.Sprintf("HandleTranOldPostNews(%v, %v)", tt.args.cc, tt.args.t)) { + return + } + + tranAssertEqual(t, tt.wantRes, gotRes) + }) + } +} |