summary refs log tree commit diff
path: root/syweb/webclient/test/unit/commands-service.spec.js
blob: 142044f1535c222b5c14a59263e448d1528fbb7f (plain) (blame)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
describe('CommandsService', function() {
    var scope;
    var roomId = "!dlwifhweu:localhost";
    
    var testPowerLevelsEvent, testMatrixServicePromise;
    
    var matrixService = { // these will be spyed on by jasmine, hence stub methods
        setDisplayName: function(args){},
        kick: function(args){},
        ban: function(args){},
        unban: function(args){},
        setUserPowerLevel: function(args){}
    };
    
    var modelService = {
        getRoom: function(roomId) {
            return {
                room_id: roomId,
                current_room_state: {
                    events: {
                        "m.room.power_levels": testPowerLevelsEvent
                    },
                    state: function(type, key) {
                        return key ? this.events[type+key] : this.events[type];
                    }
                }
            };
        }
    };
    
    
    // helper function for asserting promise outcomes
    NOTHING = "[Promise]";
    RESOLVED = "[Resolved promise]";
    REJECTED = "[Rejected promise]";
    var expectPromise = function(promise, expects) {
        var value = NOTHING;
        promise.then(function(result) {
            value = RESOLVED;
        }, function(fail) {
            value = REJECTED;
        });
        scope.$apply();
        expect(value).toEqual(expects);
    };

    // setup the service and mocked dependencies
    beforeEach(function() {
        
        // set default mock values
        testPowerLevelsEvent = {
            content: {
                default: 50
            },
            user_id: "@foo:bar",
            room_id: roomId
        }
        
        // mocked dependencies
        module(function ($provide) {
          $provide.value('matrixService', matrixService);
          $provide.value('modelService', modelService);
        });
        
        // tested service
        module('commandsService');
    });
    
    beforeEach(inject(function($rootScope, $q) {
        scope = $rootScope;
        testMatrixServicePromise = $q.defer();
    }));

    it('should reject a no-arg "/nick".', inject(
    function(commandsService) {
        var promise = commandsService.processInput(roomId, "/nick");
        expectPromise(promise, REJECTED);
    }));
    
    it('should be able to set a /nick with multiple words.', inject(
    function(commandsService) {
        spyOn(matrixService, 'setDisplayName').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/nick Bob Smith");
        expect(matrixService.setDisplayName).toHaveBeenCalledWith("Bob Smith");
        expect(promise).toBe(testMatrixServicePromise);
    }));
    
    it('should be able to /kick a user without a reason.', inject(
    function(commandsService) {
        spyOn(matrixService, 'kick').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/kick @bob:matrix.org");
        expect(matrixService.kick).toHaveBeenCalledWith(roomId, "@bob:matrix.org", undefined);
        expect(promise).toBe(testMatrixServicePromise);
    }));
    
    it('should be able to /kick a user with a reason.', inject(
    function(commandsService) {
        spyOn(matrixService, 'kick').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/kick @bob:matrix.org he smells");
        expect(matrixService.kick).toHaveBeenCalledWith(roomId, "@bob:matrix.org", "he smells");
        expect(promise).toBe(testMatrixServicePromise);
    }));
    
    it('should be able to /ban a user without a reason.', inject(
    function(commandsService) {
        spyOn(matrixService, 'ban').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/ban @bob:matrix.org");
        expect(matrixService.ban).toHaveBeenCalledWith(roomId, "@bob:matrix.org", undefined);
        expect(promise).toBe(testMatrixServicePromise);
    }));
    
    it('should be able to /ban a user with a reason.', inject(
    function(commandsService) {
        spyOn(matrixService, 'ban').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/ban @bob:matrix.org he smells");
        expect(matrixService.ban).toHaveBeenCalledWith(roomId, "@bob:matrix.org", "he smells");
        expect(promise).toBe(testMatrixServicePromise);
    }));
    
    it('should be able to /unban a user.', inject(
    function(commandsService) {
        spyOn(matrixService, 'unban').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/unban @bob:matrix.org");
        expect(matrixService.unban).toHaveBeenCalledWith(roomId, "@bob:matrix.org");
        expect(promise).toBe(testMatrixServicePromise);
    }));
    
    it('should be able to /op a user.', inject(
    function(commandsService) {
        spyOn(matrixService, 'setUserPowerLevel').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/op @bob:matrix.org 50");
        expect(matrixService.setUserPowerLevel).toHaveBeenCalledWith(roomId, "@bob:matrix.org", 50, testPowerLevelsEvent);
        expect(promise).toBe(testMatrixServicePromise);
    }));
    
    it('should be able to /deop a user.', inject(
    function(commandsService) {
        spyOn(matrixService, 'setUserPowerLevel').and.returnValue(testMatrixServicePromise);
        var promise = commandsService.processInput(roomId, "/deop @bob:matrix.org");
        expect(matrixService.setUserPowerLevel).toHaveBeenCalledWith(roomId, "@bob:matrix.org", undefined, testPowerLevelsEvent);
        expect(promise).toBe(testMatrixServicePromise);
    }));
});