~launchpad-pqm/launchpad/devel

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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
YUI({
    base: '/+icing/yui/',
    filter: 'raw', combine: false, fetchCSS: false
}).use('test', 'console', 'json', 'cookie', 'lp.testing.serverfixture',
       function(Y) {

var suite = new Y.Test.Suite("lp.testing.yuixhr Tests");
var module = Y.lp.testing.serverfixture;


/**
 * Test setup and teardown of yuixhr fixtures, and the asociated JS module.
 */
suite.add(new Y.Test.Case({
    name: 'Fixture setup and teardown tests',

    tearDown: function() {
        delete this._lp_fixture_setups;
        delete this._lp_fixture_data;
    },

    _should: {
        error: {
            test_bad_http_call_raises_error: true,
            test_bad_http_teardown_raises_error: true
        }
    },

    test_simple_setup: function() {
        var data = module.setup(this, 'baseline');
        Y.ArrayAssert.itemsAreEqual(['baseline'], this._lp_fixture_setups);
        Y.ObjectAssert.areEqual({'hello': 'world'}, data);
        Y.ObjectAssert.areEqual({'hello': 'world'}, this._lp_fixture_data);
        module.teardown(this); // Just for cleanliness, not for testing.
    },

    test_setup_with_multiple_fixtures: function() {
        var data = module.setup(this, 'baseline', 'second');
        Y.ArrayAssert.itemsAreEqual(
            ['baseline', 'second'], this._lp_fixture_setups);
        Y.ObjectAssert.areEqual({'hello': 'world', 'second': 'here'}, data);
        Y.ObjectAssert.areEqual(
            {'hello': 'world', 'second': 'here'}, this._lp_fixture_data);
        module.teardown(this); // Just for cleanliness, not for testing.
    },

    test_multiple_setup_calls: function() {
        var data = module.setup(this, 'baseline');
        var second_data = module.setup(this, 'second');
        Y.ArrayAssert.itemsAreEqual(
            ['baseline', 'second'], this._lp_fixture_setups);
        Y.ObjectAssert.areEqual({'hello': 'world'}, data);
        Y.ObjectAssert.areEqual({'second': 'here'}, second_data);
        Y.ObjectAssert.areEqual(
            {'hello': 'world', 'second': 'here'}, this._lp_fixture_data);
        module.teardown(this); // Just for cleanliness, not for testing.
    },

    test_teardown_clears_attributes: function() {
        var data = module.setup(this, 'baseline');
        module.teardown(this);
        Y.Assert.isUndefined(this._lp_fixture_setups);
        Y.Assert.isUndefined(this._lp_fixture_data);
    },

    test_bad_http_call_raises_error: function() {
        module.setup(this, 'does not exist');
    },

    test_bad_http_call_shows_traceback: function() {
        try {module.setup(this, 'does not exist');}
        catch (err) {
            Y.Assert.areEqual('Traceback (most recent call last)',
                              err.message.substring(0, 33));
        }      
    },

    test_bad_http_teardown_raises_error: function() {
        module.setup(this, 'teardown_will_fail');
        module.teardown(this);
    },

    test_bad_http_teardown_shows_traceback: function() {
        module.setup(this, 'teardown_will_fail');
        try {module.teardown(this);}
        catch (err) {
            Y.Assert.areEqual('Traceback (most recent call last)',
                              err.message.substring(0, 33));
        }
    },

    test_setup_called_twice_with_same_fixture: function() {
        // This is arguably not desirable, but it is the way it works now.
        var data = module.setup(this, 'baseline');
        var second_data = module.setup(this, 'baseline');
        Y.ArrayAssert.itemsAreEqual(
            ['baseline', 'baseline'], this._lp_fixture_setups);
        Y.ObjectAssert.areEqual({'hello': 'world'}, data);
        Y.ObjectAssert.areEqual({'hello': 'world'}, second_data);
        Y.ObjectAssert.areEqual(
            {'hello': 'world'}, this._lp_fixture_data);
        module.teardown(this); // Just for cleanliness, not for testing.
    },

    test_teardown: function() {
        module.setup(this, 'faux_database_thing');
        module.teardown(this);
        var data = module.setup(this, 'faux_database_thing');
        Y.ObjectAssert.areEqual(
            {'previous_value': 'teardown was called'}, data);
    },

    test_teardown_receives_data_from_setup: function() {
        module.setup(this, 'show_teardown_value');
        module.teardown(this);
        var data = module.setup(this, 'faux_database_thing');
        Y.ObjectAssert.areEqual(
            {'setup_data': 'Hello world'}, data.previous_value);
    },

    test_teardown_resets_database: function() {
        var data = module.setup(this, 'make_product');
        var response = Y.io(
            data.product.self_link,
            {sync: true}
            );
        Y.Assert.areEqual(200, response.status);
        module.teardown(this);
        var response = Y.io(
            data.product.self_link,
            {sync: true}
            );
        Y.Assert.areEqual(404, response.status);
    },

    test_login_works: function() {
        // Make sure the session cookie is cleared out at start of test.
        Y.Cookie.remove('launchpad_tests');
        // Make a product
        var data = module.setup(this, 'make_product');
        // We can't see this because only Launchpad and Registry admins can.
        Y.Assert.areEqual(
            'tag:launchpad.net:2008:redacted', data.product.project_reviewed);
        // Login as a Launchpad admin.
        module.setup(this, 'login_as_admin');
        // The session cookie is set.
        Y.Assert.isTrue(Y.Cookie.exists('launchpad_tests'));
        // We can now see things that only a Launchpad admin can see.
        var response = Y.io(
            data.product.self_link,
            {sync: true, headers: {Accept: 'application/json'}}
            );
        var result = Y.JSON.parse(response.responseText);
        Y.Assert.areEqual(false, result.project_reviewed);
        module.teardown(this);
        // After teardown, the database is cleared out, as shown in other
        // tests, so we are not logged in practically--the user is gone--but
        // also our session cookie is gone.
        Y.Assert.isFalse(Y.Cookie.exists('launchpad_tests'));
    }
}));

var handle_complete = function(data) {
    window.status = '::::' + Y.JSON.stringify(data);
    };
Y.Test.Runner.on('complete', handle_complete);
Y.Test.Runner.add(suite);

var console = new Y.Console({newestOnTop: false});

Y.on('domready', function() {
    console.render('#log');
    Y.Test.Runner.run();
});
});